mirror of
https://github.com/charmbracelet/gum
synced 2024-05-25 09:52:24 +02:00
refactor: bubble up ErrAborted
This commit is contained in:
parent
0baa0b98bd
commit
b670c6d674
|
@ -46,7 +46,7 @@ func (o Options) Run() error {
|
||||||
m := tm.(model)
|
m := tm.(model)
|
||||||
|
|
||||||
if m.aborted {
|
if m.aborted {
|
||||||
os.Exit(exit.Aborted)
|
return exit.ErrAborted
|
||||||
}
|
}
|
||||||
if len(m.matches) > m.selected && m.selected >= 0 {
|
if len(m.matches) > m.selected && m.selected >= 0 {
|
||||||
fmt.Println(m.matches[m.selected].Str)
|
fmt.Println(m.matches[m.selected].Str)
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (o Options) Run() error {
|
||||||
m := tm.(model)
|
m := tm.(model)
|
||||||
|
|
||||||
if m.aborted {
|
if m.aborted {
|
||||||
os.Exit(exit.Aborted)
|
return exit.ErrAborted
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(m.textinput.Value())
|
fmt.Println(m.textinput.Value())
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
package exit
|
package exit
|
||||||
|
|
||||||
// Aborted is the exit code for aborted commands.
|
import "fmt"
|
||||||
const Aborted = 130
|
|
||||||
|
// StatusAborted is the exit code for aborted commands.
|
||||||
|
const StatusAborted = 130
|
||||||
|
|
||||||
|
// ErrAborted is the error to return when a gum command is aborted by Ctrl + C.
|
||||||
|
var ErrAborted = fmt.Errorf("aborted")
|
||||||
|
|
5
main.go
5
main.go
|
@ -1,11 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
|
"github.com/charmbracelet/gum/internal/exit"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
@ -56,6 +58,9 @@ func main() {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err := ctx.Run(); err != nil {
|
if err := ctx.Run(); err != nil {
|
||||||
|
if errors.Is(err, exit.ErrAborted) {
|
||||||
|
os.Exit(exit.StatusAborted)
|
||||||
|
}
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
"github.com/charmbracelet/bubbles/spinner"
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/gum/internal/exit"
|
||||||
"github.com/charmbracelet/gum/style"
|
"github.com/charmbracelet/gum/style"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,8 +34,11 @@ func (o Options) Run() error {
|
||||||
fmt.Print(m.output)
|
fmt.Print(m.output)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(m.status)
|
if m.aborted {
|
||||||
|
return exit.ErrAborted
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(m.status)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ type model struct {
|
||||||
spinner spinner.Model
|
spinner spinner.Model
|
||||||
title string
|
title string
|
||||||
command []string
|
command []string
|
||||||
|
aborted bool
|
||||||
|
|
||||||
status int
|
status int
|
||||||
output string
|
output string
|
||||||
|
@ -76,6 +77,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c":
|
case "ctrl+c":
|
||||||
|
m.aborted = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
"github.com/charmbracelet/bubbles/textarea"
|
"github.com/charmbracelet/bubbles/textarea"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/gum/internal/exit"
|
||||||
"github.com/charmbracelet/gum/internal/stdin"
|
"github.com/charmbracelet/gum/internal/stdin"
|
||||||
"github.com/charmbracelet/gum/style"
|
"github.com/charmbracelet/gum/style"
|
||||||
)
|
)
|
||||||
|
@ -45,8 +46,13 @@ func (o Options) Run() error {
|
||||||
a.SetValue(o.Value)
|
a.SetValue(o.Value)
|
||||||
|
|
||||||
p := tea.NewProgram(model{textarea: a}, tea.WithOutput(os.Stderr))
|
p := tea.NewProgram(model{textarea: a}, tea.WithOutput(os.Stderr))
|
||||||
m, err := p.StartReturningModel()
|
tm, err := p.StartReturningModel()
|
||||||
fmt.Println(m.(model).textarea.Value())
|
m := tm.(model)
|
||||||
|
if m.aborted {
|
||||||
|
return exit.ErrAborted
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(m.textarea.Value())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
|
aborted bool
|
||||||
quitting bool
|
quitting bool
|
||||||
textarea textarea.Model
|
textarea textarea.Model
|
||||||
}
|
}
|
||||||
|
@ -29,7 +30,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case tea.KeyEscape, tea.KeyCtrlC, tea.KeyCtrlD:
|
case tea.KeyCtrlC:
|
||||||
|
m.aborted = true
|
||||||
|
fallthrough
|
||||||
|
case tea.KeyEscape, tea.KeyCtrlD:
|
||||||
m.quitting = true
|
m.quitting = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue