From afcc1ba706486a8be98adffbe8cd2052e053d0be Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Mon, 6 Mar 2017 11:23:00 +0100 Subject: [PATCH] Create a helper function to get the terminal width Rationale: contain terminal access to one file. --- src/cmds/restic/cmd_backup.go | 8 ++------ src/cmds/restic/cmd_check.go | 5 +---- src/cmds/restic/cmd_prune.go | 5 +---- src/cmds/restic/global.go | 11 +++++++++-- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 6c259ffa0..ee4a3e61d 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -10,8 +10,6 @@ import ( "strings" "time" - "golang.org/x/crypto/ssh/terminal" - "github.com/spf13/cobra" "restic/archiver" @@ -136,8 +134,7 @@ func newArchiveProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress s.Errors) status2 := fmt.Sprintf("ETA %s ", formatSeconds(eta)) - w, _, err := terminal.GetSize(int(os.Stdout.Fd())) - if err == nil { + if w := stdoutTerminalWidth(); w > 0 { maxlen := w - len(status2) - 1 if maxlen < 4 { @@ -181,8 +178,7 @@ func newArchiveStdinProgress(gopts GlobalOptions) *restic.Progress { formatBytes(s.Bytes), formatBytes(bps)) - w, _, err := terminal.GetSize(int(os.Stdout.Fd())) - if err == nil { + if w := stdoutTerminalWidth(); w > 0 { maxlen := w - len(status1) if maxlen < 4 { diff --git a/src/cmds/restic/cmd_check.go b/src/cmds/restic/cmd_check.go index 074cf6f27..cd041a42c 100644 --- a/src/cmds/restic/cmd_check.go +++ b/src/cmds/restic/cmd_check.go @@ -7,8 +7,6 @@ import ( "github.com/spf13/cobra" - "golang.org/x/crypto/ssh/terminal" - "restic" "restic/checker" "restic/errors" @@ -55,8 +53,7 @@ func newReadProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress { formatPercent(s.Blobs, todo.Blobs), s.Blobs, todo.Blobs) - w, _, err := terminal.GetSize(int(os.Stdout.Fd())) - if err == nil { + if w := stdoutTerminalWidth(); w > 0 { if len(status) > w { max := w - len(status) - 4 status = status[:max] + "... " diff --git a/src/cmds/restic/cmd_prune.go b/src/cmds/restic/cmd_prune.go index 7fadcc162..fec8126f2 100644 --- a/src/cmds/restic/cmd_prune.go +++ b/src/cmds/restic/cmd_prune.go @@ -11,8 +11,6 @@ import ( "time" "github.com/spf13/cobra" - - "golang.org/x/crypto/ssh/terminal" ) var cmdPrune = &cobra.Command{ @@ -45,8 +43,7 @@ func newProgressMax(show bool, max uint64, description string) *restic.Progress formatPercent(s.Blobs, max), s.Blobs, max, description) - w, _, err := terminal.GetSize(int(os.Stdout.Fd())) - if err == nil { + if w := stdoutTerminalWidth(); w > 0 { if len(status) > w { max := w - len(status) - 4 status = status[:max] + "... " diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index b73d9de47..02a2e9b52 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -82,6 +82,14 @@ func stdoutIsTerminal() bool { return terminal.IsTerminal(int(os.Stdout.Fd())) } +func stdoutTerminalWidth() int { + w, _, err := terminal.GetSize(int(os.Stdout.Fd())) + if err != nil { + return 0 + } + return w +} + // restoreTerminal installs a cleanup handler that restores the previous // terminal state on exit. func restoreTerminal() { @@ -110,8 +118,7 @@ func restoreTerminal() { // current windows cmd shell. func ClearLine() string { if runtime.GOOS == "windows" { - w, _, err := terminal.GetSize(int(os.Stdout.Fd())) - if err == nil { + if w := stdoutTerminalWidth(); w > 0 { return strings.Repeat(" ", w-1) + "\r" } return ""