feat: Add gum write command

Write provides a shell script interface for the text area bubble. 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.

```
gum write > output.text
```
This commit is contained in:
Maas Lalani 2022-07-06 14:38:40 -04:00
parent 25a12fbbc6
commit a0e2cda3cb
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000
5 changed files with 79 additions and 0 deletions

View file

@ -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 <command>":

11
pop.go
View file

@ -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.
//

31
write/command.go Normal file
View file

@ -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())
}

11
write/options.go Normal file
View file

@ -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"`
}

24
write/write.go Normal file
View file

@ -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
}