diff --git a/main.go b/main.go index 7e007bd..d210726 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,8 @@ func main() { switch ctx.Command() { case "input": pop.Input.Run() + case "write": + pop.Write.Run() case "search": pop.Search.Run() case "spin ": diff --git a/pop.go b/pop.go index c556cd4..02a9592 100644 --- a/pop.go +++ b/pop.go @@ -5,6 +5,7 @@ import ( "github.com/charmbracelet/sodapop/search" "github.com/charmbracelet/sodapop/spin" "github.com/charmbracelet/sodapop/style" + "github.com/charmbracelet/sodapop/write" ) // Pop is the command-line interface for Soda Pop. @@ -19,6 +20,16 @@ type Pop struct { // Input input.Options `cmd:"" help:"Prompt for input."` + // Write provides a shell script interface for the text area bubble. + // https://github.com/charmbracelet/bubbles/textarea + // + // It can be used to ask the user to write some long form of text + // (multi-line) input. The text the user entered will be sent to stdout. + // + // $ pop write > output.text + // + Write write.Options `cmd:"" help:"Prompt for text"` + // Search provides a fuzzy searching text input to allow filtering a list of // options to select one option. // diff --git a/write/command.go b/write/command.go new file mode 100644 index 0000000..1658f18 --- /dev/null +++ b/write/command.go @@ -0,0 +1,31 @@ +package write + +import ( + "fmt" + "os" + + "github.com/charmbracelet/bubbles/textarea" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +// Run provides a shell script interface for the text area bubble. +// https://github.com/charmbracelet/bubbles/textarea +func (o Options) Run() { + a := textarea.New() + a.Focus() + + a.Prompt = o.Prompt + a.Placeholder = o.Placeholder + a.ShowLineNumbers = o.ShowLineNumbers + if !o.ShowCursorLine { + a.FocusedStyle.CursorLine = lipgloss.NewStyle() + a.BlurredStyle.CursorLine = lipgloss.NewStyle() + } + + a.SetWidth(o.Width) + + p := tea.NewProgram(model{a}, tea.WithOutput(os.Stderr)) + m, _ := p.StartReturningModel() + fmt.Println(m.(model).textarea.Value()) +} diff --git a/write/options.go b/write/options.go new file mode 100644 index 0000000..9cf71fb --- /dev/null +++ b/write/options.go @@ -0,0 +1,11 @@ +package write + +// Options are the customization options for the textarea. +type Options struct { + ShowCursorLine bool `help:"Show cursor line" default:"true"` + ShowLineNumbers bool `help:"Show line numbers" default:"true"` + Placeholder string `help:"Placeholder value" default:"Write something..."` + Prompt string `help:"Prompt to display" default:"┃ "` + Width int `help:"Text area width" default:"50"` + Height int `help:"Text area height" default:"5"` +} diff --git a/write/write.go b/write/write.go new file mode 100644 index 0000000..b8ab0c0 --- /dev/null +++ b/write/write.go @@ -0,0 +1,24 @@ +package write + +import ( + "github.com/charmbracelet/bubbles/textarea" + tea "github.com/charmbracelet/bubbletea" +) + +type model struct{ textarea textarea.Model } + +func (m model) Init() tea.Cmd { return textarea.Blink } +func (m model) View() string { return m.textarea.View() } +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.Type { + case tea.KeyEscape, tea.KeyCtrlC: + return m, tea.Quit + } + } + + var cmd tea.Cmd + m.textarea, cmd = m.textarea.Update(msg) + return m, cmd +}