2018-04-22 09:57:11 +00:00
|
|
|
package termstatus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2023-02-11 13:51:58 +00:00
|
|
|
"strconv"
|
2018-04-22 09:57:11 +00:00
|
|
|
"strings"
|
2021-08-29 12:54:15 +00:00
|
|
|
"unicode"
|
2020-02-29 14:56:26 +00:00
|
|
|
|
2022-03-28 20:24:15 +00:00
|
|
|
"golang.org/x/term"
|
2020-11-02 11:06:21 +00:00
|
|
|
"golang.org/x/text/width"
|
2018-04-22 09:57:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Terminal is used to write messages and display status lines which can be
|
|
|
|
// updated. When the output is redirected to a file, the status lines are not
|
|
|
|
// printed.
|
|
|
|
type Terminal struct {
|
|
|
|
wr *bufio.Writer
|
|
|
|
fd uintptr
|
|
|
|
errWriter io.Writer
|
|
|
|
buf *bytes.Buffer
|
|
|
|
msg chan message
|
|
|
|
status chan status
|
|
|
|
canUpdateStatus bool
|
2022-10-07 18:40:39 +00:00
|
|
|
lastStatusLen int
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
// will be closed when the goroutine which runs Run() terminates, so it'll
|
|
|
|
// yield a default value immediately
|
|
|
|
closed chan struct{}
|
|
|
|
|
|
|
|
clearCurrentLine func(io.Writer, uintptr)
|
|
|
|
moveCursorUp func(io.Writer, uintptr, int)
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
|
|
|
|
type message struct {
|
|
|
|
line string
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type status struct {
|
|
|
|
lines []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type fder interface {
|
|
|
|
Fd() uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Terminal for wr. A goroutine is started to update the
|
|
|
|
// terminal. It is terminated when ctx is cancelled. When wr is redirected to
|
|
|
|
// a file (e.g. via shell output redirection) or is just an io.Writer (not the
|
|
|
|
// open *os.File for stdout), no status lines are printed. The status lines and
|
|
|
|
// normal output (via Print/Printf) are written to wr, error messages are
|
2018-05-02 19:24:18 +00:00
|
|
|
// written to errWriter. If disableStatus is set to true, no status messages
|
|
|
|
// are printed even if the terminal supports it.
|
|
|
|
func New(wr io.Writer, errWriter io.Writer, disableStatus bool) *Terminal {
|
2018-04-22 09:57:11 +00:00
|
|
|
t := &Terminal{
|
|
|
|
wr: bufio.NewWriter(wr),
|
|
|
|
errWriter: errWriter,
|
|
|
|
buf: bytes.NewBuffer(nil),
|
|
|
|
msg: make(chan message),
|
|
|
|
status: make(chan status),
|
2018-05-07 19:40:07 +00:00
|
|
|
closed: make(chan struct{}),
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:24:18 +00:00
|
|
|
if disableStatus {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-03-07 21:50:52 +00:00
|
|
|
if d, ok := wr.(fder); ok && CanUpdateStatus(d.Fd()) {
|
2018-04-22 09:57:11 +00:00
|
|
|
// only use the fancy status code when we're running on a real terminal.
|
|
|
|
t.canUpdateStatus = true
|
|
|
|
t.fd = d.Fd()
|
2023-05-18 17:30:44 +00:00
|
|
|
t.clearCurrentLine = clearCurrentLine(t.fd)
|
|
|
|
t.moveCursorUp = moveCursorUp(t.fd)
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:05:08 +00:00
|
|
|
// CanUpdateStatus return whether the status output is updated in place.
|
|
|
|
func (t *Terminal) CanUpdateStatus() bool {
|
|
|
|
return t.canUpdateStatus
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:57:11 +00:00
|
|
|
// Run updates the screen. It should be run in a separate goroutine. When
|
|
|
|
// ctx is cancelled, the status lines are cleanly removed.
|
|
|
|
func (t *Terminal) Run(ctx context.Context) {
|
2018-05-07 19:40:07 +00:00
|
|
|
defer close(t.closed)
|
2018-04-22 09:57:11 +00:00
|
|
|
if t.canUpdateStatus {
|
|
|
|
t.run(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.runWithoutStatus(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run listens on the channels and updates the terminal screen.
|
|
|
|
func (t *Terminal) run(ctx context.Context) {
|
2018-05-07 19:40:07 +00:00
|
|
|
var status []string
|
2018-04-22 09:57:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-12-06 17:28:19 +00:00
|
|
|
if !IsProcessBackground(t.fd) {
|
2024-01-21 20:16:33 +00:00
|
|
|
t.writeStatus([]string{})
|
2018-04-28 13:24:36 +00:00
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
case msg := <-t.msg:
|
2020-10-13 10:56:23 +00:00
|
|
|
if IsProcessBackground(t.fd) {
|
2018-04-28 13:24:36 +00:00
|
|
|
// ignore all messages, do nothing, we are in the background process group
|
|
|
|
continue
|
|
|
|
}
|
2018-05-07 19:40:07 +00:00
|
|
|
t.clearCurrentLine(t.wr, t.fd)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
|
|
|
var dst io.Writer
|
|
|
|
if msg.err {
|
|
|
|
dst = t.errWriter
|
|
|
|
|
|
|
|
// assume t.wr and t.errWriter are different, so we need to
|
2018-05-07 19:40:07 +00:00
|
|
|
// flush clearing the current line
|
2018-04-22 09:57:11 +00:00
|
|
|
err := t.wr.Flush()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dst = t.wr
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:54:16 +00:00
|
|
|
if _, err := io.WriteString(dst, msg.line); err != nil {
|
2018-04-22 09:57:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
t.writeStatus(status)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2020-03-15 21:54:16 +00:00
|
|
|
if err := t.wr.Flush(); err != nil {
|
2018-04-22 09:57:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case stat := <-t.status:
|
2020-10-13 10:56:23 +00:00
|
|
|
if IsProcessBackground(t.fd) {
|
2018-04-28 13:24:36 +00:00
|
|
|
// ignore all messages, do nothing, we are in the background process group
|
|
|
|
continue
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
status = status[:0]
|
|
|
|
status = append(status, stat.lines...)
|
|
|
|
t.writeStatus(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
func (t *Terminal) writeStatus(status []string) {
|
2022-10-07 18:40:39 +00:00
|
|
|
statusLen := len(status)
|
|
|
|
status = append([]string{}, status...)
|
|
|
|
for i := len(status); i < t.lastStatusLen; i++ {
|
|
|
|
// clear no longer used status lines
|
|
|
|
status = append(status, "")
|
|
|
|
if i > 0 {
|
|
|
|
// all lines except the last one must have a line break
|
|
|
|
status[i-1] = status[i-1] + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.lastStatusLen = statusLen
|
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
for _, line := range status {
|
|
|
|
t.clearCurrentLine(t.wr, t.fd)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
_, err := t.wr.WriteString(line)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush is needed so that the current line is updated
|
|
|
|
err = t.wr.Flush()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 19:40:07 +00:00
|
|
|
|
|
|
|
if len(status) > 0 {
|
|
|
|
t.moveCursorUp(t.wr, t.fd, len(status)-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.wr.Flush()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runWithoutStatus listens on the channels and just prints out the messages,
|
|
|
|
// without status lines.
|
|
|
|
func (t *Terminal) runWithoutStatus(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case msg := <-t.msg:
|
|
|
|
var flush func() error
|
|
|
|
|
|
|
|
var dst io.Writer
|
|
|
|
if msg.err {
|
|
|
|
dst = t.errWriter
|
|
|
|
} else {
|
|
|
|
dst = t.wr
|
|
|
|
flush = t.wr.Flush
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:54:16 +00:00
|
|
|
if _, err := io.WriteString(dst, msg.line); err != nil {
|
2018-04-22 09:57:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flush == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:54:16 +00:00
|
|
|
if err := flush(); err != nil {
|
2018-04-22 09:57:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:05:08 +00:00
|
|
|
case stat := <-t.status:
|
|
|
|
for _, line := range stat.lines {
|
2021-01-28 12:30:10 +00:00
|
|
|
// Ensure that each message ends with exactly one newline.
|
|
|
|
fmt.Fprintln(t.wr, strings.TrimRight(line, "\n"))
|
2020-12-29 14:05:08 +00:00
|
|
|
}
|
|
|
|
if err := t.wr.Flush(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "flush failed: %v\n", err)
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:20:18 +00:00
|
|
|
func (t *Terminal) print(line string, isErr bool) {
|
2018-04-22 09:57:11 +00:00
|
|
|
// make sure the line ends with a line break
|
|
|
|
if line[len(line)-1] != '\n' {
|
|
|
|
line += "\n"
|
|
|
|
}
|
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
select {
|
2020-10-17 18:20:18 +00:00
|
|
|
case t.msg <- message{line: line, err: isErr}:
|
2018-05-07 19:40:07 +00:00
|
|
|
case <-t.closed:
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:20:18 +00:00
|
|
|
// Print writes a line to the terminal.
|
|
|
|
func (t *Terminal) Print(line string) {
|
|
|
|
t.print(line, false)
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:57:11 +00:00
|
|
|
// Error writes an error to the terminal.
|
|
|
|
func (t *Terminal) Error(line string) {
|
2020-10-17 18:20:18 +00:00
|
|
|
t.print(line, true)
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 11:06:21 +00:00
|
|
|
// Truncate s to fit in width (number of terminal cells) w.
|
|
|
|
// If w is negative, returns the empty string.
|
2021-08-29 12:54:15 +00:00
|
|
|
func Truncate(s string, w int) string {
|
2020-11-02 11:06:21 +00:00
|
|
|
if len(s) < w {
|
|
|
|
// Since the display width of a character is at most 2
|
|
|
|
// and all of ASCII (single byte per rune) has width 1,
|
|
|
|
// no character takes more bytes to encode than its width.
|
|
|
|
return s
|
2018-05-27 10:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 09:13:39 +00:00
|
|
|
for i := uint(0); i < uint(len(s)); {
|
|
|
|
utfsize := uint(1) // UTF-8 encoding size of first rune in s.
|
2021-08-29 12:54:15 +00:00
|
|
|
w--
|
2023-04-14 09:13:39 +00:00
|
|
|
|
|
|
|
if s[i] > unicode.MaxASCII {
|
|
|
|
var wide bool
|
|
|
|
if wide, utfsize = wideRune(s[i:]); wide {
|
|
|
|
w--
|
|
|
|
}
|
2020-11-02 11:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if w < 0 {
|
|
|
|
return s[:i]
|
|
|
|
}
|
2023-04-14 09:13:39 +00:00
|
|
|
i += utfsize
|
2018-05-27 10:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 11:06:21 +00:00
|
|
|
return s
|
2018-05-27 10:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 09:13:39 +00:00
|
|
|
// Guess whether the first rune in s would occupy two terminal cells
|
|
|
|
// instead of one. This cannot be determined exactly without knowing
|
2023-12-06 12:11:55 +00:00
|
|
|
// the terminal font, so we treat all ambiguous runes as full-width,
|
2023-04-14 09:13:39 +00:00
|
|
|
// i.e., two cells.
|
|
|
|
func wideRune(s string) (wide bool, utfsize uint) {
|
|
|
|
prop, size := width.LookupString(s)
|
|
|
|
kind := prop.Kind()
|
|
|
|
wide = kind != width.Neutral && kind != width.EastAsianNarrow
|
|
|
|
return wide, uint(size)
|
2021-08-29 12:54:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 16:21:08 +00:00
|
|
|
func sanitizeLines(lines []string, width int) []string {
|
|
|
|
// Sanitize lines and truncate them if they're too long.
|
|
|
|
for i, line := range lines {
|
|
|
|
line = Quote(line)
|
|
|
|
if width > 0 {
|
|
|
|
line = Truncate(line, width-2)
|
|
|
|
}
|
|
|
|
if i < len(lines)-1 { // Last line gets no line break.
|
|
|
|
line += "\n"
|
|
|
|
}
|
|
|
|
lines[i] = line
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:57:11 +00:00
|
|
|
// SetStatus updates the status lines.
|
2023-02-11 13:51:58 +00:00
|
|
|
// The lines should not contain newlines; this method adds them.
|
2024-07-06 09:27:35 +00:00
|
|
|
// Pass nil or an empty array to remove the status lines.
|
2018-04-22 09:57:11 +00:00
|
|
|
func (t *Terminal) SetStatus(lines []string) {
|
2020-12-29 14:08:29 +00:00
|
|
|
// only truncate interactive status output
|
|
|
|
var width int
|
|
|
|
if t.canUpdateStatus {
|
|
|
|
var err error
|
2022-03-28 20:24:15 +00:00
|
|
|
width, _, err = term.GetSize(int(t.fd))
|
2020-12-29 14:08:29 +00:00
|
|
|
if err != nil || width <= 0 {
|
|
|
|
// use 80 columns by default
|
|
|
|
width = 80
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 16:21:08 +00:00
|
|
|
sanitizeLines(lines, width)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
select {
|
|
|
|
case t.status <- status{lines: lines}:
|
|
|
|
case <-t.closed:
|
|
|
|
}
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
2023-02-11 13:51:58 +00:00
|
|
|
|
|
|
|
// Quote lines with funny characters in them, meaning control chars, newlines,
|
|
|
|
// tabs, anything else non-printable and invalid UTF-8.
|
|
|
|
//
|
|
|
|
// This is intended to produce a string that does not mess up the terminal
|
|
|
|
// rather than produce an unambiguous quoted string.
|
|
|
|
func Quote(line string) string {
|
|
|
|
for _, r := range line {
|
|
|
|
// The replacement character usually means the input is not UTF-8.
|
|
|
|
if r == unicode.ReplacementChar || !unicode.IsPrint(r) {
|
|
|
|
return strconv.Quote(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line
|
|
|
|
}
|