Skip to content

Commit

Permalink
feat: huh for gum input (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani authored Mar 28, 2024
1 parent 3a71710 commit 4a560b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 109 deletions.
63 changes: 31 additions & 32 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package input

import (
"fmt"
"os"

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

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

Expand All @@ -22,40 +20,41 @@ func (o Options) Run() error {
i.SetValue(in)
}

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
theme := huh.ThemeCharm()
theme.Focused.Base = lipgloss.NewStyle()
theme.Focused.TextInput.Cursor = o.CursorStyle.ToLipgloss()
theme.Focused.TextInput.Placeholder = o.PlaceholderStyle.ToLipgloss()
theme.Focused.TextInput.Prompt = o.PromptStyle.ToLipgloss()
theme.Focused.Title = o.HeaderStyle.ToLipgloss()

var value string
var echoMode huh.EchoMode

if o.Password {
i.EchoMode = textinput.EchoPassword
i.EchoCharacter = '•'
echoMode = huh.EchoModePassword
} else {
echoMode = huh.EchoModeNormal
}

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()
if err != nil {
return fmt.Errorf("failed to run input: %w", err)
}
m := tm.(model)
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Prompt(o.Prompt).
Placeholder(o.Placeholder).
CharLimit(o.CharLimit).
EchoMode(echoMode).
Title(o.Header).
Value(&value),
),
).
WithShowHelp(false).
WithTheme(theme).
Run()

if m.aborted {
return exit.ErrAborted
if err != nil {
return err

Check failure on line 55 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)
}

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

This file was deleted.

0 comments on commit 4a560b1

Please sign in to comment.