2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-14 17:24:10 +00:00
restic/internal/ui/termstatus/terminal_unix.go
Michael Eischer 5e6af77b7a Unify interactive terminal detection code
Previously the progress bar / status update interval used
stdoutIsTerminal to determine whether it is possible to update the
progress bar or not. However, its implementation differed from the
detection within the backup command which included additional checks to
detect the presence of mintty on Windows. mintty behaves like a terminal
but uses pipes for communication.

This adds stdoutCanUpdateStatus() which calls the same terminal detection
code used by backup. This ensures that all commands consistently switch
between interactive and non-interactive terminal mode.

stdoutIsTerminal() now also returns true whenever stdoutCanUpdateStatus()
does so. This is required to properly handle the special case of mintty.
2021-04-11 20:02:09 +02:00

36 lines
893 B
Go

// +build !windows
package termstatus
import (
"io"
"os"
"golang.org/x/crypto/ssh/terminal"
)
// clearCurrentLine removes all characters from the current line and resets the
// cursor position to the first column.
func clearCurrentLine(wr io.Writer, fd uintptr) func(io.Writer, uintptr) {
return posixClearCurrentLine
}
// moveCursorUp moves the cursor to the line n lines above the current one.
func moveCursorUp(wr io.Writer, fd uintptr) func(io.Writer, uintptr, int) {
return posixMoveCursorUp
}
// CanUpdateStatus returns true if status lines can be printed, the process
// output is not redirected to a file or pipe.
func CanUpdateStatus(fd uintptr) bool {
if !terminal.IsTerminal(int(fd)) {
return false
}
term := os.Getenv("TERM")
if term == "" {
return false
}
// TODO actually read termcap db and detect if terminal supports what we need
return term != "dumb"
}