mirror of
https://github.com/restic/restic.git
synced 2026-05-23 10:35:23 +00:00
e33bcede2f
Check if each line of status is changed, and write the line to the terminal only if it has changed
52 lines
1.5 KiB
Go
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
|
|
}
|