mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:47:50 +00:00
consolidate checks whether stdin/stdout is terminal
This commit is contained in:
29
internal/terminal/stdio.go
Normal file
29
internal/terminal/stdio.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package terminal
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func StdinIsTerminal() bool {
|
||||
return term.IsTerminal(int(os.Stdin.Fd()))
|
||||
}
|
||||
|
||||
func StdoutIsTerminal() bool {
|
||||
// mintty on windows can use pipes which behave like a posix terminal,
|
||||
// but which are not a terminal handle
|
||||
return term.IsTerminal(int(os.Stdout.Fd())) || StdoutCanUpdateStatus()
|
||||
}
|
||||
|
||||
func StdoutCanUpdateStatus() bool {
|
||||
return CanUpdateStatus(os.Stdout.Fd())
|
||||
}
|
||||
|
||||
func StdoutTerminalWidth() int {
|
||||
w, _, err := term.GetSize(int(os.Stdout.Fd()))
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return w
|
||||
}
|
||||
@@ -13,7 +13,7 @@ const (
|
||||
PosixControlClearLine = "\x1b[2K"
|
||||
)
|
||||
|
||||
// posixClearCurrentLine removes all characters from the current line and resets the
|
||||
// PosixClearCurrentLine removes all characters from the current line and resets the
|
||||
// cursor position to the first column.
|
||||
func PosixClearCurrentLine(wr io.Writer, _ uintptr) {
|
||||
// clear current line
|
||||
@@ -24,7 +24,7 @@ func PosixClearCurrentLine(wr io.Writer, _ uintptr) {
|
||||
}
|
||||
}
|
||||
|
||||
// posixMoveCursorUp moves the cursor to the line n lines above the current one.
|
||||
// PosixMoveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func PosixMoveCursorUp(wr io.Writer, _ uintptr, n int) {
|
||||
data := []byte(PosixControlMoveCursorHome)
|
||||
data = append(data, bytes.Repeat([]byte(PosixControlMoveCursorUp), n)...)
|
||||
|
||||
@@ -10,13 +10,13 @@ import (
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// clearCurrentLine removes all characters from the current line and resets the
|
||||
// ClearCurrentLine removes all characters from the current line and resets the
|
||||
// cursor position to the first column.
|
||||
func ClearCurrentLine(_ uintptr) func(io.Writer, uintptr) {
|
||||
return PosixClearCurrentLine
|
||||
}
|
||||
|
||||
// moveCursorUp moves the cursor to the line n lines above the current one.
|
||||
// MoveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func MoveCursorUp(_ uintptr) func(io.Writer, uintptr, int) {
|
||||
return PosixMoveCursorUp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user