Skip to content

Commit

Permalink
use x/term instead of stty/pause to prompt any key
Browse files Browse the repository at this point in the history
cc #480
  • Loading branch information
gokcehan committed Feb 21, 2021
1 parent c071e37 commit 2c3cd35
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 33 deletions.
18 changes: 1 addition & 17 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 0 additions & 12 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
13 changes: 13 additions & 0 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
"golang.org/x/term"
)

const gEscapeCode = 27
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2c3cd35

Please sign in to comment.