2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 19:49:44 +00:00

Create a helper function to get the terminal width

Rationale: contain terminal access to one file.
This commit is contained in:
Pauline Middelink 2017-03-06 11:23:00 +01:00
parent bf88a62a16
commit afcc1ba706
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 ""