1
1
package auto
2
2
3
3
import (
4
- "fmt"
5
4
"log"
6
- "io/ioutil"
7
5
"strings"
6
+ "os"
8
7
9
8
tea "github.com/charmbracelet/bubbletea"
10
9
"github.com/charmbracelet/bubbles/textarea"
11
- "github.com/charmbracelet/bubbles/cursor"
12
10
"github.com/mitchellh/go-wordwrap"
13
11
"github.com/charmbracelet/bubbles/viewport"
14
12
"github.com/charmbracelet/lipgloss"
@@ -26,9 +24,11 @@ const (
26
24
)
27
25
28
26
type Auto struct {
29
- common common.Common
27
+ common * common.Common
30
28
styles common.Styles
29
+ picked pane
31
30
tabs * tabs.Tabs
31
+ panes []tea.Model
32
32
33
33
textarea textarea.Model
34
34
state State
@@ -41,21 +41,26 @@ type Auto struct {
41
41
height int
42
42
}
43
43
44
- func New (c common.Common , s common.Styles ) * Auto {
44
+ func New (c * common.Common , s common.Styles ) * Auto {
45
45
a := & Auto {
46
46
common : c ,
47
47
textarea : textarea .New (),
48
48
styles : s ,
49
- tabs : tabs . New ( c , [] string { "chat" , "history" }) ,
49
+ picked : paneChat ,
50
50
viewport : viewport .New (0 , 0 ),
51
+ tabs : InitTabs (c ),
52
+ panes : []tea.Model {
53
+ nil ,
54
+ nil ,
55
+ },
51
56
}
52
57
a .viewport .YPosition = 0
53
58
54
59
a .textarea .Placeholder = ">"
55
60
a .textarea .ShowLineNumbers = false
56
61
a .textarea .Focus ()
57
62
58
- data , err := ioutil .ReadFile ("chat/llm_1.json" )
63
+ data , err := os .ReadFile ("chat/llm_1.json" )
59
64
if err != nil {
60
65
log .Fatal (err )
61
66
}
@@ -67,20 +72,65 @@ func New(c common.Common, s common.Styles) *Auto {
67
72
return a
68
73
}
69
74
75
+ func (a * Auto ) Init () tea.Cmd {
76
+ return nil
77
+ }
78
+
79
+ type pane int
80
+
81
+ const (
82
+ paneChat pane = iota
83
+ paneHistory
84
+ paneLast
85
+ )
86
+
87
+ func (p pane ) String () string {
88
+ return []string {
89
+ "Chat" ,
90
+ "History" ,
91
+ }[p ]
92
+ }
93
+
94
+ func InitTabs (c * common.Common ) * tabs.Tabs {
95
+ ts := make ([]string , paneLast )
96
+ for i , b := range []pane {paneChat , paneHistory } {
97
+ ts [i ] = b .String ()
98
+ }
99
+ t := tabs .New (c , ts )
100
+ return t
101
+ }
102
+
70
103
func (a * Auto ) Focus () tea.Cmd {
71
104
return textarea .Blink
72
105
}
73
106
74
107
func (a * Auto ) SetSize (width , height int ) {
75
- a .width = width
76
- a .height = height
108
+ a .common .SetSize (width , height )
77
109
a .textarea .SetWidth (width )
78
110
a .textarea .MaxHeight = height / 2
79
111
a .viewport .Width = width
80
112
a .viewport .Height = height
81
113
}
82
114
115
+ func (a * Auto ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
116
+ cmds := make ([]tea.Cmd , 0 )
117
+ switch msg := msg .(type ) {
118
+ case tabs.ActiveTabMsg :
119
+ a .picked = pane (msg )
120
+ }
121
+ model , cmd := a .tabs .Update (msg )
122
+ cmds = append (cmds , cmd )
123
+ a .tabs = model .(* tabs.Tabs )
124
+
125
+ /*
126
+ model, cmd = a.panes[a.picked].Update(msg)
127
+ cmds = append(cmds, cmd)
128
+ a.panes[a.picked] = model
129
+ */
130
+ return a , tea .Batch (cmds ... )
131
+ }
83
132
133
+ /*
84
134
func (a *Auto) Update(msg tea.Msg) (*Auto, tea.Cmd) {
85
135
var cmd tea.Cmd
86
136
cmds := make([]tea.Cmd, 0)
@@ -120,38 +170,46 @@ func (a *Auto) Update(msg tea.Msg) (*Auto, tea.Cmd) {
120
170
}
121
171
return a, tea.Batch(cmds...)
122
172
}
173
+ */
123
174
124
175
func (a * Auto ) wrap (in string ) string {
125
176
return wordwrap .WrapString (in , uint (a .textarea .Width ()/ 2 ))
126
177
}
127
178
128
179
func (a * Auto ) View () string {
129
- // FIXME use string builder
130
- view := a .styles .Logo .Render (" Alfredo " )
131
- view += "\n \n "
132
- view += a .tabs .View ()
133
- view += "\n \n "
134
- view += a .styles .ContextTag .String () + " " + a .styles .Context .Render (a .wrap (a .Response .Context ))
135
- view += "\n \n "
136
- view += a .styles .GoalTag .String () + " " + a .styles .Goal .Render (a .wrap (a .Response .Goal ))
137
- view += "\n \n "
180
+ var builder strings.Builder
181
+
182
+ builder .WriteString (a .styles .Logo .Render (" Alfredo " ))
183
+ builder .WriteString ("\n \n " )
184
+ builder .WriteString (a .tabs .View ())
185
+ builder .WriteString ("\n \n " )
186
+ builder .WriteString (a .styles .ContextTag .String () + " " + a .styles .Context .Render (a .wrap (a .Response .Context )))
187
+ /*
188
+ builder.WriteString("\n\n")
189
+ builder.WriteString(a.styles.GoalTag.String() + " " + a.styles.Goal.Render(a.wrap(a.Response.Goal)))
190
+ builder.WriteString("\n\n")
191
+
138
192
if a.state == stateAnswering {
139
193
if len(a.outputs) < len(a.Response.Commands) {
140
- var styledCmds []string
141
- for _ , cmd := range a .Response .Commands {
142
- styledCmds = append (styledCmds , fmt .Sprintf ("$ %s" , a .wrap (cmd )))
143
- }
144
- styledCmdStr := strings .Join (styledCmds , "\n " )
145
- view += a .styles .Command .Render (styledCmdStr ) + "\n \n "
146
- view += a .styles .Comment .Render ("Press y to execute all commands or esc to exit" )
194
+ var styledCmds []string
195
+ for _, cmd := range a.Response.Commands {
196
+ styledCmds = append(styledCmds, fmt.Sprintf("$ %s", a.wrap(cmd)))
197
+ }
198
+ styledCmdStr := strings.Join(styledCmds, "\n")
199
+ builder.WriteString( a.styles.Command.Render(styledCmdStr) + "\n\n")
200
+ builder.WriteString( a.styles.Comment.Render("Press y to execute all commands or esc to exit") )
147
201
} else if len(a.answers) < len(a.Response.Questions) {
148
202
question := a.Response.Questions[len(a.answers)]
149
- view += a .styles .QuestionTag .String () + " " + a .styles .Question .Render (a .wrap (question ))
150
- view += "\n \n "
151
- view += a .textarea .View ()
152
- view += "\n "
153
- view += a .styles .Comment .Render (fmt .Sprintf ("Question %d of %d: press ctrl+d to submit answer" , len (a .answers )+ 1 , len (a .Response .Questions )))
203
+ builder.WriteString( a.styles.QuestionTag.String() + " " + a.styles.Question.Render(a.wrap(question) ))
204
+ builder.WriteString( "\n\n")
205
+ builder.WriteString( a.textarea.View() )
206
+ builder.WriteString( "\n")
207
+ builder.WriteString( a.styles.Comment.Render(fmt.Sprintf("Question %d of %d: press ctrl+d to submit answer", len(a.answers)+1, len(a.Response.Questions) )))
154
208
}
155
209
}
156
- return lipgloss .Place (a .width , a .height , lipgloss .Left , lipgloss .Top , a .styles .App .Render (view ))
210
+ */
211
+
212
+ // Use String method to get the final string
213
+ view := builder .String ()
214
+ return lipgloss .Place (a .common .Width , a .common .Height , lipgloss .Left , lipgloss .Top , a .styles .App .Render (view ))
157
215
}
0 commit comments