TUI IDE multiplexer?!
golang
tui
ide
1package main
2
3import (
4 "fmt"
5
6 "github.com/godbus/dbus/v5"
7 "github.com/godbus/dbus/v5/introspect"
8 "github.com/rivo/tview"
9)
10
11type Editors struct {
12 conn *dbus.Conn // Assigned from app.
13 env *[]string // Assigned from app.
14 setFocusFunc func(tview.Primitive) // Calls app's SetFocus
15
16 editors []*Term
17 lastFocusedEditor int
18
19 *tview.Flex
20}
21
22func (s *Editors) Setup() error {
23 // UI
24 s.Flex = tview.NewFlex()
25
26 // D-Bus
27 if err := app.Conn.ExportMethodTable(map[string]any{
28 "SetFocus": func() *dbus.Error {
29 if editor := s.GetLastFocusedEditor(); editor != nil {
30 s.setFocusFunc(editor)
31 }
32 return nil
33 },
34 "SendString": func(str string) *dbus.Error {
35 if editor := s.GetLastFocusedEditor(); editor != nil {
36 return editor.SendString(str)
37 }
38 return nil
39 },
40 "SendBytes": func(b []byte) *dbus.Error {
41 if editor := s.GetLastFocusedEditor(); editor != nil {
42 return editor.SendBytes(b)
43 }
44 return nil
45 },
46 }, dbus.ObjectPath("/panels/editor/active"), "net.kettek.Aight.Panel"); err != nil {
47 return err
48 }
49 if err := app.Conn.Export(introspect.Introspectable(termIntro), dbus.ObjectPath("/panels/editor/active"), "org.freedesktop.DBus.Introspectable"); err != nil {
50 return err
51 }
52
53 return nil
54}
55
56func (s *Editors) Cleanup() error {
57 // D-Bus
58 for _, editor := range s.editors {
59 editor.RemoveFromConn(s.conn)
60 }
61
62 s.conn.Export(nil, dbus.ObjectPath("/panels/editor/active"), "org.freedesktop.DBus.Introspectable")
63 s.conn.Export(nil, dbus.ObjectPath("/panels/editor/active"), "net.kettek.Aight.Status")
64
65 return nil
66}
67
68func (s *Editors) AddEditor() error {
69 id := len(s.editors)
70 editor := NewTerm(fmt.Sprintf("editor/%d", id), cfg.Editor)
71 editor.SetFocusFunc(func() {
72 s.lastFocusedEditor = id
73 })
74
75 // Add focus handler.
76 if err := s.conn.ExportMethodTable(map[string]any{
77 "SetFocus": func() *dbus.Error {
78 if editor := s.GetEditorByIndex(s.lastFocusedEditor); editor != nil {
79 s.setFocusFunc(editor)
80 }
81 return nil
82 },
83 "SendString": func(s string) *dbus.Error {
84 return editor.SendString(s)
85 },
86 "SendBytes": func(b []byte) *dbus.Error {
87 return editor.SendBytes(b)
88 },
89 }, dbus.ObjectPath(fmt.Sprintf("/panels/editor/%d", id)), "net.kettek.Aight.Panel"); err != nil {
90 return err
91 }
92
93 // Start 'er up.
94 if err := editor.Start(*s.env); err != nil {
95 return err
96 }
97
98 s.editors = append(s.editors, editor)
99
100 // Add to UI.
101 s.AddItem(editor, 0, 1, true)
102
103 return editor.AddToConn(s.conn)
104}
105
106func (s *Editors) GetEditorByIndex(id int) *Term {
107 if id > len(s.editors) {
108 return nil
109 }
110 return s.editors[id]
111}
112
113func (s *Editors) GetLastFocusedEditor() *Term {
114 return s.GetEditorByIndex(s.lastFocusedEditor)
115}