mirror of
https://github.com/charmbracelet/gum
synced 2024-06-06 15:52:16 +02:00
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:
parent
25a12fbbc6
commit
a0e2cda3cb
2
main.go
2
main.go
|
@ -21,6 +21,8 @@ func main() {
|
||||||
switch ctx.Command() {
|
switch ctx.Command() {
|
||||||
case "input":
|
case "input":
|
||||||
pop.Input.Run()
|
pop.Input.Run()
|
||||||
|
case "write":
|
||||||
|
pop.Write.Run()
|
||||||
case "search":
|
case "search":
|
||||||
pop.Search.Run()
|
pop.Search.Run()
|
||||||
case "spin <command>":
|
case "spin <command>":
|
||||||
|
|
11
pop.go
11
pop.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/charmbracelet/sodapop/search"
|
"github.com/charmbracelet/sodapop/search"
|
||||||
"github.com/charmbracelet/sodapop/spin"
|
"github.com/charmbracelet/sodapop/spin"
|
||||||
"github.com/charmbracelet/sodapop/style"
|
"github.com/charmbracelet/sodapop/style"
|
||||||
|
"github.com/charmbracelet/sodapop/write"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pop is the command-line interface for Soda Pop.
|
// Pop is the command-line interface for Soda Pop.
|
||||||
|
@ -19,6 +20,16 @@ type Pop struct {
|
||||||
//
|
//
|
||||||
Input input.Options `cmd:"" help:"Prompt for input."`
|
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
|
// Search provides a fuzzy searching text input to allow filtering a list of
|
||||||
// options to select one option.
|
// options to select one option.
|
||||||
//
|
//
|
||||||
|
|
31
write/command.go
Normal file
31
write/command.go
Normal 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
11
write/options.go
Normal 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
24
write/write.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue