Skip to content

Commit

Permalink
feat: use huh for gum input
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 25, 2024
1 parent 1084bd4 commit 7bc85c9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 122 deletions.
71 changes: 29 additions & 42 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,47 @@ package input

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"

"github.com/charmbracelet/gum/cursor"
"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/internal/stdin"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)

// Run provides a shell script interface for the text input bubble.
// https://github.com/charmbracelet/bubbles/textinput
func (o Options) Run() error {
i := textinput.New()
if o.Value != "" {
i.SetValue(o.Value)
} else if in, _ := stdin.Read(); in != "" {
i.SetValue(in)
if o.Value == "" {
o.Value, _ = stdin.Read()
}

i.Focus()
i.Prompt = o.Prompt
i.Placeholder = o.Placeholder
i.Width = o.Width
i.PromptStyle = o.PromptStyle.ToLipgloss()
i.PlaceholderStyle = o.PlaceholderStyle.ToLipgloss()
i.Cursor.Style = o.CursorStyle.ToLipgloss()
i.Cursor.SetMode(cursor.Modes[o.CursorMode])
i.CharLimit = o.CharLimit

if o.Password {
i.EchoMode = textinput.EchoPassword
i.EchoCharacter = '•'
}

p := tea.NewProgram(model{
textinput: i,
aborted: false,
header: o.Header,
headerStyle: o.HeaderStyle.ToLipgloss(),
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
autoWidth: o.Width < 1,
}, tea.WithOutput(os.Stderr))
tm, err := p.Run()
theme := huh.ThemeCharm()
theme.Focused.Base.Border(lipgloss.Border{})
theme.Focused.Title = o.HeaderStyle.ToLipgloss()
theme.Focused.TextInput.Prompt = o.PromptStyle.ToLipgloss()
theme.Focused.TextInput.Placeholder = o.PlaceholderStyle.ToLipgloss()
theme.Focused.TextInput.Cursor = o.CursorStyle.ToLipgloss()

err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Password(o.Password).
Title(o.Header).
Prompt(o.Prompt).
CharLimit(o.CharLimit).
Placeholder(o.Placeholder).
Value(&o.Value),
),
).
WithWidth(o.Width).
WithShowHelp(false).
WithTheme(theme).
Run()
if err != nil {
return fmt.Errorf("failed to run input: %w", err)
return err

Check failure on line 41 in input/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func (*github.com/charmbracelet/huh.Form).Run() error (wrapcheck)

Check failure on line 41 in input/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func (*github.com/charmbracelet/huh.Form).Run() error (wrapcheck)
}
m := tm.(model)

if m.aborted {
return exit.ErrAborted
if o.Value != "" {
fmt.Println(o.Value)
}

fmt.Println(m.textinput.Value())
return nil
}
77 changes: 0 additions & 77 deletions input/input.go

This file was deleted.

6 changes: 3 additions & 3 deletions input/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
type Options struct {
Placeholder string `help:"Placeholder value" default:"Type something..." env:"GUM_INPUT_PLACEHOLDER"`
Prompt string `help:"Prompt to display" default:"> " env:"GUM_INPUT_PROMPT"`
PromptStyle style.Styles `embed:"" prefix:"prompt." envprefix:"GUM_INPUT_PROMPT_"`
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=#F780E2" envprefix:"GUM_INPUT_PROMPT_"`
PlaceholderStyle style.Styles `embed:"" prefix:"placeholder." set:"defaultForeground=240" envprefix:"GUM_INPUT_PLACEHOLDER_"`
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_INPUT_CURSOR_"`
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=#02BF87" envprefix:"GUM_INPUT_CURSOR_"`
CursorMode string `prefix:"cursor." name:"mode" help:"Cursor mode" default:"blink" enum:"blink,hide,static" env:"GUM_INPUT_CURSOR_MODE"`
Value string `help:"Initial value (can also be passed via stdin)" default:""`
CharLimit int `help:"Maximum value length (0 for no limit)" default:"400"`
Width int `help:"Input width (0 for terminal width)" default:"40" env:"GUM_INPUT_WIDTH"`
Password bool `help:"Mask input characters" default:"false"`
Header string `help:"Header value" default:"" env:"GUM_INPUT_HEADER"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_INPUT_HEADER_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=#7571F9" envprefix:"GUM_INPUT_HEADER_"`
Timeout time.Duration `help:"Timeout until input aborts" default:"0" env:"GUM_INPUT_TIMEOUT"`
}

0 comments on commit 7bc85c9

Please sign in to comment.