-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: restore terminal state properly
- Loading branch information
Showing
6 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |