2022-10-31 15:40:22 +00:00
|
|
|
package terminal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/muesli/cancelreader"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Terminal interface {
|
|
|
|
io.WriteCloser
|
|
|
|
|
|
|
|
IsRaw() bool
|
|
|
|
Size() (width, height int, err error)
|
|
|
|
|
|
|
|
ClearLine()
|
2022-12-20 18:23:03 +00:00
|
|
|
ClearEnd()
|
2022-10-31 15:40:22 +00:00
|
|
|
CursorUp(count int)
|
|
|
|
CursorDown(count int)
|
2022-12-20 18:56:59 +00:00
|
|
|
HideCursor()
|
|
|
|
ShowCursor()
|
2022-10-31 15:40:22 +00:00
|
|
|
|
|
|
|
ReadKey() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrNotATerminal = errors.New("not a terminal")
|
|
|
|
|
|
|
|
type terminal struct {
|
|
|
|
fd int
|
|
|
|
info Info
|
|
|
|
raw bool
|
|
|
|
save *term.State
|
|
|
|
|
|
|
|
out io.Writer
|
|
|
|
in cancelreader.CancelReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func Open(in io.Reader, out io.Writer, raw bool) (Terminal, error) {
|
|
|
|
type fileLike interface {
|
|
|
|
Fd() uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile, ok := out.(fileLike)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrNotATerminal
|
|
|
|
}
|
|
|
|
outFd := int(outFile.Fd())
|
|
|
|
|
|
|
|
width, height, err := term.GetSize(outFd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting dimensions: %w", err)
|
|
|
|
}
|
|
|
|
if width == 0 || height == 0 {
|
|
|
|
return nil, fmt.Errorf("unusable dimensions (%v x %v)", width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
termType := os.Getenv("TERM")
|
|
|
|
if termType == "" {
|
|
|
|
termType = "vt102"
|
|
|
|
}
|
|
|
|
info := OpenInfo(termType)
|
|
|
|
|
|
|
|
var save *term.State
|
|
|
|
var inFile cancelreader.CancelReader
|
|
|
|
if raw {
|
|
|
|
if save, err = term.MakeRaw(outFd); err != nil {
|
|
|
|
return nil, fmt.Errorf("enabling raw mode: %w", err)
|
|
|
|
}
|
|
|
|
if inFile, err = cancelreader.NewReader(in); err != nil {
|
|
|
|
return nil, ErrNotATerminal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &terminal{
|
|
|
|
fd: outFd,
|
|
|
|
info: info,
|
|
|
|
raw: raw,
|
|
|
|
save: save,
|
|
|
|
out: out,
|
|
|
|
in: inFile,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) IsRaw() bool {
|
|
|
|
return t.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) Close() error {
|
|
|
|
t.in.Cancel()
|
|
|
|
if t.save != nil {
|
|
|
|
return term.Restore(t.fd, t.save)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) Size() (width, height int, err error) {
|
|
|
|
return term.GetSize(t.fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) Write(b []byte) (int, error) {
|
|
|
|
if !t.raw {
|
|
|
|
return t.out.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
written := 0
|
|
|
|
for {
|
|
|
|
newline := bytes.IndexByte(b, '\n')
|
|
|
|
if newline == -1 {
|
|
|
|
w, err := t.out.Write(b)
|
|
|
|
written += w
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := t.out.Write(b[:newline])
|
|
|
|
written += w
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = t.out.Write([]byte{'\r', '\n'}); err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
written++
|
|
|
|
|
|
|
|
b = b[newline+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) ClearLine() {
|
|
|
|
t.info.ClearLine(t.out)
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:23:03 +00:00
|
|
|
func (t *terminal) ClearEnd() {
|
|
|
|
t.info.ClearEnd(t.out)
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:40:22 +00:00
|
|
|
func (t *terminal) CursorUp(count int) {
|
|
|
|
t.info.CursorUp(t.out, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) CursorDown(count int) {
|
|
|
|
t.info.CursorDown(t.out, count)
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:56:59 +00:00
|
|
|
func (t *terminal) HideCursor() {
|
|
|
|
t.info.HideCursor(t.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *terminal) ShowCursor() {
|
|
|
|
t.info.ShowCursor(t.out)
|
|
|
|
}
|
|
|
|
|
2023-03-06 14:56:54 +00:00
|
|
|
type stateFunc func(b byte) stateFunc
|
|
|
|
|
|
|
|
// ansiKind
|
|
|
|
type ansiKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ansiError ansiKind = iota // ansiError indicates a decoding error
|
|
|
|
ansiKey // ansiKey indicates a normal keypress
|
|
|
|
ansiEscape // ansiEscape indicates an ANSI escape sequence
|
|
|
|
ansiControl // ansiControl indicates an ANSI control sequence
|
|
|
|
)
|
|
|
|
|
|
|
|
// ansiDecoder is responsible for decoding ANSI escape and control sequences as per ECMA-48 et. al.
|
|
|
|
//
|
|
|
|
// - ANSI escape sequences are of the form "'\x1b' (intermediate bytes) <final byte>", where intermediate bytes are in
|
|
|
|
// the range [0x20, 0x30) and the final byte is in the range [0x30, 0x7f)
|
|
|
|
// - ANSI control sequences are of the form "'\x1b' '[' (parameter bytes) (intermediate bytes) <final byte>", where
|
|
|
|
// parameter bytes are in the range [0x30, 0x40), intermediate bytes are in the range [0x20, 0x30), and the final
|
|
|
|
// byte is in the range [0x40, 0x7f). Note that in most references (incl. ECMA-48), "'\x1b' '['" is referred to as
|
|
|
|
// a Control Sequence Indicator, or CSI.
|
|
|
|
//
|
|
|
|
// Any sequence that is introduced with a byte that is not '\x1b' is treated as a normal keypress.
|
|
|
|
//
|
|
|
|
// No post-processing is done on the decoded sequences to ensure that e.g. the parameter count, etc. is valid--any such
|
|
|
|
// processing is up to the consumer.
|
|
|
|
type ansiDecoder struct {
|
|
|
|
kind ansiKind // the kind of the decoded sequence.
|
|
|
|
params []byte // the decoded control sequence's parameter bytes, if any
|
|
|
|
intermediate []byte // the decoded escape or control sequence's intermediate bytes, if any.
|
|
|
|
final byte // the final byte of the sequence.
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateControlIntermediate decodes optional intermediate bytes and the final byte of a control sequence.
|
|
|
|
func (d *ansiDecoder) stateControlIntermediate(b byte) stateFunc {
|
|
|
|
if b >= 0x20 && b < 0x30 {
|
|
|
|
d.intermediate = append(d.intermediate, b)
|
|
|
|
return d.stateControlIntermediate
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
if b >= 0x40 && b < 0x7f {
|
|
|
|
d.kind = ansiControl
|
|
|
|
}
|
|
|
|
d.final = b
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-31 15:40:22 +00:00
|
|
|
|
2023-03-06 14:56:54 +00:00
|
|
|
// stateControl decodes optional parameter bytes of a control sequence.
|
|
|
|
func (d *ansiDecoder) stateControl(b byte) stateFunc {
|
|
|
|
if b >= 0x30 && b < 0x40 {
|
|
|
|
d.params = append(d.params, b)
|
|
|
|
return d.stateControl
|
|
|
|
}
|
|
|
|
return d.stateControlIntermediate(b)
|
|
|
|
}
|
2022-10-31 15:40:22 +00:00
|
|
|
|
2023-03-06 14:56:54 +00:00
|
|
|
// stateEscapeIntermediate decodes optional intermediate bytes and the final byte of an escape sequence.
|
|
|
|
func (d *ansiDecoder) stateEscapeIntermediate(b byte) stateFunc {
|
|
|
|
if b >= 0x20 && b < 0x30 {
|
|
|
|
d.intermediate = append(d.intermediate, b)
|
|
|
|
return d.stateEscapeIntermediate
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
if b >= 0x30 && b < 0x7f {
|
|
|
|
d.kind = ansiEscape
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
d.final = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateEscape determines whether a sequence beginning with '\x1b' is an escape sequence or a control sequence.
|
|
|
|
func (d *ansiDecoder) stateEscape(b byte) stateFunc {
|
|
|
|
if b == '[' {
|
|
|
|
return d.stateControl
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
return d.stateEscapeIntermediate(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateInit is the initial state for the decoder.
|
|
|
|
func (d *ansiDecoder) stateInit(b byte) stateFunc {
|
|
|
|
if b == 0x1b {
|
|
|
|
return d.stateEscape
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
d.kind, d.final = ansiKey, b
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-31 15:40:22 +00:00
|
|
|
|
2023-03-06 14:56:54 +00:00
|
|
|
// decode decodes the next key, escape sequence, or control sequence from in. The results are left in the decoder.
|
|
|
|
func (d *ansiDecoder) decode(in io.Reader) error {
|
|
|
|
state := d.stateInit
|
2022-10-31 15:40:22 +00:00
|
|
|
for {
|
|
|
|
var b [1]byte
|
2023-03-06 14:56:54 +00:00
|
|
|
if _, err := in.Read(b[:]); err != nil {
|
|
|
|
return err
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 14:56:54 +00:00
|
|
|
next := state(b[0])
|
2022-10-31 15:40:22 +00:00
|
|
|
if next == nil {
|
2023-03-06 14:56:54 +00:00
|
|
|
return nil
|
2022-10-31 15:40:22 +00:00
|
|
|
}
|
|
|
|
state = next
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 14:56:54 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
KeyCtrlC = "ctrl+c"
|
2023-03-06 17:06:48 +00:00
|
|
|
KeyCtrlO = "ctrl+o"
|
2023-03-06 14:56:54 +00:00
|
|
|
KeyDown = "down"
|
2024-02-24 08:31:36 +00:00
|
|
|
KeyEnd = "end"
|
|
|
|
KeyHome = "home"
|
2023-03-06 14:56:54 +00:00
|
|
|
KeyPageDown = "page-down"
|
|
|
|
KeyPageUp = "page-up"
|
|
|
|
KeyUp = "up"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadKey reads a keypress from the terminal.
|
|
|
|
func (t *terminal) ReadKey() (string, error) {
|
|
|
|
if t.in == nil {
|
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode an ANSI sequence from the input.
|
|
|
|
var d ansiDecoder
|
|
|
|
if err := d.decode(t.in); err != nil {
|
|
|
|
if errors.Is(err, cancelreader.ErrCanceled) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the decoded sequence into a key name.
|
|
|
|
//
|
|
|
|
// Some of these are described by ECMA-48, while others are described by the xterm or DEC docs:
|
|
|
|
// - https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf
|
|
|
|
// - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
|
|
|
// - https://vt100.net/docs/vt510-rm/contents.html
|
|
|
|
switch d.kind {
|
|
|
|
case ansiKey:
|
2023-03-06 17:06:48 +00:00
|
|
|
switch d.final {
|
2024-02-24 08:31:36 +00:00
|
|
|
case 2: // Ctrl+B --- Vim key for page up (page back)
|
|
|
|
return KeyPageUp, nil
|
2023-03-06 17:06:48 +00:00
|
|
|
case 3: // ETX
|
2023-03-06 14:56:54 +00:00
|
|
|
return KeyCtrlC, nil
|
2024-02-24 08:31:36 +00:00
|
|
|
case 6: // Ctrl+F ---- Vim key for page down (page forward)
|
|
|
|
return KeyPageDown, nil
|
2023-03-06 17:06:48 +00:00
|
|
|
case 15: // SI
|
|
|
|
return KeyCtrlO, nil
|
2023-03-06 14:56:54 +00:00
|
|
|
}
|
|
|
|
return string([]byte{d.final}), nil
|
|
|
|
case ansiEscape:
|
|
|
|
return fmt.Sprintf("<escape %v>", d.final), nil
|
|
|
|
case ansiControl:
|
|
|
|
switch d.final {
|
|
|
|
case 'A':
|
|
|
|
// CUU - Cursor Up: CSI (Pn) A
|
|
|
|
return KeyUp, nil
|
|
|
|
case 'B':
|
|
|
|
// CUD - Cursor Down: CSI (Pn) B
|
|
|
|
return KeyDown, nil
|
2024-02-24 08:31:36 +00:00
|
|
|
case 'F':
|
|
|
|
// Some terminals use CSI F for End, other use CSI 4 ~
|
|
|
|
// Historically this is the SCO mapping for a vt220
|
|
|
|
return KeyEnd, nil
|
|
|
|
case 'H':
|
|
|
|
// Some terminals use CSI H for home, other use CSI 1 ~
|
|
|
|
// in VT100 terms this is CUP (Pl; Pc) H
|
|
|
|
return KeyHome, nil
|
2023-03-06 14:56:54 +00:00
|
|
|
case '~':
|
|
|
|
// DECFNK - Function Key: CSI Ps1 (; Ps2) ~
|
|
|
|
switch string(d.params) {
|
2024-02-24 08:31:36 +00:00
|
|
|
case "1":
|
|
|
|
// Some terminals use CSI H for home, other use CSI 1 ~
|
|
|
|
// which is probably a reinterpretation of the DEC Find key
|
|
|
|
return KeyHome, nil
|
|
|
|
case "4":
|
|
|
|
// Some terminals use CSI F for End, other use CSI 4 ~
|
|
|
|
// which is probably a reinterpretation of the DEC Select key
|
|
|
|
return KeyEnd, nil
|
2023-03-06 14:56:54 +00:00
|
|
|
case "5":
|
|
|
|
// Page Up: CSI 5 ~
|
|
|
|
return KeyPageUp, nil
|
|
|
|
case "6":
|
|
|
|
// Page Down: CSI 6 ~
|
|
|
|
return KeyPageDown, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<control %v>", d.final), nil
|
turn on the golangci-lint exhaustive linter (#15028)
Turn on the golangci-lint exhaustive linter. This is the first step
towards catching more missing cases during development rather than
in tests, or in production.
This might be best reviewed commit-by-commit, as the first commit turns
on the linter with the `default-signifies-exhaustive: true` option set,
which requires a lot less changes in the current codebase.
I think it's probably worth doing the second commit as well, as that
will get us the real benefits, even though we end up with a little bit
more churn. However it means all the `switch` statements are covered,
which isn't the case after the first commit, since we do have a lot of
`default` statements that just call `assert.Fail`.
Fixes #14601
## Checklist
- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
- [x] I have formatted my code using `gofumpt`
<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!---
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-01-17 16:50:41 +00:00
|
|
|
case ansiError:
|
|
|
|
return "", errors.New("invalid ANSI sequence")
|
2023-03-06 14:56:54 +00:00
|
|
|
default:
|
|
|
|
return "", errors.New("invalid control sequence")
|
|
|
|
}
|
|
|
|
}
|