mirror of
https://github.com/charmbracelet/gum.git
synced 2024-08-17 02:00:46 +03: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)
|
||||
|
||||
if m.aborted {
|
||||
os.Exit(exit.Aborted)
|
||||
return exit.ErrAborted
|
||||
}
|
||||
if len(m.matches) > m.selected && m.selected >= 0 {
|
||||
fmt.Println(m.matches[m.selected].Str)
|
||||
|
@ -42,7 +42,7 @@ func (o Options) Run() error {
|
||||
m := tm.(model)
|
||||
|
||||
if m.aborted {
|
||||
os.Exit(exit.Aborted)
|
||||
return exit.ErrAborted
|
||||
}
|
||||
|
||||
fmt.Println(m.textinput.Value())
|
||||
|
@ -1,4 +1,9 @@
|
||||
package exit
|
||||
|
||||
// Aborted is the exit code for aborted commands.
|
||||
const Aborted = 130
|
||||
import "fmt"
|
||||
|
||||
// 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
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
"github.com/charmbracelet/gum/internal/exit"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
@ -56,6 +58,9 @@ func main() {
|
||||
},
|
||||
)
|
||||
if err := ctx.Run(); err != nil {
|
||||
if errors.Is(err, exit.ErrAborted) {
|
||||
os.Exit(exit.StatusAborted)
|
||||
}
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/alecthomas/kong"
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/gum/internal/exit"
|
||||
"github.com/charmbracelet/gum/style"
|
||||
)
|
||||
|
||||
@ -33,8 +34,11 @@ func (o Options) Run() error {
|
||||
fmt.Print(m.output)
|
||||
}
|
||||
|
||||
os.Exit(m.status)
|
||||
if m.aborted {
|
||||
return exit.ErrAborted
|
||||
}
|
||||
|
||||
os.Exit(m.status)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ type model struct {
|
||||
spinner spinner.Model
|
||||
title string
|
||||
command []string
|
||||
aborted bool
|
||||
|
||||
status int
|
||||
output string
|
||||
@ -76,6 +77,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c":
|
||||
m.aborted = true
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/alecthomas/kong"
|
||||
"github.com/charmbracelet/bubbles/textarea"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/gum/internal/exit"
|
||||
"github.com/charmbracelet/gum/internal/stdin"
|
||||
"github.com/charmbracelet/gum/style"
|
||||
)
|
||||
@ -45,8 +46,13 @@ func (o Options) Run() error {
|
||||
a.SetValue(o.Value)
|
||||
|
||||
p := tea.NewProgram(model{textarea: a}, tea.WithOutput(os.Stderr))
|
||||
m, err := p.StartReturningModel()
|
||||
fmt.Println(m.(model).textarea.Value())
|
||||
tm, err := p.StartReturningModel()
|
||||
m := tm.(model)
|
||||
if m.aborted {
|
||||
return exit.ErrAborted
|
||||
}
|
||||
|
||||
fmt.Println(m.textarea.Value())
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type model struct {
|
||||
aborted bool
|
||||
quitting bool
|
||||
textarea textarea.Model
|
||||
}
|
||||
@ -29,7 +30,10 @@ 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, tea.KeyCtrlD:
|
||||
case tea.KeyCtrlC:
|
||||
m.aborted = true
|
||||
fallthrough
|
||||
case tea.KeyEscape, tea.KeyCtrlD:
|
||||
m.quitting = true
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user