Skip to content

Commit

Permalink
refactor: UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed Jun 30, 2024
1 parent 187404c commit 7451272
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## What's Changed
* refactor: UI code
* fix: Idiomatic info message style
* docs: Add CHANGELOG
* fix: Idiomatic error message style by @Allaman

Expand Down
58 changes: 34 additions & 24 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func formatToolString(name string, tool Tool) string {
Foreground(theme.Blurred.MultiSelectSelector.GetForeground())

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

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

func startUI(cfg cliConfig) {
tools, err := createDefaultTools()
if err != nil {
logger.Error("could not parse tools data", "error", err)
os.Exit(1)
}

func createForm(cfg cliConfig, tools Tools) *huh.Form {
form := huh.NewForm(
huh.NewGroup(
huh.NewNote().
Expand All @@ -88,7 +83,6 @@ func startUI(cfg cliConfig) {
}).
Value(&workingDir),
),

huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Which tools do you want to install?").
Expand All @@ -104,41 +98,57 @@ func startUI(cfg cliConfig) {
),
).WithAccessible(cfg.accessible)

form.WithTheme(theme)

err = form.Run()

if err != nil {
logger.Fatal(err)
}
return form
}

installDir, err := normalizePath(workingDir)
if err != nil {
logger.Error("could not normalize path")
os.Exit(1)
}
func process(tools Tools) func() {
return func() {
installDir, err := normalizePath(workingDir)
if err != nil {
logger.Error("could not normalize path")
os.Exit(1)
}

start := func() {
config := newDefaultConfig()
if os.Getenv("WK_EGET_VERSION") != "" {
version := os.Getenv("WK_EGET_VERSION")
logger.Debug("setting eget version", "version", version)
config.version = version
}
err := downloadEgetBinary(installDir, config)
err = downloadEgetBinary(installDir, config)
if err != nil {
logger.Error("could not download eget binary", "error", err)
os.Exit(1)
}
for _, t := range selectedTools {
err = downloadToolWithEget(installDir, tools.Tools[t])
if err != nil {
logger.Warn("could not download tool", "error", err)
logger.Warn("could not download tool", "tool", t, "error", err)
continue
}
}
logger.Info(fmt.Sprintf("Run 'export PATH=$PATH:%s' to add your tools to the PATH", installDir))
}
}

func startUI(cfg cliConfig) {
tools, err := createDefaultTools()
if err != nil {
logger.Error("could not parse tools data", "error", err)
os.Exit(1)
}

form := createForm(cfg, tools)

form.WithTheme(theme)

err = form.Run()

if err != nil {
logger.Fatal(err)
}

start := process(tools)

_ = spinner.New().Title("Downloading tools ...").Accessible(cfg.accessible).Action(start).Run()
logger.Print(fmt.Sprintf("Run 'export PATH=$PATH:%s' to add your tools to the PATH", installDir))
}

0 comments on commit 7451272

Please sign in to comment.