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"
"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 {

View File

@ -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] + "... "

View File

@ -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] + "... "

View File

@ -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 ""