From 2c3cd35a5db9ae8c05dbdf4829138a97e71ba762 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 21 Feb 2021 18:01:20 +0300 Subject: [PATCH] use x/term instead of stty/pause to prompt any key cc #480 --- app.go | 18 +----------------- go.mod | 1 + go.sum | 2 ++ os.go | 12 ------------ os_windows.go | 4 ---- ui.go | 13 +++++++++++++ 6 files changed, 17 insertions(+), 33 deletions(-) diff --git a/app.go b/app.go index 7b2f649..69b9f78 100644 --- a/app.go +++ b/app.go @@ -366,20 +366,6 @@ func (app *app) exportFiles() { exportFiles(currFile, currSelections) } -func waitKey() error { - cmd := pauseCommand() - - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - return fmt.Errorf("waiting key: %s", err) - } - - return nil -} - // This function is used to run a shell command. Modes are as follows: // // Prefix Wait Async Stdin Stdout Stderr UI action @@ -430,9 +416,7 @@ func (app *app) runShell(s string, args []string, prefix string) { switch prefix { case "!": - if err := waitKey(); err != nil { - app.ui.echoerrf("waiting key: %s", err) - } + anyKey() } app.ui.loadFile(app.nav, true) diff --git a/go.mod b/go.mod index dbd424c..3f0cdac 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.12 require ( github.com/gdamore/tcell/v2 v2.2.0 github.com/mattn/go-runewidth v0.0.10 + golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d gopkg.in/djherbis/times.v1 v1.2.0 ) diff --git a/go.sum b/go.sum index 6f26e72..2079cfb 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuF golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE= +golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM= diff --git a/os.go b/os.go index 6dd2aff..d6a7779 100644 --- a/os.go +++ b/os.go @@ -94,18 +94,6 @@ func detachedCommand(name string, arg ...string) *exec.Cmd { return cmd } -func pauseCommand() *exec.Cmd { - cmd := `echo - echo -n 'Press any key to continue' - old=$(stty -g) - stty raw -echo - eval "ignore=\$(dd bs=1 count=1 2> /dev/null)" - stty $old - echo` - - return exec.Command(gOpts.shell, "-c", cmd) -} - func shellCommand(s string, args []string) *exec.Cmd { if len(gOpts.ifs) != 0 { s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s) diff --git a/os_windows.go b/os_windows.go index 09b06f4..37fb296 100644 --- a/os_windows.go +++ b/os_windows.go @@ -76,10 +76,6 @@ func detachedCommand(name string, arg ...string) *exec.Cmd { return cmd } -func pauseCommand() *exec.Cmd { - return exec.Command("cmd", "/c", "pause") -} - func shellCommand(s string, args []string) *exec.Cmd { args = append([]string{"/c", s}, args...) diff --git a/ui.go b/ui.go index eabb100..e576e00 100644 --- a/ui.go +++ b/ui.go @@ -17,6 +17,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/mattn/go-runewidth" + "golang.org/x/term" ) const gEscapeCode = 27 @@ -1151,6 +1152,18 @@ func (ui *ui) resume() { ui.screen.Resume() } +func anyKey() { + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer term.Restore(int(os.Stdin.Fd()), oldState) + + fmt.Print("Press any key to continue") + b := make([]byte, 1) + os.Stdin.Read(b) +} + func listMatches(screen tcell.Screen, matches []string) (*bytes.Buffer, error) { b := new(bytes.Buffer)