package main import ( "fmt" "github.com/gdamore/tcell/v2" "github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5/introspect" "github.com/rivo/tview" ) type Shells struct { conn *dbus.Conn // Assigned from app. env *[]string // Assigned from app. setFocusFunc func(tview.Primitive) // Calls app's SetFocus shells []*Term lastFocusedShell int *Resizable } func (s *Shells) Setup() error { // UI s.Resizable = NewResizable("shells", false) // D-Bus if err := app.Conn.ExportMethodTable(map[string]any{ "SetFocus": func() *dbus.Error { if shell := s.GetLastFocusedShell(); shell != nil { s.setFocusFunc(shell) } return nil }, "SendString": func(str string) *dbus.Error { if shell := s.GetLastFocusedShell(); shell != nil { return shell.SendString(str) } return nil }, "SendBytes": func(b []byte) *dbus.Error { if shell := s.GetLastFocusedShell(); shell != nil { return shell.SendBytes(b) } return nil }, }, dbus.ObjectPath("/panels/shell/active"), "net.kettek.Aight.Panel"); err != nil { return err } if err := app.Conn.Export(introspect.Introspectable(termIntro), dbus.ObjectPath("/panels/shell/active"), "org.freedesktop.DBus.Introspectable"); err != nil { return err } return nil } func (s *Shells) Draw(screen tcell.Screen) { s.Resizable.Draw(screen) // Uh.. titles, I guess for index, item := range s.items { title := s.shells[index].state.Title() if title == "" { continue } x, y, w, _ := item.item.GetRect() x1 := x + w/2 - len(title)/2 screen.PutStr(x1, y-1, title) // -1 y to draw over the gripper } } func (s *Shells) Cleanup() error { // D-Bus for _, shell := range s.shells { shell.RemoveFromConn(s.conn) } s.conn.Export(nil, dbus.ObjectPath("/panels/shell/active"), "org.freedesktop.DBus.Introspectable") s.conn.Export(nil, dbus.ObjectPath("/panels/shell/active"), "net.kettek.Aight.Status") return nil } func (s *Shells) AddShell() error { id := len(s.shells) shell := NewTerm(fmt.Sprintf("shell/%d", id), cfg.Shell) shell.SetFocusFunc(func() { s.lastFocusedShell = id }) // Add focus handler. if err := s.conn.ExportMethodTable(map[string]any{ "SetFocus": func() *dbus.Error { if shell := s.GetShellByIndex(id); shell != nil { s.setFocusFunc(shell) } return nil }, "SendString": func(s string) *dbus.Error { if _, err := shell.vt.File().WriteString(s); err != nil { return dbus.NewError("Send", []any{err.Error()}) } return nil }, "SendBytes": func(b []byte) *dbus.Error { if _, err := shell.vt.File().Write(b); err != nil { return dbus.NewError("Send", []any{err.Error()}) } return nil }, }, dbus.ObjectPath(fmt.Sprintf("/panels/shell/%d", id)), "net.kettek.Aight.Panel"); err != nil { return err } // Start 'er up. if err := shell.Start(*s.env); err != nil { return err } s.shells = append(s.shells, shell) // Add to UI. s.AddItem(shell, 0, false) return shell.AddToConn(s.conn) } func (s *Shells) GetShellByIndex(id int) *Term { if id > len(s.shells) { return nil } return s.shells[id] } func (s *Shells) GetLastFocusedShell() *Term { return s.GetShellByIndex(s.lastFocusedShell) }