Skip to content

Commit 912892e

Browse files
committed
refactor: extract stainlessviews
1 parent b1b82f6 commit 912892e

File tree

4 files changed

+151
-105
lines changed

4 files changed

+151
-105
lines changed

pkg/cmd/dev.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
tea "github.com/charmbracelet/bubbletea"
1818
"github.com/charmbracelet/huh"
1919
"github.com/stainless-api/stainless-api-cli/pkg/stainlessutils"
20+
"github.com/stainless-api/stainless-api-cli/pkg/stainlessviews"
2021
"github.com/stainless-api/stainless-api-go"
2122
"github.com/stainless-api/stainless-api-go/option"
2223
"github.com/tidwall/gjson"
@@ -34,11 +35,8 @@ type BuildModel struct {
3435
branch string
3536
help help.Model
3637
diagnostics []stainless.BuildDiagnostic
37-
downloads map[stainless.Target]struct {
38-
status string
39-
path string
40-
}
41-
view string
38+
downloads map[stainless.Target]stainlessviews.DownloadStatus
39+
view string
4240

4341
cc *apiCommandContext
4442
ctx context.Context
@@ -97,7 +95,7 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9795

9896
case downloadMsg:
9997
download := m.downloads[stainless.Target(msg)]
100-
download.status = "completed"
98+
download.Status = "completed"
10199
m.downloads[stainless.Target(msg)] = download
102100

103101
case tickMsg:
@@ -112,17 +110,11 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
112110
case fetchBuildMsg:
113111
if m.build == nil {
114112
m.build = msg
115-
m.downloads = make(map[stainless.Target]struct {
116-
status string
117-
path string
118-
})
113+
m.downloads = make(map[stainless.Target]stainlessviews.DownloadStatus)
119114
for targetName, targetConfig := range m.cc.workspaceConfig.Targets {
120-
m.downloads[stainless.Target(targetName)] = struct {
121-
status string
122-
path string
123-
}{
124-
status: "not started",
125-
path: targetConfig.OutputPath,
115+
m.downloads[stainless.Target(targetName)] = stainlessviews.DownloadStatus{
116+
Status: "not started",
117+
Path: targetConfig.OutputPath,
126118
}
127119
}
128120
cmds = append(cmds, m.updateView("header"))
@@ -153,8 +145,8 @@ func (m BuildModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
153145
}
154146
status, _, conclusion := buildTarget.StepInfo("commit")
155147
if status == "completed" && conclusion != "fatal" {
156-
if download, ok := m.downloads[target]; ok && download.status == "not started" {
157-
download.status = "started"
148+
if download, ok := m.downloads[target]; ok && download.Status == "not started" {
149+
download.Status = "started"
158150
cmds = append(cmds, m.downloadTarget(target))
159151
m.downloads[target] = download
160152
}
@@ -218,7 +210,7 @@ func (m BuildModel) downloadTarget(target stainless.Target) tea.Cmd {
218210
if err != nil {
219211
return errorMsg(err)
220212
}
221-
err = pullOutput(outputRes.Output, outputRes.URL, outputRes.Ref, m.downloads[target].path, &Group{silent: true})
213+
err = pullOutput(outputRes.Output, outputRes.URL, outputRes.Ref, m.downloads[target].Path, &Group{silent: true})
222214
if err != nil {
223215
return errorMsg(err)
224216
}

pkg/cmd/dev_view.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
tea "github.com/charmbracelet/bubbletea"
5+
"github.com/stainless-api/stainless-api-cli/pkg/stainlessviews"
6+
)
7+
8+
func (m BuildModel) View() string {
9+
data := stainlessviews.ViewData{
10+
Build: m.build,
11+
Diagnostics: m.diagnostics,
12+
Duration: m.getBuildDuration(),
13+
Branch: m.branch,
14+
Downloads: m.downloads,
15+
HelpText: m.help.View(m),
16+
}
17+
return stainlessviews.ViewBuild(data, m.view)
18+
}
19+
20+
// updateView updates the view state and prints everything from current state to target state to scrollback
21+
func (m *BuildModel) updateView(targetState string) tea.Cmd {
22+
data := stainlessviews.ViewData{
23+
Build: m.build,
24+
Diagnostics: m.diagnostics,
25+
Duration: m.getBuildDuration(),
26+
Branch: m.branch,
27+
Downloads: m.downloads,
28+
HelpText: m.help.View(*m),
29+
}
30+
31+
output := stainlessviews.ViewBuildRange(data, m.view, targetState)
32+
33+
// Update model state
34+
m.view = targetState
35+
36+
// Print to scrollback
37+
if len(output) > 0 {
38+
if output[len(output)-1] == '\n' {
39+
return tea.Println(output[:len(output)-1])
40+
} else {
41+
return tea.Println(output)
42+
}
43+
}
44+
return nil
45+
}

pkg/cmd/lint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/charmbracelet/bubbles/spinner"
1313
tea "github.com/charmbracelet/bubbletea"
1414
"github.com/charmbracelet/lipgloss"
15+
"github.com/stainless-api/stainless-api-cli/pkg/stainlessviews"
1516
"github.com/stainless-api/stainless-api-go"
1617
"github.com/urfave/cli/v3"
1718
)
@@ -132,7 +133,7 @@ func (m lintModel) View() string {
132133
content = m.spinner.View() + " Linting"
133134
}
134135
} else {
135-
content = ViewDiagnosticsPrint(m.diagnostics, -1)
136+
content = stainlessviews.ViewDiagnosticsPrint(m.diagnostics, -1)
136137
if m.skipped {
137138
content += "\nContinuing..."
138139
} else if m.watching {

0 commit comments

Comments
 (0)