Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle signals in Getline. #20

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func main() {
if errors.Is(err, bubbline.ErrInterrupted) {
// Entered Ctrl+C to cancel input.
fmt.Println("^C")
} else if errors.Is(err, bubbline.ErrTerminated) {
fmt.Println("terminated")
break
} else {
fmt.Println("error:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/complete-choice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ of a name. Or just maybe one letter of the alphabet.`)

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/complete-rewrite-word/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ It's case-insensitive!`)

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/complete-simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You can also press tab with the cursor in the middle of a word!`)

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/detect-end-of-input/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ after a semicolon (;).`)

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/editline-history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/editline-simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {

for {
m.Reset()
if err := tea.NewProgram(m).Start(); err != nil {
if _, err := tea.NewProgram(m).Run(); err != nil {
log.Fatal(err)
}

Expand Down
3 changes: 3 additions & 0 deletions examples/getline-history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func main() {
if errors.Is(err, bubbline.ErrInterrupted) {
// Entered Ctrl+C to cancel input.
fmt.Println("^C")
} else if errors.Is(err, bubbline.ErrTerminated) {
fmt.Println("terminated")
break
} else {
fmt.Println("error:", err)
}
Expand Down
3 changes: 3 additions & 0 deletions examples/getline-simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func main() {
if errors.Is(err, bubbline.ErrInterrupted) {
// Entered Ctrl+C to cancel input.
fmt.Println("^C")
} else if errors.Is(err, bubbline.ErrTerminated) {
fmt.Println("terminated")
break
} else {
fmt.Println("error:", err)
}
Expand Down
37 changes: 34 additions & 3 deletions getline.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package bubbline

import (
"context"
"errors"
"os"
"os/signal"

tea "github.com/charmbracelet/bubbletea"
"github.com/knz/bubbline/complete"
Expand Down Expand Up @@ -40,14 +43,42 @@ func (m *Editor) Update(imsg tea.Msg) (tea.Model, tea.Cmd) {
func (m *Editor) Close() {}

// ErrInterrupted is returned when the input was interrupted with
// e.g. Ctrl+C.
// e.g. Ctrl+C, or when SIGINT was received.
var ErrInterrupted = editline.ErrInterrupted

// ErrTerminated is returned when the input was interrupted
// by receiving SIGTERM.
var ErrTerminated = errors.New("terminated")

// Getline runs the editor and returns the line that was read.
func (m *Editor) GetLine() (string, error) {
p := tea.NewProgram(m)
// We don't like the default handling of SIGINT/SIGTERM. Provide our own.
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
signal.Notify(ch, stopSignals...)
defer signal.Stop(ch)
var sig os.Signal
go func() {
select {
case sig = <-ch:
cancel()
case <-ctx.Done():
}
}()
// Create a Bubbletea program to handle our input.
p := tea.NewProgram(m, tea.WithoutSignalHandler(), tea.WithContext(ctx))
m.Reset()
if err := p.Start(); err != nil {
if _, err := p.Run(); err != nil {
// Was a signal received?
if ctx.Err() != nil {
// Yes: choose the resulting error depending on which signal was
// received.
if sig == os.Interrupt {
err = ErrInterrupted
} else {
err = ErrTerminated
}
}
return "", err
}
return m.Value(), m.Err
Expand Down
12 changes: 12 additions & 0 deletions getline_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !windows
// +build !windows

package bubbline

import (
"os"

"golang.org/x/sys/unix"
)

var stopSignals = []os.Signal{unix.SIGINT, unix.SIGTERM}
8 changes: 8 additions & 0 deletions getline_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows
// +build windows

package bubbline

import "os"

var stopSignals = []os.Signal{os.Interrupt}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.13
require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbles v0.14.1-0.20221007152719-9a48dca00354
github.com/charmbracelet/bubbletea v0.22.2-0.20221007125427-0e76ba142aa1
github.com/charmbracelet/bubbletea v0.23.1
github.com/charmbracelet/lipgloss v0.6.0
github.com/cockroachdb/datadriven v1.0.2
github.com/knz/catwalk v0.1.2
github.com/mattn/go-runewidth v0.0.14
github.com/muesli/reflow v0.3.0
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/charmbracelet/bubbles v0.14.1-0.20221007152719-9a48dca00354/go.mod h1
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.22.1/go.mod h1:8/7hVvbPN6ZZPkczLiB8YpLkLJ0n7DMho5Wvfd2X1C0=
github.com/charmbracelet/bubbletea v0.22.2-0.20220830200705-989d49f3e69f/go.mod h1:8/7hVvbPN6ZZPkczLiB8YpLkLJ0n7DMho5Wvfd2X1C0=
github.com/charmbracelet/bubbletea v0.22.2-0.20221007125427-0e76ba142aa1 h1:rrLUJ9buyLnhFl/tjW3AK1kXSJNe9+Z2BP60umbBPkQ=
github.com/charmbracelet/bubbletea v0.22.2-0.20221007125427-0e76ba142aa1/go.mod h1:JAfGK/3/pPKHTnAS8JIE2u9f61BjWTQY57RbT25aMXU=
github.com/charmbracelet/bubbletea v0.23.1 h1:CYdteX1wCiCzKNUlwm25ZHBIc1GXlYFyUIte8WPvhck=
github.com/charmbracelet/bubbletea v0.23.1/go.mod h1:JAfGK/3/pPKHTnAS8JIE2u9f61BjWTQY57RbT25aMXU=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
Expand Down