Files
restic/internal/terminal/terminal_posix.go
T
Donggyu Kim e33bcede2f terminal: Do not write unchanged status lines
Check if each line of status is changed, and write
the line to the terminal only if it has changed
2026-05-14 10:42:13 +02:00

52 lines
1.5 KiB
Go

package terminal
import (
"bytes"
"fmt"
"io"
)
const (
// PosixControlMoveCursorHome moves cursor to the first column
PosixControlMoveCursorHome = "\r"
// PosixControlMoveCursorUp moves cursor up one line
PosixControlMoveCursorUp = "\x1b[1A"
// PosixControlMoveCursorDown moves cursor down one line
PosixControlMoveCursorDown = "\x1b[1B"
// PosixControlClearLine clears the current line
PosixControlClearLine = "\x1b[2K"
)
// PosixClearCurrentLine removes all characters from the current line and resets the
// cursor position to the first column.
func PosixClearCurrentLine(wr io.Writer, _ uintptr) error {
// clear current line
_, err := wr.Write([]byte(PosixControlMoveCursorHome + PosixControlClearLine))
if err != nil {
return fmt.Errorf("write failed: %w", err)
}
return nil
}
// 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)
}
return nil
}
// PosixMoveCursorDown moves the cursor to the line n lines below the current one.
func PosixMoveCursorDown(wr io.Writer, _ uintptr, n int) error {
data := []byte(PosixControlMoveCursorHome)
data = append(data, bytes.Repeat([]byte(PosixControlMoveCursorDown), n)...)
_, err := wr.Write(data)
if err != nil {
return fmt.Errorf("write failed: %w", err)
}
return nil
}