termstatus: move cursor handling to terminal package

This commit is contained in:
Michael Eischer
2025-09-07 13:49:26 +02:00
parent 0ab38faa2e
commit 93ccc548c8
7 changed files with 31 additions and 30 deletions

View 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
}
}