Skip to content

Commit 7451272

Browse files
committed
refactor: UI code
1 parent 187404c commit 7451272

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## What's Changed
2+
* refactor: UI code
3+
* fix: Idiomatic info message style
24
* docs: Add CHANGELOG
35
* fix: Idiomatic error message style by @Allaman
46

ui.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func formatToolString(name string, tool Tool) string {
3333
Foreground(theme.Blurred.MultiSelectSelector.GetForeground())
3434

3535
styledToolName := toolNameStyle.Render(name)
36+
// TODO: How to handle long descriptions?
3637
styledDescription := descriptionStyle.Render(tool.Description)
3738
styledCategories := categoriesStyle.Render(strings.Join(tool.Categories, ","))
3839

@@ -63,13 +64,7 @@ func createToolOptions(tools Tools) []huh.Option[string] {
6364
return options
6465
}
6566

66-
func startUI(cfg cliConfig) {
67-
tools, err := createDefaultTools()
68-
if err != nil {
69-
logger.Error("could not parse tools data", "error", err)
70-
os.Exit(1)
71-
}
72-
67+
func createForm(cfg cliConfig, tools Tools) *huh.Form {
7368
form := huh.NewForm(
7469
huh.NewGroup(
7570
huh.NewNote().
@@ -88,7 +83,6 @@ func startUI(cfg cliConfig) {
8883
}).
8984
Value(&workingDir),
9085
),
91-
9286
huh.NewGroup(
9387
huh.NewMultiSelect[string]().
9488
Title("Which tools do you want to install?").
@@ -104,41 +98,57 @@ func startUI(cfg cliConfig) {
10498
),
10599
).WithAccessible(cfg.accessible)
106100

107-
form.WithTheme(theme)
108-
109-
err = form.Run()
110-
111-
if err != nil {
112-
logger.Fatal(err)
113-
}
101+
return form
102+
}
114103

115-
installDir, err := normalizePath(workingDir)
116-
if err != nil {
117-
logger.Error("could not normalize path")
118-
os.Exit(1)
119-
}
104+
func process(tools Tools) func() {
105+
return func() {
106+
installDir, err := normalizePath(workingDir)
107+
if err != nil {
108+
logger.Error("could not normalize path")
109+
os.Exit(1)
110+
}
120111

121-
start := func() {
122112
config := newDefaultConfig()
123113
if os.Getenv("WK_EGET_VERSION") != "" {
124114
version := os.Getenv("WK_EGET_VERSION")
125115
logger.Debug("setting eget version", "version", version)
126116
config.version = version
127117
}
128-
err := downloadEgetBinary(installDir, config)
118+
err = downloadEgetBinary(installDir, config)
129119
if err != nil {
130120
logger.Error("could not download eget binary", "error", err)
131121
os.Exit(1)
132122
}
133123
for _, t := range selectedTools {
134124
err = downloadToolWithEget(installDir, tools.Tools[t])
135125
if err != nil {
136-
logger.Warn("could not download tool", "error", err)
126+
logger.Warn("could not download tool", "tool", t, "error", err)
137127
continue
138128
}
139129
}
130+
logger.Info(fmt.Sprintf("Run 'export PATH=$PATH:%s' to add your tools to the PATH", installDir))
131+
}
132+
}
133+
134+
func startUI(cfg cliConfig) {
135+
tools, err := createDefaultTools()
136+
if err != nil {
137+
logger.Error("could not parse tools data", "error", err)
138+
os.Exit(1)
140139
}
141140

141+
form := createForm(cfg, tools)
142+
143+
form.WithTheme(theme)
144+
145+
err = form.Run()
146+
147+
if err != nil {
148+
logger.Fatal(err)
149+
}
150+
151+
start := process(tools)
152+
142153
_ = spinner.New().Title("Downloading tools ...").Accessible(cfg.accessible).Action(start).Run()
143-
logger.Print(fmt.Sprintf("Run 'export PATH=$PATH:%s' to add your tools to the PATH", installDir))
144154
}

0 commit comments

Comments
 (0)