restic/internal/terminal/terminal_posix.go

39 lines
1 KiB
Go
Raw Permalink Normal View History

package terminal
2018-04-22 11:57:11 +02:00
import (
"bytes"
2018-04-22 11:57:11 +02:00
"fmt"
"io"
)
const (
2025-09-13 22:22:53 +02:00
// PosixControlMoveCursorHome moves cursor to the first column
PosixControlMoveCursorHome = "\r"
2025-09-13 22:22:53 +02:00
// PosixControlMoveCursorUp moves cursor up one line
PosixControlMoveCursorUp = "\x1b[1A"
// PosixControlClearLine clears the current line
PosixControlClearLine = "\x1b[2K"
2018-04-22 11:57:11 +02:00
)
// PosixClearCurrentLine removes all characters from the current line and resets the
// cursor position to the first column.
func PosixClearCurrentLine(wr io.Writer, _ uintptr) error {
2018-04-22 11:57:11 +02:00
// clear current line
_, err := wr.Write([]byte(PosixControlMoveCursorHome + PosixControlClearLine))
2018-04-22 11:57:11 +02:00
if err != nil {
return fmt.Errorf("write failed: %w", err)
2018-04-22 11:57:11 +02:00
}
return nil
}
2018-04-22 11:57:11 +02:00
// PosixMoveCursorUp moves the cursor to the line n lines above the current one.
func PosixMoveCursorUp(wr io.Writer, _ uintptr, n int) error {
data := []byte(PosixControlMoveCursorHome)
data = append(data, bytes.Repeat([]byte(PosixControlMoveCursorUp), n)...)
_, err := wr.Write(data)
if err != nil {
return fmt.Errorf("write failed: %w", err)
2018-04-22 11:57:11 +02:00
}
return nil
2018-04-22 11:57:11 +02:00
}