Skip to content

Commit 94ee15d

Browse files
author
Bruce Hill
committed
Use help widget
1 parent 85a3ea1 commit 94ee15d

File tree

3 files changed

+80
-78
lines changed

3 files changed

+80
-78
lines changed

pkg/cmd/dev.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313
"time"
1414

15+
"github.com/charmbracelet/bubbles/help"
16+
"github.com/charmbracelet/bubbles/key"
1517
tea "github.com/charmbracelet/bubbletea"
1618
"github.com/charmbracelet/huh"
1719
"github.com/stainless-api/stainless-api-go"
@@ -29,6 +31,7 @@ type BuildModel struct {
2931
ended *time.Time
3032
build *stainless.Build
3133
branch string
34+
help help.Model
3235
diagnostics []stainless.BuildDiagnostic
3336
downloads map[stainless.Target]struct {
3437
status string
@@ -56,6 +59,7 @@ func NewBuildModel(cc *apiCommandContext, ctx context.Context, branch string, fn
5659
cc: cc,
5760
ctx: ctx,
5861
branch: branch,
62+
help: help.New(),
5963
}
6064
}
6165

@@ -77,13 +81,17 @@ func (m BuildModel) Init() tea.Cmd {
7781
func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7882
cmds := []tea.Cmd{}
7983
switch msg := msg.(type) {
84+
case tea.WindowSizeMsg:
85+
m.help.Width = msg.Width
8086
case tea.KeyMsg:
8187
switch msg.String() {
8288
case "ctrl+c":
8389
m.err = ErrUserCancelled
8490
cmds = append(cmds, tea.Quit)
8591
case "enter":
86-
cmds = append(cmds, tea.Quit)
92+
if m.cc.cmd.Bool("watch") {
93+
cmds = append(cmds, tea.Quit)
94+
}
8795
}
8896

8997
case downloadMsg:
@@ -169,6 +177,28 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
169177
return m, tea.Sequence(cmds...)
170178
}
171179

180+
func (m BuildModel) ShortHelp() []key.Binding {
181+
if m.cc.cmd.Bool("watch") {
182+
return []key.Binding{
183+
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit")),
184+
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "rebuild")),
185+
}
186+
} else {
187+
return []key.Binding{key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit"))}
188+
}
189+
}
190+
191+
func (m BuildModel) FullHelp() [][]key.Binding {
192+
if m.cc.cmd.Bool("watch") {
193+
return [][]key.Binding{{
194+
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit")),
195+
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "rebuild")),
196+
}}
197+
} else {
198+
return [][]key.Binding{{key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit"))}}
199+
}
200+
}
201+
172202
func (m BuildModel) downloadTarget(target stainless.Target) tea.Cmd {
173203
return func() tea.Msg {
174204
if m.build == nil {

pkg/cmd/dev_view.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ var parts = []struct {
9898
view: func(m BuildModel, s *strings.Builder) {
9999
buildIDStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("0")).Background(lipgloss.Color("6")).Bold(true)
100100
if m.build != nil {
101-
s.WriteString(fmt.Sprintf("\n\n%s %s\n\n", buildIDStyle.Render(" BUILD "), m.build.ID))
101+
fmt.Fprintf(s, "\n\n%s %s\n\n", buildIDStyle.Render(" BUILD "), m.build.ID)
102102
} else {
103-
s.WriteString(fmt.Sprintf("\n\n%s\n\n", buildIDStyle.Render(" BUILD ")))
103+
fmt.Fprintf(s, "\n\n%s\n\n", buildIDStyle.Render(" BUILD "))
104104
}
105105
},
106106
},
@@ -169,8 +169,7 @@ var parts = []struct {
169169
{
170170
name: "help",
171171
view: func(m BuildModel, s *strings.Builder) {
172-
s.WriteString("\n")
173-
s.WriteString(ViewHelpMenu())
172+
s.WriteString(m.help.View(m))
174173
},
175174
},
176175
}
@@ -257,42 +256,6 @@ func ViewDiagnosticIcon(level stainless.BuildDiagnosticLevel) string {
257256
}
258257
}
259258

260-
// ViewHelpMenu creates a styled help menu inspired by huh help component
261-
func ViewHelpMenu() string {
262-
keyStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
263-
Light: "#909090",
264-
Dark: "#626262",
265-
})
266-
267-
descStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
268-
Light: "#B2B2B2",
269-
Dark: "#4A4A4A",
270-
})
271-
272-
sepStyle := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
273-
Light: "#DDDADA",
274-
Dark: "#3C3C3C",
275-
})
276-
277-
helpItems := []struct {
278-
key string
279-
desc string
280-
}{
281-
{"enter", "rebuild"},
282-
{"ctrl+c", "exit"},
283-
}
284-
285-
var parts []string
286-
for _, item := range helpItems {
287-
parts = append(parts,
288-
keyStyle.Render(item.key)+
289-
sepStyle.Render(" ")+
290-
descStyle.Render(item.desc))
291-
}
292-
293-
return strings.Join(parts, sepStyle.Render(" • "))
294-
}
295-
296259
// renderMarkdown renders markdown content using glamour
297260
func renderMarkdown(content string) string {
298261
width, _, err := term.GetSize(uintptr(os.Stdout.Fd()))

pkg/cmd/lint.go

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"time"
99

10+
"github.com/charmbracelet/bubbles/help"
11+
"github.com/charmbracelet/bubbles/key"
1012
"github.com/charmbracelet/bubbles/spinner"
1113
tea "github.com/charmbracelet/bubbletea"
1214
"github.com/charmbracelet/lipgloss"
@@ -49,26 +51,17 @@ var lintCommand = cli.Command{
4951
},
5052
}
5153

52-
var helpStyle = lipgloss.NewStyle().
53-
Foreground(lipgloss.Color("241")).
54-
Margin(1, 0, 0, 0)
55-
5654
type lintModel struct {
57-
spinner spinner.Model
58-
diagnostics []stainless.BuildDiagnostic
59-
error error
60-
watching bool
61-
stopWhenPassing bool
62-
ctx context.Context
63-
cmd *cli.Command
64-
cc *apiCommandContext
65-
stopPolling chan struct{}
66-
help helpModel
67-
}
68-
69-
type helpModel struct {
70-
width int
71-
height int
55+
spinner spinner.Model
56+
diagnostics []stainless.BuildDiagnostic
57+
error error
58+
watching bool
59+
canSkip bool
60+
ctx context.Context
61+
cmd *cli.Command
62+
cc *apiCommandContext
63+
stopPolling chan struct{}
64+
help help.Model
7265
}
7366

7467
func (m lintModel) Init() tea.Cmd {
@@ -94,7 +87,7 @@ func (m lintModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9487
m.cmd = msg.cmd
9588
m.cc = msg.cc
9689

97-
if m.stopWhenPassing && !hasBlockingDiagnostic(m.diagnostics) {
90+
if m.canSkip && !hasBlockingDiagnostic(m.diagnostics) {
9891
m.watching = false
9992
return m, tea.Quit
10093
}
@@ -119,8 +112,7 @@ func (m lintModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
119112
return m, cmd
120113

121114
case tea.WindowSizeMsg:
122-
m.help.width = msg.Width
123-
m.help.height = msg.Height
115+
m.help.Width = msg.Width
124116
return m, nil
125117
}
126118

@@ -142,12 +134,7 @@ func (m lintModel) View() string {
142134
}
143135
}
144136

145-
if m.stopWhenPassing {
146-
content += helpStyle.Render("Press Enter to skip diagnostics, Ctrl+C to exit")
147-
} else {
148-
content += helpStyle.Render("Press Ctrl+C to exit")
149-
}
150-
137+
content += "\n" + m.help.View(m)
151138
return content
152139
}
153140

@@ -172,22 +159,44 @@ func getDiagnosticsCmd(ctx context.Context, cmd *cli.Command, cc *apiCommandCont
172159
}
173160
}
174161

175-
func runLinter(ctx context.Context, cmd *cli.Command, stopWhenPassing bool) error {
162+
func (m lintModel) ShortHelp() []key.Binding {
163+
if m.canSkip {
164+
return []key.Binding{
165+
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit")),
166+
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "skip diagnostics")),
167+
}
168+
} else {
169+
return []key.Binding{key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit"))}
170+
}
171+
}
172+
173+
func (m lintModel) FullHelp() [][]key.Binding {
174+
if m.canSkip {
175+
return [][]key.Binding{{
176+
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit")),
177+
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "skip diagnostics")),
178+
}}
179+
} else {
180+
return [][]key.Binding{{key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl-c", "quit"))}}
181+
}
182+
}
183+
184+
func runLinter(ctx context.Context, cmd *cli.Command, canSkip bool) error {
176185
cc := getAPICommandContext(cmd)
177186

178187
s := spinner.New()
179188
s.Spinner = spinner.MiniDot
180189
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("208"))
181190

182191
m := lintModel{
183-
spinner: s,
184-
watching: cmd.Bool("watch"),
185-
stopWhenPassing: stopWhenPassing,
186-
ctx: ctx,
187-
cmd: cmd,
188-
cc: cc,
189-
stopPolling: make(chan struct{}),
190-
help: helpModel{},
192+
spinner: s,
193+
watching: cmd.Bool("watch"),
194+
canSkip: canSkip,
195+
ctx: ctx,
196+
cmd: cmd,
197+
cc: cc,
198+
stopPolling: make(chan struct{}),
199+
help: help.New(),
191200
}
192201

193202
p := tea.NewProgram(m, tea.WithContext(ctx))

0 commit comments

Comments
 (0)