Skip to content

Commit

Permalink
fix: restore terminal state properly
Browse files Browse the repository at this point in the history
  • Loading branch information
pombadev committed Feb 10, 2022
1 parent 5b5f2ed commit 1b8b9c0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SHELL = /usr/bin/bash

clean:
go clean

Expand All @@ -8,4 +10,21 @@ sbuild:
go build -ldflags "-s -w"

run:
go run .
go run .

.ONESHELL:
tag:
@force=''
@tag=$$(grep -w AppVersion main.go | cut -d' ' -f4 | sed 's/"//g')

if git rev-parse -q --verify "refs/tags/$$tag" &> /dev/null; then
@echo "'$$tag' exits, overwrite?"
select yn in "Yes" "No"; do
case $$yn in
Yes ) force="-f"; break;;
No ) exit 0;;
esac
done
fi

git tag -a "$$tag" -m "Tagging $$tag" $$force
5 changes: 5 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func HasTheme(name string) (bool, error) {
}
}

//lint:ignore ST1005 I like the way this looks
return false, fmt.Errorf("\nValue must be one of\n%s\n", strings.Join(availableStyles, ", "))
}

Expand Down Expand Up @@ -177,6 +178,8 @@ func (app *App) Repl() {
case "version":
app.Version()
case "exit":
// deferred functions are not run of os.Exit is called
restoreTermState()
os.Exit(0)
case "help":
help("")
Expand All @@ -195,6 +198,8 @@ func (app *App) Repl() {
)

repl.Run()

defer restoreTermState()
}

// Search whatever query (-query flag) was passed
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ require (
github.com/pkg/term v1.2.0-beta.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20220207234003-57398862261d // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.7 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220207234003-57398862261d h1:Bm7BNOQt2Qv7ZqysjeLjgCBanX+88Z/OtdvsrEv1Djc=
golang.org/x/sys v0.0.0-20220207234003-57398862261d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
Expand Down
18 changes: 0 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package main

import (
"io"
"os/exec"
"runtime"
)

const (
AppName string = "commandlinefu"
AppVersion string = "v1.5.2"
)

func main() {
NewApp().Run()

defer func() {
// terminal state is not restored on exit, manually doing it
// https://github.com/c-bata/go-prompt/issues?q=is%3Aissue+stty
if runtime.GOOS == "linux" {
cmd := exec.Command("stty", "sane")
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard

_ = cmd.Run()
}
}()
}
37 changes: 37 additions & 0 deletions terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"os"

"golang.org/x/term"
)

// terminal state is not restored on exit, manually doing it
// https://github.com/c-bata/go-prompt/issues?q=is%3Aissue+stty

// Thanks @WangYihang
// https://github.com/c-bata/go-prompt/issues/233#issuecomment-934395156

var termState *term.State

func saveTermState() {
oldState, err := term.GetState(int(os.Stdin.Fd()))

if err != nil {
return
}

termState = oldState
}

func restoreTermState() {
if termState != nil {
run(func() error {
return term.Restore(int(os.Stdin.Fd()), termState)
})
}
}

func init() {
saveTermState()
}

0 comments on commit 1b8b9c0

Please sign in to comment.