mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:47:50 +00:00
termstatus: move cursor handling to terminal package
This commit is contained in:
36
internal/terminal/terminal_posix.go
Normal file
36
internal/terminal/terminal_posix.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package terminal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
PosixControlMoveCursorHome = "\r"
|
||||
PosixControlMoveCursorUp = "\x1b[1A"
|
||||
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) {
|
||||
// clear current line
|
||||
_, err := wr.Write([]byte(PosixControlMoveCursorHome + PosixControlClearLine))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 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)...)
|
||||
_, err := wr.Write(data)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user