Skip to content

Commit bfa6c00

Browse files
author
tartavull
committed
fix(talk): fix rendering of tabs.
1 parent ad0e950 commit bfa6c00

File tree

5 files changed

+125
-47
lines changed

5 files changed

+125
-47
lines changed

talk/auto/auto.go

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package auto
22

33
import (
4-
"fmt"
54
"log"
6-
"io/ioutil"
75
"strings"
6+
"os"
87

98
tea "github.com/charmbracelet/bubbletea"
109
"github.com/charmbracelet/bubbles/textarea"
11-
"github.com/charmbracelet/bubbles/cursor"
1210
"github.com/mitchellh/go-wordwrap"
1311
"github.com/charmbracelet/bubbles/viewport"
1412
"github.com/charmbracelet/lipgloss"
@@ -26,9 +24,11 @@ const (
2624
)
2725

2826
type Auto struct {
29-
common common.Common
27+
common *common.Common
3028
styles common.Styles
29+
picked pane
3130
tabs *tabs.Tabs
31+
panes []tea.Model
3232

3333
textarea textarea.Model
3434
state State
@@ -41,21 +41,26 @@ type Auto struct {
4141
height int
4242
}
4343

44-
func New(c common.Common, s common.Styles) *Auto {
44+
func New(c *common.Common, s common.Styles) *Auto {
4545
a := &Auto{
4646
common: c,
4747
textarea: textarea.New(),
4848
styles: s,
49-
tabs: tabs.New(c, []string{"chat", "history"}),
49+
picked: paneChat,
5050
viewport: viewport.New(0, 0),
51+
tabs: InitTabs(c),
52+
panes: []tea.Model{
53+
nil,
54+
nil,
55+
},
5156
}
5257
a.viewport.YPosition = 0
5358

5459
a.textarea.Placeholder = ">"
5560
a.textarea.ShowLineNumbers = false
5661
a.textarea.Focus()
5762

58-
data, err := ioutil.ReadFile("chat/llm_1.json")
63+
data, err := os.ReadFile("chat/llm_1.json")
5964
if err != nil {
6065
log.Fatal(err)
6166
}
@@ -67,20 +72,65 @@ func New(c common.Common, s common.Styles) *Auto {
6772
return a
6873
}
6974

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+
70103
func (a *Auto) Focus() tea.Cmd {
71104
return textarea.Blink
72105
}
73106

74107
func (a *Auto) SetSize(width, height int) {
75-
a.width = width
76-
a.height = height
108+
a.common.SetSize(width, height)
77109
a.textarea.SetWidth(width)
78110
a.textarea.MaxHeight = height / 2
79111
a.viewport.Width = width
80112
a.viewport.Height = height
81113
}
82114

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+
}
83132

133+
/*
84134
func (a *Auto) Update(msg tea.Msg) (*Auto, tea.Cmd) {
85135
var cmd tea.Cmd
86136
cmds := make([]tea.Cmd, 0)
@@ -120,38 +170,46 @@ func (a *Auto) Update(msg tea.Msg) (*Auto, tea.Cmd) {
120170
}
121171
return a, tea.Batch(cmds...)
122172
}
173+
*/
123174

124175
func (a *Auto) wrap(in string) string {
125176
return wordwrap.WrapString(in, uint(a.textarea.Width()/2))
126177
}
127178

128179
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+
138192
if a.state == stateAnswering {
139193
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"))
147201
} else if len(a.answers) < len(a.Response.Questions) {
148202
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))))
154208
}
155209
}
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))
157215
}

talk/common/styles.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ type Styles struct {
6060
TabInactive Style
6161
TabActive Style
6262
TabSeparator Style
63+
64+
TopLevelNormalTab Style
65+
TopLevelActiveTab Style
66+
TopLevelActiveTabDot Style
6367
}
6468

6569
func MakeStyles(r *Renderer) (s Styles) {
@@ -107,5 +111,16 @@ func MakeStyles(r *Renderer) (s Styles) {
107111
SetString("│").
108112
Padding(0, 1).
109113
Foreground(Color("238"))
114+
115+
116+
s.TopLevelNormalTab = NewStyle().
117+
MarginRight(2)
118+
119+
s.TopLevelActiveTab = s.TopLevelNormalTab.Copy().
120+
Foreground(Color("36"))
121+
122+
s.TopLevelActiveTabDot = NewStyle().
123+
Foreground(Color("36"))
124+
110125
return s
111126
}

talk/components/chat/chat.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package chat
2+
3+
type Chat struct {
4+
}
5+
6+
func New() *Chat {
7+
return &Chat{}
8+
}

talk/components/tabs/tabs.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ActiveTabMsg int
1616

1717
// Tabs is bubbletea component that displays a list of tabs.
1818
type Tabs struct {
19-
common common.Common
19+
common *common.Common
2020
tabs []string
2121
activeTab int
2222
TabSeparator lipgloss.Style
@@ -27,7 +27,7 @@ type Tabs struct {
2727
}
2828

2929
// New creates a new Tabs component.
30-
func New(c common.Common, tabs []string) *Tabs {
30+
func New(c *common.Common, tabs []string) *Tabs {
3131
r := &Tabs{
3232
common: c,
3333
tabs: tabs,
@@ -86,28 +86,25 @@ func (t *Tabs) View() string {
8686
s := strings.Builder{}
8787
sep := t.TabSeparator
8888
for i, tab := range t.tabs {
89-
style := t.TabInactive.Copy()
90-
prefix := " "
89+
style := t.TabInactive
90+
prefix := " "
9191
if i == t.activeTab {
92-
style = t.TabActive.Copy()
93-
prefix = t.TabDot.Render("• ")
92+
style = t.TabActive
93+
prefix = t.TabDot.Render(" • ")
9494
}
9595
if t.UseDot {
9696
s.WriteString(prefix)
9797
}
98-
s.WriteString(
99-
t.common.Zone.Mark(
100-
tab,
101-
style.Render(tab),
102-
),
103-
)
98+
s.WriteString(style.Render(tab))
10499
if i != len(t.tabs)-1 {
105100
s.WriteString(sep.String())
106101
}
107102
}
103+
104+
out := s.String()
108105
return lipgloss.NewStyle().
109106
MaxWidth(t.common.Width).
110-
Render(s.String())
107+
Render(out)
111108
}
112109

113110
func (t *Tabs) activeTabCmd() tea.Msg {

talk/mods.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func newMods(r *lipgloss.Renderer) *Mods {
5656
state: stateStart,
5757
renderer: r,
5858
styles: s,
59-
auto: auto.New(c, s),
59+
auto: auto.New(&c, s),
6060
}
6161

6262
}
@@ -121,9 +121,9 @@ func (m *Mods) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
121121
}
122122
}
123123
if m.Config.Auto {
124-
var cmd tea.Cmd
125124
m.state = stateAuto
126-
m.auto, cmd = m.auto.Update(msg)
125+
model, cmd := m.auto.Update(msg)
126+
m.auto = model.(*auto.Auto)
127127
return m, cmd
128128
}
129129
if m.state == stateConfigLoaded || m.state == stateCompletion {

0 commit comments

Comments
 (0)