Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Fix Ctrl-C exit bug
Browse files Browse the repository at this point in the history
Restore the terminal to its previous state when exiting with Ctrl-C.
Fixes #6.
  • Loading branch information
alfredxing committed Feb 17, 2015
1 parent 9a3700b commit 13f61c4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

// Stores the state of the terminal before making it raw
var regularState *terminal.State

func main() {
if len(os.Args) > 1 {
input := strings.Replace(strings.Join(os.Args[1:], ""), " ", "", -1)
Expand All @@ -28,11 +31,12 @@ func main() {
return
}

oldState, err := terminal.MakeRaw(0)
var err error
regularState, err = terminal.MakeRaw(0)
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
defer terminal.Restore(0, regularState)

term := terminal.NewTerminal(os.Stdin, "> ")
term.AutoCompleteCallback = handleKey
Expand Down Expand Up @@ -63,6 +67,7 @@ func main() {
func handleKey(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
if key == '\x03' {
fmt.Println()
terminal.Restore(0, regularState)
os.Exit(0)
}
return "", 0, false
Expand Down

0 comments on commit 13f61c4

Please sign in to comment.