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