summaryrefslogtreecommitdiff
path: root/internal/ui/termstatus/terminal_posix.go
blob: ca5468f45b98e703dbb3c23cc649cc9b27b97752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package termstatus

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