Skip to content

Commit

Permalink
Simplify and better design on the UI for substreams init. Thanks Ch…
Browse files Browse the repository at this point in the history
…arles!
  • Loading branch information
Alexandre Bourget committed Sep 19, 2024
1 parent 17154c1 commit 72b41a4
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions cmd/substreams/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ var initCmd = &cobra.Command{
SilenceUsage: true,
}

var huhTheme *huh.Theme

func init() {
defaultEndpoint := "https://codegen.substreams.dev"
if newValue := os.Getenv("SUBSTREAMS_INIT_CODEGEN_ENDPOINT"); newValue != "" {
Expand All @@ -57,6 +59,10 @@ func init() {
initCmd.Flags().String("state-file", "./generator.json", "File to load/save the state of the code generator")
initCmd.Flags().Bool("force-download-cwd", false, "Force download at current dir")
rootCmd.AddCommand(initCmd)

huhTheme = huh.ThemeCharm()
huhTheme.Focused.Base = huhTheme.Focused.Base.
BorderForeground(lipgloss.Color("15"))
}

var INIT_TRACE = false
Expand Down Expand Up @@ -148,7 +154,7 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
Title(fmt.Sprintf("State file %q was found (%s - %s). Do you want to start from there ?", stateFile, state.GeneratorID, humanize.Time(s.ModTime()))).
Value(&useGenerator)

if err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huh.ThemeCharm()).WithAccessible(WITH_ACCESSIBLE).Run(); err != nil {
if err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run(); err != nil {
return fmt.Errorf("failed taking confirmation input: %w", err)
}

Expand Down Expand Up @@ -194,19 +200,12 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
Options(options...).
Value(&codegen)

err = huh.NewForm(huh.NewGroup(selectField)).WithTheme(huh.ThemeCharm()).WithAccessible(WITH_ACCESSIBLE).Run()
err = huh.NewForm(huh.NewGroup(selectField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking input: %w", err)
}

fmt.Println("┃ ", bold("Selected code generator:"), codegen.Id, "-", codegen.Title)
for i, desc := range strings.Split(codegen.Description, "\n") {
dd := desc
if i == 0 {
dd = bold("Description: ") + desc
}
fmt.Println("┃ ", dd)
}
fmt.Println(gray("┃"), "Chosen code generator:", bold(codegen.Id), "-", codegen.Title)
lastState.GeneratorID = codegen.Id
generatorID = codegen.Id
}
Expand Down Expand Up @@ -314,19 +313,16 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
Options(options...).
Value(&selection)

err := huh.NewForm(huh.NewGroup(selectField)).WithTheme(huh.ThemeCharm()).WithAccessible(WITH_ACCESSIBLE).Run()
err := huh.NewForm(huh.NewGroup(selectField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking input: %w", err)
}

fmt.Println("┃ ", input.Instructions)
var selectedValue string
for _, opt := range options {
if opt.Value == selection {
fmt.Println("┃ -", bold(opt.Key))
} else {
fmt.Println("┃ -", opt.Key)
}
selectedValue = opt.Key
}
fmt.Println(gray("┃"), input.Instructions+":", bold(selectedValue))
fmt.Println("")

if err := sendFunc(&pbconvo.UserInput{
Expand Down Expand Up @@ -367,12 +363,12 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
})
}

err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huh.ThemeCharm()).WithAccessible(WITH_ACCESSIBLE).Run()
err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking input: %w", err)
}

fmt.Println("┃ ", input.Prompt+":", bold(returnValue))
fmt.Println(gray("┃"), input.Prompt+":", bold(returnValue))
fmt.Println("")

if err := sendFunc(&pbconvo.UserInput{
Expand All @@ -395,7 +391,7 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
Description(input.Description).
Value(&returnValue)

err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huh.ThemeCharm()).WithAccessible(WITH_ACCESSIBLE).Run()
err := huh.NewForm(huh.NewGroup(inputField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking confirmation input: %w", err)
}
Expand All @@ -407,9 +403,9 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
} else {
deny = bold(deny)
}
fmt.Println("┃ ", input.Prompt)
fmt.Println("┃ ")
fmt.Println("┃ " + affirm + " " + deny)
fmt.Println(gray("┃"), input.Prompt)
fmt.Println(gray("┃"))
fmt.Println(gray("┃"), affirm+" "+deny)

if err := sendFunc(&pbconvo.UserInput{
FromActionId: resp.ActionId,
Expand Down Expand Up @@ -640,6 +636,10 @@ func bold(input string) string {
return lipgloss.NewStyle().Bold(true).Render(input)
}

func gray(input string) string {
return lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Render(input)
}

func filenameStyle(input string) string {
return lipgloss.NewStyle().Foreground(lipgloss.Color("34")).Render(input)
}
Expand Down Expand Up @@ -731,7 +731,7 @@ func (f *OverwriteForm) createOverwriteForm(path string) error {
Options(options...).
Value(&selection)

err := huh.NewForm(huh.NewGroup(selectField)).WithAccessible(WITH_ACCESSIBLE).Run()
err := huh.NewForm(huh.NewGroup(selectField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
f.Overwrite = false
return fmt.Errorf("failed confirming: %w", err)
Expand Down

0 comments on commit 72b41a4

Please sign in to comment.