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

Option to restore terminal when interrupted #195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions example/readline-demo/readline-demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func main() {

HistorySearchFold: true,
FuncFilterInputRune: filterInput,
ExitRawOnInterrupt: true,
})
if err != nil {
panic(err)
Expand Down
27 changes: 26 additions & 1 deletion readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
//
package readline

import "io"
import (
"fmt"
"io"
"os"
"os/signal"
"syscall"
)

type Instance struct {
Config *Config
Expand Down Expand Up @@ -77,6 +83,10 @@ type Config struct {
FuncOnWidthChanged func(func())
ForceUseInteractive bool

// ExitRawOnInterrupt, if set, forces a return to previous terminal settings
// after receiving Interrupt and TERM signals.
ExitRawOnInterrupt bool

// private fields
inited bool
opHistory *opHistory
Expand Down Expand Up @@ -168,6 +178,21 @@ func NewEx(cfg *Config) (*Instance, error) {
if cfg.Painter == nil {
cfg.Painter = &defaultPainter{}
}

// just restore the terminal before quitting.
if cfg.ExitRawOnInterrupt && cfg.FuncIsTerminal() {
cSignal := make(chan os.Signal, 1)
signal.Notify(cSignal, os.Interrupt, syscall.SIGTERM)

go func() {
for range cSignal {
_ = t.ExitRawMode()
fmt.Println()
os.Exit(1)
}
}()
}

return &Instance{
Config: cfg,
Terminal: t,
Expand Down