Merge pull request #860 from middelink/factor-out

Create a helper function to get the terminal width
This commit is contained in:
Alexander Neumann 2017-03-07 10:59:39 +01:00
commit 00e7158381
4 changed files with 13 additions and 16 deletions

View File

@ -10,8 +10,6 @@ import (
"strings" "strings"
"time" "time"
"golang.org/x/crypto/ssh/terminal"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"restic/archiver" "restic/archiver"
@ -136,8 +134,7 @@ func newArchiveProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress
s.Errors) s.Errors)
status2 := fmt.Sprintf("ETA %s ", formatSeconds(eta)) status2 := fmt.Sprintf("ETA %s ", formatSeconds(eta))
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) if w := stdoutTerminalWidth(); w > 0 {
if err == nil {
maxlen := w - len(status2) - 1 maxlen := w - len(status2) - 1
if maxlen < 4 { if maxlen < 4 {
@ -181,8 +178,7 @@ func newArchiveStdinProgress(gopts GlobalOptions) *restic.Progress {
formatBytes(s.Bytes), formatBytes(s.Bytes),
formatBytes(bps)) formatBytes(bps))
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) if w := stdoutTerminalWidth(); w > 0 {
if err == nil {
maxlen := w - len(status1) maxlen := w - len(status1)
if maxlen < 4 { if maxlen < 4 {

View File

@ -7,8 +7,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"restic" "restic"
"restic/checker" "restic/checker"
"restic/errors" "restic/errors"
@ -55,8 +53,7 @@ func newReadProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress {
formatPercent(s.Blobs, todo.Blobs), formatPercent(s.Blobs, todo.Blobs),
s.Blobs, todo.Blobs) s.Blobs, todo.Blobs)
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) if w := stdoutTerminalWidth(); w > 0 {
if err == nil {
if len(status) > w { if len(status) > w {
max := w - len(status) - 4 max := w - len(status) - 4
status = status[:max] + "... " status = status[:max] + "... "

View File

@ -11,8 +11,6 @@ import (
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
) )
var cmdPrune = &cobra.Command{ var cmdPrune = &cobra.Command{
@ -45,8 +43,7 @@ func newProgressMax(show bool, max uint64, description string) *restic.Progress
formatPercent(s.Blobs, max), formatPercent(s.Blobs, max),
s.Blobs, max, description) s.Blobs, max, description)
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) if w := stdoutTerminalWidth(); w > 0 {
if err == nil {
if len(status) > w { if len(status) > w {
max := w - len(status) - 4 max := w - len(status) - 4
status = status[:max] + "... " status = status[:max] + "... "

View File

@ -82,6 +82,14 @@ func stdoutIsTerminal() bool {
return terminal.IsTerminal(int(os.Stdout.Fd())) 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 // restoreTerminal installs a cleanup handler that restores the previous
// terminal state on exit. // terminal state on exit.
func restoreTerminal() { func restoreTerminal() {
@ -110,8 +118,7 @@ func restoreTerminal() {
// current windows cmd shell. // current windows cmd shell.
func ClearLine() string { func ClearLine() string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) if w := stdoutTerminalWidth(); w > 0 {
if err == nil {
return strings.Repeat(" ", w-1) + "\r" return strings.Repeat(" ", w-1) + "\r"
} }
return "" return ""