mirror of
https://github.com/restic/restic.git
synced 2024-12-26 09:47:49 +00:00
Merge pull request #439 from restic/fix-402
Add cleanup handler to restore terminal state
This commit is contained in:
commit
50380f8c14
1 changed files with 43 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
"github.com/restic/restic/backend"
|
"github.com/restic/restic/backend"
|
||||||
|
@ -34,6 +35,48 @@ type GlobalOptions struct {
|
||||||
stderr io.Writer
|
stderr io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
restoreTerminal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkErrno returns nil when err is set to syscall.Errno(0), since this is no
|
||||||
|
// error condition.
|
||||||
|
func checkErrno(err error) error {
|
||||||
|
e, ok := err.(syscall.Errno)
|
||||||
|
if !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if e == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// restoreTerminal installs a cleanup handler that restores the previous
|
||||||
|
// terminal state on exit.
|
||||||
|
func restoreTerminal() {
|
||||||
|
fd := int(os.Stdout.Fd())
|
||||||
|
if !terminal.IsTerminal(fd) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err := terminal.GetState(fd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "unable to get terminal state: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
AddCleanupHandler(func() error {
|
||||||
|
err := checkErrno(terminal.Restore(fd, state))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "unable to get restore terminal state: %#+v\n", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var globalOpts = GlobalOptions{stdout: os.Stdout, stderr: os.Stderr}
|
var globalOpts = GlobalOptions{stdout: os.Stdout, stderr: os.Stderr}
|
||||||
var parser = flags.NewParser(&globalOpts, flags.HelpFlag|flags.PassDoubleDash)
|
var parser = flags.NewParser(&globalOpts, flags.HelpFlag|flags.PassDoubleDash)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue