777ebb61fdff90053d08f2ca312db83f1385f832
- Author
- Toby Padilla <toby@charm.sh>
- Committer
- Toby Padilla <toby@charm.sh>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/tui/bubble.go b/tui/bubble.go
2index a12a20925270b387fc74ad3cb7bc33ab64022f2c..cead1f5bdb057ea456da0bb805831fba9a67d7ed 100644
3--- a/tui/bubble.go
4+++ b/tui/bubble.go
5@@ -1,13 +1,11 @@
6 package tui
7
8 import (
9- "encoding/json"
10 "fmt"
11- "log"
12 "smoothie/git"
13 "smoothie/tui/bubbles/commits"
14+ "smoothie/tui/bubbles/repo"
15 "smoothie/tui/bubbles/selection"
16- "time"
17
18 tea "github.com/charmbracelet/bubbletea"
19 "github.com/charmbracelet/lipgloss"
20@@ -34,9 +32,10 @@ type Config struct {
21 }
22
23 type MenuEntry struct {
24- Name string `json:"name"`
25- Note string `json:"note"`
26- Repo string `json:"repo"`
27+ Name string `json:"name"`
28+ Note string `json:"note"`
29+ Repo string `json:"repo"`
30+ bubble *repo.Bubble
31 }
32
33 type SessionConfig struct {
34@@ -68,6 +67,7 @@ func NewBubble(cfg *Config, sCfg *SessionConfig) *Bubble {
35 height: sCfg.Height,
36 windowChanges: sCfg.WindowChanges,
37 repoSource: cfg.RepoSource,
38+ repoMenu: make([]MenuEntry, 0),
39 boxes: make([]tea.Model, 2),
40 }
41 b.state = startState
42@@ -100,9 +100,13 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
43 b.height = msg.Height
44 case selection.SelectedMsg:
45 b.activeBox = 1
46- cmds = append(cmds, b.getRepoCmd(b.repoMenu[msg.Index].Repo))
47+ rb := b.repoMenu[msg.Index].bubble
48+ rb.GotoTop()
49+ b.boxes[1] = rb
50 case selection.ActiveMsg:
51- cmds = append(cmds, b.getRepoCmd(b.repoMenu[msg.Index].Repo))
52+ rb := b.repoMenu[msg.Index].bubble
53+ rb.GotoTop()
54+ b.boxes[1] = b.repoMenu[msg.Index].bubble
55 }
56 if b.state == loadedState {
57 ab, cmd := b.boxes[b.activeBox].Update(msg)
58@@ -136,77 +140,7 @@ func (b *Bubble) View() string {
59 s += lipgloss.JoinHorizontal(lipgloss.Top, lb, rb)
60 case errorState:
61 s += errorStyle.Render(fmt.Sprintf("Bummer: %s", b.error))
62- default:
63- s = normalStyle.Render(fmt.Sprintf("Doing something weird %d", b.state))
64 }
65 content = h + "\n\n" + s + "\n" + f
66 return appBoxStyle.Render(content)
67 }
68-
69-func loadConfig(rs *git.RepoSource) (*Config, error) {
70- cfg := &Config{}
71- cfg.RepoSource = rs
72- cr, err := rs.GetRepo("config")
73- if err != nil {
74- return nil, fmt.Errorf("cannot load config repo: %s", err)
75- }
76- cs, err := cr.LatestFile("config.json")
77- if err != nil {
78- return nil, fmt.Errorf("cannot load config.json: %s", err)
79- }
80- err = json.Unmarshal([]byte(cs), cfg)
81- if err != nil {
82- return nil, fmt.Errorf("bad json in config.json: %s", err)
83- }
84- return cfg, nil
85-}
86-
87-func SessionHandler(reposPath string, repoPoll time.Duration) func(ssh.Session) (tea.Model, []tea.ProgramOption) {
88- rs := git.NewRepoSource(reposPath)
89- err := createDefaultConfigRepo(rs)
90- if err != nil {
91- if err != nil {
92- log.Fatalf("cannot create config repo: %s", err)
93- }
94- }
95- appCfg, err := loadConfig(rs)
96- if err != nil {
97- if err != nil {
98- log.Printf("cannot load config: %s", err)
99- }
100- }
101- go func() {
102- for {
103- time.Sleep(repoPoll)
104- err := rs.LoadRepos()
105diff --git a/tui/bubbles/repo/bubble.go b/tui/bubbles/repo/bubble.go
106index dbfd40e0b104bbfbc727ab6fab5ee45207d2906b..be50cfeaff4975c0cb3aa0de59e401cf7ce5b16d 100644
107--- a/tui/bubbles/repo/bubble.go
108+++ b/tui/bubbles/repo/bubble.go
109@@ -50,12 +50,19 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
110 return b, tea.Batch(cmds...)
111 }
112
113+func (b *Bubble) GotoTop() {
114+ b.readmeViewport.Viewport.GotoTop()
115+}
116+
117 func (b *Bubble) View() string {
118 return b.readmeViewport.View()
119 }
120
121 func (b *Bubble) setupCmd() tea.Msg {
122 r, err := b.repoSource.GetRepo(b.name)
123+ if err == git.ErrMissingRepo {
124+ return nil
125+ }
126 if err != nil {
127 return ErrMsg{err}
128 }
129@@ -70,7 +77,7 @@ func (b *Bubble) setupCmd() tea.Msg {
130 if err != nil {
131 return ErrMsg{err}
132 }
133- b.readmeViewport.Viewport.GotoTop()
134+ b.GotoTop()
135 b.readmeViewport.Viewport.SetContent(md)
136 return nil
137 }
138diff --git a/tui/commands.go b/tui/commands.go
139index 0cfaa03ef2567be62265f13feaf8d743140c1198..a1ebe49c19788977dd64750b998dc9cec707b1af 100644
140--- a/tui/commands.go
141+++ b/tui/commands.go
142@@ -1,6 +1,7 @@
143 package tui
144
145 import (
146+ "fmt"
147 "smoothie/tui/bubbles/commits"
148 "smoothie/tui/bubbles/repo"
149 "smoothie/tui/bubbles/selection"
150@@ -43,8 +44,22 @@ func (b *Bubble) setupCmd() tea.Msg {
151 mes = append(mes, MenuEntry{Name: r.Name, Repo: r.Name})
152 }
153 }
154- b.repoMenu = mes
155+ var tmplConfig *Config
156+ h := b.height - verticalPadding - viewportHeightConstant
157+ w := boxRightWidth - 2
158 for _, me := range mes {
159+ if me.Repo == "config" {
160+ tmplConfig = b.config
161+ }
162+ rb := repo.NewBubble(b.repoSource, me.Repo, w, h, tmplConfig)
163+ initCmd := rb.Init()
164+ msg := initCmd()
165+ switch msg := msg.(type) {
166+ case repo.ErrMsg:
167+ return errMsg{fmt.Errorf("missing %s: %s", me.Repo, msg.Error)}
168+ }
169+ me.bubble = rb
170+ b.repoMenu = append(b.repoMenu, me)
171 rs = append(rs, me.Name)
172 }
173 b.repoSelect = selection.NewBubble(rs)
174@@ -54,20 +69,8 @@ func (b *Bubble) setupCmd() tea.Msg {
175 boxRightWidth-horizontalPadding-2,
176 b.repoSource.GetCommits(200),
177 )
178- msg := b.getRepoCmd("config")()
179+ b.boxes[1] = b.repoMenu[0].bubble
180 b.activeBox = 0
181 b.state = loadedState
182- return msg
183-}
184-
185-func (b *Bubble) getRepoCmd(name string) tea.Cmd {
186- var tmplConfig *Config
187- if name == "config" {
188- tmplConfig = b.config
189- }
190- h := b.height - verticalPadding - viewportHeightConstant
191- w := boxRightWidth - 2
192- rb := repo.NewBubble(b.repoSource, name, w, h, tmplConfig)
193- b.boxes[1] = rb
194- return rb.Init()
195+ return nil
196 }
197diff --git a/tui/session.go b/tui/session.go
198new file mode 100644
199index 0000000000000000000000000000000000000000..c00f6674eb330b067c60fb1239e673fbb48ead88
200--- /dev/null
201+++ b/tui/session.go
202@@ -0,0 +1,80 @@
203+package tui
204+
205+import (
206+ "encoding/json"
207+ "fmt"
208+ "log"
209+ "smoothie/git"
210+ "time"
211+
212+ tea "github.com/charmbracelet/bubbletea"
213+ "github.com/gliderlabs/ssh"
214+)
215+
216+func SessionHandler(reposPath string, repoPoll time.Duration) func(ssh.Session) (tea.Model, []tea.ProgramOption) {
217+ rs := git.NewRepoSource(reposPath)
218+ err := createDefaultConfigRepo(rs)
219+ if err != nil {
220+ if err != nil {
221+ log.Fatalf("cannot create config repo: %s", err)
222+ }
223+ }
224+ appCfg, err := loadConfig(rs)
225+ if err != nil {
226+ if err != nil {
227+ log.Printf("cannot load config: %s", err)
228+ }
229+ }
230+ go func() {
231+ for {
232+ time.Sleep(repoPoll)
233+ err := rs.LoadRepos()
234+ if err != nil {
235+ log.Printf("cannot load repos: %s", err)
236+ continue
237+ }
238+ cfg, err := loadConfig(rs)
239+ if err != nil {
240+ if err != nil {
241+ log.Printf("cannot load config: %s", err)
242+ continue
243+ }
244+ }
245+ appCfg = cfg
246+ }
247+ }()
248+
249+ return func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
250+ if len(s.Command()) == 0 {
251+ pty, changes, active := s.Pty()
252+ if !active {
253+ return nil, nil
254+ }
255+ cfg := &SessionConfig{
256+ Width: pty.Window.Width,
257+ Height: pty.Window.Height,
258+ WindowChanges: changes,
259+ }
260+ return NewBubble(appCfg, cfg), []tea.ProgramOption{tea.WithAltScreen()}
261+ }
262+ return nil, nil
263+ }
264+}
265+
266+func loadConfig(rs *git.RepoSource) (*Config, error) {
267+ cfg := &Config{}
268+ cfg.RepoSource = rs
269+ cr, err := rs.GetRepo("config")
270+ if err != nil {
271+ return nil, fmt.Errorf("cannot load config repo: %s", err)
272+ }
273+ cs, err := cr.LatestFile("config.json")
274+ if err != nil {
275+ return nil, fmt.Errorf("cannot load config.json: %s", err)
276+ }
277+ err = json.Unmarshal([]byte(cs), cfg)
278+ if err != nil {
279+ return nil, fmt.Errorf("bad json in config.json: %s", err)
280+ }
281+ return cfg, nil
282+}