2012-03-30 08:27:01 +04:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-09-30 15:49:57 +03:00
|
|
|
package term
|
2012-03-30 08:27:01 +04:00
|
|
|
|
|
|
|
import (
|
2014-11-17 01:01:45 +03:00
|
|
|
"bytes"
|
2012-03-30 08:27:01 +04:00
|
|
|
"io"
|
2020-01-19 01:47:15 +03:00
|
|
|
"runtime"
|
2019-03-25 17:57:12 +03:00
|
|
|
"strconv"
|
2012-03-30 08:27:01 +04:00
|
|
|
"sync"
|
2013-09-13 22:33:00 +04:00
|
|
|
"unicode/utf8"
|
2012-03-30 08:27:01 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// EscapeCodes contains escape sequences that can be written to the terminal in
|
|
|
|
// order to achieve different styles of text.
|
|
|
|
type EscapeCodes struct {
|
|
|
|
// Foreground colors
|
|
|
|
Black, Red, Green, Yellow, Blue, Magenta, Cyan, White []byte
|
|
|
|
|
|
|
|
// Reset all attributes
|
|
|
|
Reset []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var vt100EscapeCodes = EscapeCodes{
|
|
|
|
Black: []byte{keyEscape, '[', '3', '0', 'm'},
|
|
|
|
Red: []byte{keyEscape, '[', '3', '1', 'm'},
|
|
|
|
Green: []byte{keyEscape, '[', '3', '2', 'm'},
|
|
|
|
Yellow: []byte{keyEscape, '[', '3', '3', 'm'},
|
|
|
|
Blue: []byte{keyEscape, '[', '3', '4', 'm'},
|
|
|
|
Magenta: []byte{keyEscape, '[', '3', '5', 'm'},
|
|
|
|
Cyan: []byte{keyEscape, '[', '3', '6', 'm'},
|
|
|
|
White: []byte{keyEscape, '[', '3', '7', 'm'},
|
|
|
|
|
|
|
|
Reset: []byte{keyEscape, '[', '0', 'm'},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminal contains the state for running a VT100 terminal that is capable of
|
|
|
|
// reading lines of input.
|
|
|
|
type Terminal struct {
|
2013-09-13 22:33:00 +04:00
|
|
|
// AutoCompleteCallback, if non-null, is called for each keypress with
|
|
|
|
// the full input line and the current position of the cursor (in
|
|
|
|
// bytes, as an index into |line|). If it returns ok=false, the key
|
|
|
|
// press is processed normally. Otherwise it returns a replacement line
|
|
|
|
// and the new cursor position.
|
|
|
|
AutoCompleteCallback func(line string, pos int, key rune) (newLine string, newPos int, ok bool)
|
2012-03-30 08:27:01 +04:00
|
|
|
|
|
|
|
// Escape contains a pointer to the escape codes for this terminal.
|
|
|
|
// It's always a valid pointer, although the escape codes themselves
|
|
|
|
// may be empty if the terminal doesn't support them.
|
|
|
|
Escape *EscapeCodes
|
|
|
|
|
|
|
|
// lock protects the terminal and the state in this object from
|
|
|
|
// concurrent processing of a key press and a Write() call.
|
|
|
|
lock sync.Mutex
|
|
|
|
|
|
|
|
c io.ReadWriter
|
2014-08-01 22:22:47 +04:00
|
|
|
prompt []rune
|
2012-03-30 08:27:01 +04:00
|
|
|
|
|
|
|
// line is the current line being entered.
|
2013-09-13 22:33:00 +04:00
|
|
|
line []rune
|
2012-03-30 08:27:01 +04:00
|
|
|
// pos is the logical position of the cursor in line
|
|
|
|
pos int
|
|
|
|
// echo is true if local echo is enabled
|
|
|
|
echo bool
|
2014-11-17 01:01:45 +03:00
|
|
|
// pasteActive is true iff there is a bracketed paste operation in
|
|
|
|
// progress.
|
|
|
|
pasteActive bool
|
2012-03-30 08:27:01 +04:00
|
|
|
|
|
|
|
// cursorX contains the current X value of the cursor where the left
|
|
|
|
// edge is 0. cursorY contains the row number where the first row of
|
|
|
|
// the current line is 0.
|
|
|
|
cursorX, cursorY int
|
|
|
|
// maxLine is the greatest value of cursorY so far.
|
|
|
|
maxLine int
|
|
|
|
|
|
|
|
termWidth, termHeight int
|
|
|
|
|
|
|
|
// outBuf contains the terminal data to be sent.
|
|
|
|
outBuf []byte
|
|
|
|
// remainder contains the remainder of any partial key sequences after
|
|
|
|
// a read. It aliases into inBuf.
|
|
|
|
remainder []byte
|
|
|
|
inBuf [256]byte
|
2013-06-07 18:21:53 +04:00
|
|
|
|
|
|
|
// history contains previously entered commands so that they can be
|
|
|
|
// accessed with the up and down keys.
|
|
|
|
history stRingBuffer
|
|
|
|
// historyIndex stores the currently accessed history entry, where zero
|
|
|
|
// means the immediately previous entry.
|
|
|
|
historyIndex int
|
|
|
|
// When navigating up and down the history it's possible to return to
|
|
|
|
// the incomplete, initial line. That value is stored in
|
|
|
|
// historyPending.
|
|
|
|
historyPending string
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is
|
|
|
|
// a local terminal, that terminal must first have been put into raw mode.
|
|
|
|
// prompt is a string that is written at the start of each input line (i.e.
|
|
|
|
// "> ").
|
|
|
|
func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
|
|
|
|
return &Terminal{
|
2013-06-07 18:21:53 +04:00
|
|
|
Escape: &vt100EscapeCodes,
|
|
|
|
c: c,
|
2014-08-01 22:22:47 +04:00
|
|
|
prompt: []rune(prompt),
|
2013-06-07 18:21:53 +04:00
|
|
|
termWidth: 80,
|
|
|
|
termHeight: 24,
|
|
|
|
echo: true,
|
|
|
|
historyIndex: -1,
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-04-14 18:39:45 +03:00
|
|
|
keyCtrlC = 3
|
2012-03-30 08:27:01 +04:00
|
|
|
keyCtrlD = 4
|
2014-05-28 06:45:07 +04:00
|
|
|
keyCtrlU = 21
|
2012-03-30 08:27:01 +04:00
|
|
|
keyEnter = '\r'
|
|
|
|
keyEscape = 27
|
|
|
|
keyBackspace = 127
|
2013-09-13 22:33:00 +04:00
|
|
|
keyUnknown = 0xd800 /* UTF-16 surrogate area */ + iota
|
2012-03-30 08:27:01 +04:00
|
|
|
keyUp
|
|
|
|
keyDown
|
|
|
|
keyLeft
|
|
|
|
keyRight
|
|
|
|
keyAltLeft
|
|
|
|
keyAltRight
|
2013-06-07 18:21:53 +04:00
|
|
|
keyHome
|
|
|
|
keyEnd
|
2013-08-28 15:36:04 +04:00
|
|
|
keyDeleteWord
|
|
|
|
keyDeleteLine
|
2014-05-28 06:45:07 +04:00
|
|
|
keyClearScreen
|
2014-11-17 01:01:45 +03:00
|
|
|
keyPasteStart
|
|
|
|
keyPasteEnd
|
2012-03-30 08:27:01 +04:00
|
|
|
)
|
|
|
|
|
2016-12-05 20:23:45 +03:00
|
|
|
var (
|
|
|
|
crlf = []byte{'\r', '\n'}
|
|
|
|
pasteStart = []byte{keyEscape, '[', '2', '0', '0', '~'}
|
|
|
|
pasteEnd = []byte{keyEscape, '[', '2', '0', '1', '~'}
|
|
|
|
)
|
2014-11-17 01:01:45 +03:00
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
// bytesToKey tries to parse a key sequence from b. If successful, it returns
|
2013-09-13 22:33:00 +04:00
|
|
|
// the key and the remainder of the input. Otherwise it returns utf8.RuneError.
|
2014-11-17 01:01:45 +03:00
|
|
|
func bytesToKey(b []byte, pasteActive bool) (rune, []byte) {
|
2012-03-30 08:27:01 +04:00
|
|
|
if len(b) == 0 {
|
2013-09-13 22:33:00 +04:00
|
|
|
return utf8.RuneError, nil
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
if !pasteActive {
|
|
|
|
switch b[0] {
|
|
|
|
case 1: // ^A
|
|
|
|
return keyHome, b[1:]
|
2020-04-14 18:39:45 +03:00
|
|
|
case 2: // ^B
|
|
|
|
return keyLeft, b[1:]
|
2014-11-17 01:01:45 +03:00
|
|
|
case 5: // ^E
|
|
|
|
return keyEnd, b[1:]
|
2020-04-14 18:39:45 +03:00
|
|
|
case 6: // ^F
|
|
|
|
return keyRight, b[1:]
|
2014-11-17 01:01:45 +03:00
|
|
|
case 8: // ^H
|
|
|
|
return keyBackspace, b[1:]
|
|
|
|
case 11: // ^K
|
|
|
|
return keyDeleteLine, b[1:]
|
|
|
|
case 12: // ^L
|
|
|
|
return keyClearScreen, b[1:]
|
|
|
|
case 23: // ^W
|
|
|
|
return keyDeleteWord, b[1:]
|
2016-11-30 01:27:05 +03:00
|
|
|
case 14: // ^N
|
|
|
|
return keyDown, b[1:]
|
|
|
|
case 16: // ^P
|
|
|
|
return keyUp, b[1:]
|
2014-11-17 01:01:45 +03:00
|
|
|
}
|
2013-08-28 15:36:04 +04:00
|
|
|
}
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
if b[0] != keyEscape {
|
2013-09-13 22:33:00 +04:00
|
|
|
if !utf8.FullRune(b) {
|
|
|
|
return utf8.RuneError, b
|
|
|
|
}
|
|
|
|
r, l := utf8.DecodeRune(b)
|
|
|
|
return r, b[l:]
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
if !pasteActive && len(b) >= 3 && b[0] == keyEscape && b[1] == '[' {
|
2012-03-30 08:27:01 +04:00
|
|
|
switch b[2] {
|
|
|
|
case 'A':
|
|
|
|
return keyUp, b[3:]
|
|
|
|
case 'B':
|
|
|
|
return keyDown, b[3:]
|
|
|
|
case 'C':
|
|
|
|
return keyRight, b[3:]
|
|
|
|
case 'D':
|
|
|
|
return keyLeft, b[3:]
|
2013-06-07 18:21:53 +04:00
|
|
|
case 'H':
|
|
|
|
return keyHome, b[3:]
|
|
|
|
case 'F':
|
|
|
|
return keyEnd, b[3:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
if !pasteActive && len(b) >= 6 && b[0] == keyEscape && b[1] == '[' && b[2] == '1' && b[3] == ';' && b[4] == '3' {
|
2012-03-30 08:27:01 +04:00
|
|
|
switch b[5] {
|
|
|
|
case 'C':
|
|
|
|
return keyAltRight, b[6:]
|
|
|
|
case 'D':
|
|
|
|
return keyAltLeft, b[6:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
if !pasteActive && len(b) >= 6 && bytes.Equal(b[:6], pasteStart) {
|
|
|
|
return keyPasteStart, b[6:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if pasteActive && len(b) >= 6 && bytes.Equal(b[:6], pasteEnd) {
|
|
|
|
return keyPasteEnd, b[6:]
|
|
|
|
}
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
// If we get here then we have a key that we don't recognise, or a
|
|
|
|
// partial sequence. It's not clear how one should find the end of a
|
2014-11-17 01:01:45 +03:00
|
|
|
// sequence without knowing them all, but it seems that [a-zA-Z~] only
|
2012-03-30 08:27:01 +04:00
|
|
|
// appears at the end of a sequence.
|
|
|
|
for i, c := range b[0:] {
|
2014-11-17 01:01:45 +03:00
|
|
|
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '~' {
|
2012-03-30 08:27:01 +04:00
|
|
|
return keyUnknown, b[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
return utf8.RuneError, b
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// queue appends data to the end of t.outBuf
|
2013-09-13 22:33:00 +04:00
|
|
|
func (t *Terminal) queue(data []rune) {
|
|
|
|
t.outBuf = append(t.outBuf, []byte(string(data))...)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
var space = []rune{' '}
|
2012-03-30 08:27:01 +04:00
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
func isPrintable(key rune) bool {
|
|
|
|
isInSurrogateArea := key >= 0xd800 && key <= 0xdbff
|
|
|
|
return key >= 32 && !isInSurrogateArea
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// moveCursorToPos appends data to t.outBuf which will move the cursor to the
|
|
|
|
// given, logical position in the text.
|
|
|
|
func (t *Terminal) moveCursorToPos(pos int) {
|
|
|
|
if !t.echo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
x := visualLength(t.prompt) + pos
|
2012-03-30 08:27:01 +04:00
|
|
|
y := x / t.termWidth
|
|
|
|
x = x % t.termWidth
|
|
|
|
|
|
|
|
up := 0
|
|
|
|
if y < t.cursorY {
|
|
|
|
up = t.cursorY - y
|
|
|
|
}
|
|
|
|
|
|
|
|
down := 0
|
|
|
|
if y > t.cursorY {
|
|
|
|
down = y - t.cursorY
|
|
|
|
}
|
|
|
|
|
|
|
|
left := 0
|
|
|
|
if x < t.cursorX {
|
|
|
|
left = t.cursorX - x
|
|
|
|
}
|
|
|
|
|
|
|
|
right := 0
|
|
|
|
if x > t.cursorX {
|
|
|
|
right = x - t.cursorX
|
|
|
|
}
|
|
|
|
|
|
|
|
t.cursorX = x
|
|
|
|
t.cursorY = y
|
|
|
|
t.move(up, down, left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) move(up, down, left, right int) {
|
2019-03-25 17:57:12 +03:00
|
|
|
m := []rune{}
|
|
|
|
|
|
|
|
// 1 unit up can be expressed as ^[[A or ^[A
|
|
|
|
// 5 units up can be expressed as ^[[5A
|
|
|
|
|
|
|
|
if up == 1 {
|
|
|
|
m = append(m, keyEscape, '[', 'A')
|
|
|
|
} else if up > 1 {
|
|
|
|
m = append(m, keyEscape, '[')
|
|
|
|
m = append(m, []rune(strconv.Itoa(up))...)
|
|
|
|
m = append(m, 'A')
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
2019-03-25 17:57:12 +03:00
|
|
|
|
|
|
|
if down == 1 {
|
|
|
|
m = append(m, keyEscape, '[', 'B')
|
|
|
|
} else if down > 1 {
|
|
|
|
m = append(m, keyEscape, '[')
|
|
|
|
m = append(m, []rune(strconv.Itoa(down))...)
|
|
|
|
m = append(m, 'B')
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
2019-03-25 17:57:12 +03:00
|
|
|
|
|
|
|
if right == 1 {
|
|
|
|
m = append(m, keyEscape, '[', 'C')
|
|
|
|
} else if right > 1 {
|
|
|
|
m = append(m, keyEscape, '[')
|
|
|
|
m = append(m, []rune(strconv.Itoa(right))...)
|
|
|
|
m = append(m, 'C')
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
2019-03-25 17:57:12 +03:00
|
|
|
|
|
|
|
if left == 1 {
|
|
|
|
m = append(m, keyEscape, '[', 'D')
|
|
|
|
} else if left > 1 {
|
|
|
|
m = append(m, keyEscape, '[')
|
|
|
|
m = append(m, []rune(strconv.Itoa(left))...)
|
|
|
|
m = append(m, 'D')
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:57:12 +03:00
|
|
|
t.queue(m)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) clearLineToRight() {
|
2013-09-13 22:33:00 +04:00
|
|
|
op := []rune{keyEscape, '[', 'K'}
|
2012-03-30 08:27:01 +04:00
|
|
|
t.queue(op)
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxLineLength = 4096
|
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
func (t *Terminal) setLine(newLine []rune, newPos int) {
|
2013-06-07 18:21:53 +04:00
|
|
|
if t.echo {
|
|
|
|
t.moveCursorToPos(0)
|
|
|
|
t.writeLine(newLine)
|
|
|
|
for i := len(newLine); i < len(t.line); i++ {
|
|
|
|
t.writeLine(space)
|
|
|
|
}
|
|
|
|
t.moveCursorToPos(newPos)
|
|
|
|
}
|
|
|
|
t.line = newLine
|
|
|
|
t.pos = newPos
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
func (t *Terminal) advanceCursor(places int) {
|
|
|
|
t.cursorX += places
|
|
|
|
t.cursorY += t.cursorX / t.termWidth
|
|
|
|
if t.cursorY > t.maxLine {
|
|
|
|
t.maxLine = t.cursorY
|
|
|
|
}
|
|
|
|
t.cursorX = t.cursorX % t.termWidth
|
|
|
|
|
|
|
|
if places > 0 && t.cursorX == 0 {
|
|
|
|
// Normally terminals will advance the current position
|
|
|
|
// when writing a character. But that doesn't happen
|
|
|
|
// for the last character in a line. However, when
|
|
|
|
// writing a character (except a new line) that causes
|
|
|
|
// a line wrap, the position will be advanced two
|
|
|
|
// places.
|
|
|
|
//
|
|
|
|
// So, if we are stopping at the end of a line, we
|
|
|
|
// need to write a newline so that our cursor can be
|
|
|
|
// advanced to the next line.
|
2016-12-05 20:23:45 +03:00
|
|
|
t.outBuf = append(t.outBuf, '\r', '\n')
|
2014-08-01 22:22:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 15:36:04 +04:00
|
|
|
func (t *Terminal) eraseNPreviousChars(n int) {
|
|
|
|
if n == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.pos < n {
|
|
|
|
n = t.pos
|
|
|
|
}
|
|
|
|
t.pos -= n
|
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
|
|
|
|
copy(t.line[t.pos:], t.line[n+t.pos:])
|
|
|
|
t.line = t.line[:len(t.line)-n]
|
|
|
|
if t.echo {
|
|
|
|
t.writeLine(t.line[t.pos:])
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
t.queue(space)
|
|
|
|
}
|
2014-08-01 22:22:47 +04:00
|
|
|
t.advanceCursor(n)
|
2013-08-28 15:36:04 +04:00
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// countToLeftWord returns then number of characters from the cursor to the
|
|
|
|
// start of the previous word.
|
|
|
|
func (t *Terminal) countToLeftWord() int {
|
|
|
|
if t.pos == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := t.pos - 1
|
|
|
|
for pos > 0 {
|
|
|
|
if t.line[pos] != ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pos--
|
|
|
|
}
|
|
|
|
for pos > 0 {
|
|
|
|
if t.line[pos] == ' ' {
|
|
|
|
pos++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pos--
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.pos - pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// countToRightWord returns then number of characters from the cursor to the
|
|
|
|
// start of the next word.
|
|
|
|
func (t *Terminal) countToRightWord() int {
|
|
|
|
pos := t.pos
|
|
|
|
for pos < len(t.line) {
|
|
|
|
if t.line[pos] == ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pos++
|
|
|
|
}
|
|
|
|
for pos < len(t.line) {
|
|
|
|
if t.line[pos] != ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pos++
|
|
|
|
}
|
|
|
|
return pos - t.pos
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
// visualLength returns the number of visible glyphs in s.
|
|
|
|
func visualLength(runes []rune) int {
|
|
|
|
inEscapeSeq := false
|
|
|
|
length := 0
|
|
|
|
|
|
|
|
for _, r := range runes {
|
|
|
|
switch {
|
|
|
|
case inEscapeSeq:
|
|
|
|
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
|
|
|
|
inEscapeSeq = false
|
|
|
|
}
|
|
|
|
case r == '\x1b':
|
|
|
|
inEscapeSeq = true
|
|
|
|
default:
|
|
|
|
length++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
// handleKey processes the given key and, optionally, returns a line of text
|
|
|
|
// that the user has entered.
|
2013-09-13 22:33:00 +04:00
|
|
|
func (t *Terminal) handleKey(key rune) (line string, ok bool) {
|
2014-11-17 01:01:45 +03:00
|
|
|
if t.pasteActive && key != keyEnter {
|
|
|
|
t.addKeyToLine(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
switch key {
|
|
|
|
case keyBackspace:
|
|
|
|
if t.pos == 0 {
|
|
|
|
return
|
|
|
|
}
|
2013-08-28 15:36:04 +04:00
|
|
|
t.eraseNPreviousChars(1)
|
2012-03-30 08:27:01 +04:00
|
|
|
case keyAltLeft:
|
|
|
|
// move left by a word.
|
2013-08-28 15:36:04 +04:00
|
|
|
t.pos -= t.countToLeftWord()
|
2012-03-30 08:27:01 +04:00
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
case keyAltRight:
|
|
|
|
// move right by a word.
|
2013-08-28 15:36:04 +04:00
|
|
|
t.pos += t.countToRightWord()
|
2012-03-30 08:27:01 +04:00
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
case keyLeft:
|
|
|
|
if t.pos == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.pos--
|
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
case keyRight:
|
|
|
|
if t.pos == len(t.line) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.pos++
|
|
|
|
t.moveCursorToPos(t.pos)
|
2013-06-07 18:21:53 +04:00
|
|
|
case keyHome:
|
|
|
|
if t.pos == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.pos = 0
|
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
case keyEnd:
|
|
|
|
if t.pos == len(t.line) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.pos = len(t.line)
|
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
case keyUp:
|
|
|
|
entry, ok := t.history.NthPreviousEntry(t.historyIndex + 1)
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if t.historyIndex == -1 {
|
|
|
|
t.historyPending = string(t.line)
|
|
|
|
}
|
|
|
|
t.historyIndex++
|
2013-09-15 20:48:02 +04:00
|
|
|
runes := []rune(entry)
|
|
|
|
t.setLine(runes, len(runes))
|
2013-06-07 18:21:53 +04:00
|
|
|
case keyDown:
|
|
|
|
switch t.historyIndex {
|
|
|
|
case -1:
|
|
|
|
return
|
|
|
|
case 0:
|
2013-09-15 20:48:02 +04:00
|
|
|
runes := []rune(t.historyPending)
|
|
|
|
t.setLine(runes, len(runes))
|
2013-06-07 18:21:53 +04:00
|
|
|
t.historyIndex--
|
|
|
|
default:
|
|
|
|
entry, ok := t.history.NthPreviousEntry(t.historyIndex - 1)
|
|
|
|
if ok {
|
|
|
|
t.historyIndex--
|
2013-09-15 20:48:02 +04:00
|
|
|
runes := []rune(entry)
|
|
|
|
t.setLine(runes, len(runes))
|
2013-06-07 18:21:53 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-30 08:27:01 +04:00
|
|
|
case keyEnter:
|
|
|
|
t.moveCursorToPos(len(t.line))
|
2013-09-13 22:33:00 +04:00
|
|
|
t.queue([]rune("\r\n"))
|
2012-03-30 08:27:01 +04:00
|
|
|
line = string(t.line)
|
|
|
|
ok = true
|
|
|
|
t.line = t.line[:0]
|
|
|
|
t.pos = 0
|
|
|
|
t.cursorX = 0
|
|
|
|
t.cursorY = 0
|
|
|
|
t.maxLine = 0
|
2013-08-28 15:36:04 +04:00
|
|
|
case keyDeleteWord:
|
|
|
|
// Delete zero or more spaces and then one or more characters.
|
|
|
|
t.eraseNPreviousChars(t.countToLeftWord())
|
|
|
|
case keyDeleteLine:
|
|
|
|
// Delete everything from the current cursor position to the
|
|
|
|
// end of line.
|
|
|
|
for i := t.pos; i < len(t.line); i++ {
|
|
|
|
t.queue(space)
|
2014-08-01 22:22:47 +04:00
|
|
|
t.advanceCursor(1)
|
2013-08-28 15:36:04 +04:00
|
|
|
}
|
|
|
|
t.line = t.line[:t.pos]
|
|
|
|
t.moveCursorToPos(t.pos)
|
2014-05-28 06:45:07 +04:00
|
|
|
case keyCtrlD:
|
|
|
|
// Erase the character under the current position.
|
|
|
|
// The EOF case when the line is empty is handled in
|
|
|
|
// readLine().
|
|
|
|
if t.pos < len(t.line) {
|
|
|
|
t.pos++
|
|
|
|
t.eraseNPreviousChars(1)
|
|
|
|
}
|
|
|
|
case keyCtrlU:
|
|
|
|
t.eraseNPreviousChars(t.pos)
|
|
|
|
case keyClearScreen:
|
|
|
|
// Erases the screen and moves the cursor to the home position.
|
|
|
|
t.queue([]rune("\x1b[2J\x1b[H"))
|
2014-08-01 22:22:47 +04:00
|
|
|
t.queue(t.prompt)
|
|
|
|
t.cursorX, t.cursorY = 0, 0
|
|
|
|
t.advanceCursor(visualLength(t.prompt))
|
2014-05-28 06:45:07 +04:00
|
|
|
t.setLine(t.line, t.pos)
|
2012-03-30 08:27:01 +04:00
|
|
|
default:
|
|
|
|
if t.AutoCompleteCallback != nil {
|
2013-09-13 22:33:00 +04:00
|
|
|
prefix := string(t.line[:t.pos])
|
|
|
|
suffix := string(t.line[t.pos:])
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
t.lock.Unlock()
|
2013-09-13 22:33:00 +04:00
|
|
|
newLine, newPos, completeOk := t.AutoCompleteCallback(prefix+suffix, len(prefix), key)
|
2012-03-30 08:27:01 +04:00
|
|
|
t.lock.Lock()
|
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
if completeOk {
|
|
|
|
t.setLine([]rune(newLine), utf8.RuneCount([]byte(newLine)[:newPos]))
|
2012-03-30 08:27:01 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isPrintable(key) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(t.line) == maxLineLength {
|
|
|
|
return
|
|
|
|
}
|
2014-11-17 01:01:45 +03:00
|
|
|
t.addKeyToLine(key)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
// addKeyToLine inserts the given key at the current position in the current
|
|
|
|
// line.
|
|
|
|
func (t *Terminal) addKeyToLine(key rune) {
|
|
|
|
if len(t.line) == cap(t.line) {
|
|
|
|
newLine := make([]rune, len(t.line), 2*(1+len(t.line)))
|
|
|
|
copy(newLine, t.line)
|
|
|
|
t.line = newLine
|
|
|
|
}
|
|
|
|
t.line = t.line[:len(t.line)+1]
|
|
|
|
copy(t.line[t.pos+1:], t.line[t.pos:])
|
|
|
|
t.line[t.pos] = key
|
|
|
|
if t.echo {
|
|
|
|
t.writeLine(t.line[t.pos:])
|
|
|
|
}
|
|
|
|
t.pos++
|
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
}
|
|
|
|
|
2013-09-13 22:33:00 +04:00
|
|
|
func (t *Terminal) writeLine(line []rune) {
|
2012-03-30 08:27:01 +04:00
|
|
|
for len(line) != 0 {
|
|
|
|
remainingOnLine := t.termWidth - t.cursorX
|
|
|
|
todo := len(line)
|
|
|
|
if todo > remainingOnLine {
|
|
|
|
todo = remainingOnLine
|
|
|
|
}
|
|
|
|
t.queue(line[:todo])
|
2014-08-01 22:22:47 +04:00
|
|
|
t.advanceCursor(visualLength(line[:todo]))
|
2012-03-30 08:27:01 +04:00
|
|
|
line = line[todo:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 00:06:48 +03:00
|
|
|
// writeWithCRLF writes buf to w but replaces all occurrences of \n with \r\n.
|
2016-12-05 20:23:45 +03:00
|
|
|
func writeWithCRLF(w io.Writer, buf []byte) (n int, err error) {
|
|
|
|
for len(buf) > 0 {
|
|
|
|
i := bytes.IndexByte(buf, '\n')
|
|
|
|
todo := len(buf)
|
|
|
|
if i >= 0 {
|
|
|
|
todo = i
|
|
|
|
}
|
|
|
|
|
|
|
|
var nn int
|
|
|
|
nn, err = w.Write(buf[:todo])
|
|
|
|
n += nn
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
buf = buf[todo:]
|
|
|
|
|
|
|
|
if i >= 0 {
|
|
|
|
if _, err = w.Write(crlf); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2017-11-28 07:39:32 +03:00
|
|
|
n++
|
2016-12-05 20:23:45 +03:00
|
|
|
buf = buf[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
func (t *Terminal) Write(buf []byte) (n int, err error) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
if t.cursorX == 0 && t.cursorY == 0 {
|
|
|
|
// This is the easy case: there's nothing on the screen that we
|
|
|
|
// have to move out of the way.
|
2016-12-05 20:23:45 +03:00
|
|
|
return writeWithCRLF(t.c, buf)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have a prompt and possibly user input on the screen. We
|
|
|
|
// have to clear it first.
|
|
|
|
t.move(0 /* up */, 0 /* down */, t.cursorX /* left */, 0 /* right */)
|
|
|
|
t.cursorX = 0
|
|
|
|
t.clearLineToRight()
|
|
|
|
|
|
|
|
for t.cursorY > 0 {
|
|
|
|
t.move(1 /* up */, 0, 0, 0)
|
|
|
|
t.cursorY--
|
|
|
|
t.clearLineToRight()
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = t.c.Write(t.outBuf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.outBuf = t.outBuf[:0]
|
|
|
|
|
2016-12-05 20:23:45 +03:00
|
|
|
if n, err = writeWithCRLF(t.c, buf); err != nil {
|
2012-03-30 08:27:01 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
t.writeLine(t.prompt)
|
2012-03-30 08:27:01 +04:00
|
|
|
if t.echo {
|
2014-08-01 22:22:47 +04:00
|
|
|
t.writeLine(t.line)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
2014-08-01 22:22:47 +04:00
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
t.moveCursorToPos(t.pos)
|
|
|
|
|
|
|
|
if _, err = t.c.Write(t.outBuf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.outBuf = t.outBuf[:0]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPassword temporarily changes the prompt and reads a password, without
|
|
|
|
// echo, from the terminal.
|
|
|
|
func (t *Terminal) ReadPassword(prompt string) (line string, err error) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
oldPrompt := t.prompt
|
2014-08-01 22:22:47 +04:00
|
|
|
t.prompt = []rune(prompt)
|
2012-03-30 08:27:01 +04:00
|
|
|
t.echo = false
|
|
|
|
|
|
|
|
line, err = t.readLine()
|
|
|
|
|
|
|
|
t.prompt = oldPrompt
|
|
|
|
t.echo = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLine returns a line of input from the terminal.
|
|
|
|
func (t *Terminal) ReadLine() (line string, err error) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
return t.readLine()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) readLine() (line string, err error) {
|
|
|
|
// t.lock must be held at this point
|
|
|
|
|
|
|
|
if t.cursorX == 0 && t.cursorY == 0 {
|
2014-08-01 22:22:47 +04:00
|
|
|
t.writeLine(t.prompt)
|
2012-03-30 08:27:01 +04:00
|
|
|
t.c.Write(t.outBuf)
|
|
|
|
t.outBuf = t.outBuf[:0]
|
|
|
|
}
|
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
lineIsPasted := t.pasteActive
|
|
|
|
|
2012-03-30 08:27:01 +04:00
|
|
|
for {
|
|
|
|
rest := t.remainder
|
|
|
|
lineOk := false
|
|
|
|
for !lineOk {
|
2013-09-13 22:33:00 +04:00
|
|
|
var key rune
|
2014-11-17 01:01:45 +03:00
|
|
|
key, rest = bytesToKey(rest, t.pasteActive)
|
2013-09-13 22:33:00 +04:00
|
|
|
if key == utf8.RuneError {
|
2012-03-30 08:27:01 +04:00
|
|
|
break
|
|
|
|
}
|
2014-11-17 01:01:45 +03:00
|
|
|
if !t.pasteActive {
|
|
|
|
if key == keyCtrlD {
|
|
|
|
if len(t.line) == 0 {
|
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 18:39:45 +03:00
|
|
|
if key == keyCtrlC {
|
|
|
|
return "", io.EOF
|
|
|
|
}
|
2014-11-17 01:01:45 +03:00
|
|
|
if key == keyPasteStart {
|
|
|
|
t.pasteActive = true
|
|
|
|
if len(t.line) == 0 {
|
|
|
|
lineIsPasted = true
|
|
|
|
}
|
|
|
|
continue
|
2014-05-28 06:45:07 +04:00
|
|
|
}
|
2014-11-17 01:01:45 +03:00
|
|
|
} else if key == keyPasteEnd {
|
|
|
|
t.pasteActive = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !t.pasteActive {
|
|
|
|
lineIsPasted = false
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
line, lineOk = t.handleKey(key)
|
|
|
|
}
|
|
|
|
if len(rest) > 0 {
|
|
|
|
n := copy(t.inBuf[:], rest)
|
|
|
|
t.remainder = t.inBuf[:n]
|
|
|
|
} else {
|
|
|
|
t.remainder = nil
|
|
|
|
}
|
|
|
|
t.c.Write(t.outBuf)
|
|
|
|
t.outBuf = t.outBuf[:0]
|
|
|
|
if lineOk {
|
2013-07-03 03:46:13 +04:00
|
|
|
if t.echo {
|
|
|
|
t.historyIndex = -1
|
|
|
|
t.history.Add(line)
|
|
|
|
}
|
2014-11-17 01:01:45 +03:00
|
|
|
if lineIsPasted {
|
|
|
|
err = ErrPasteIndicator
|
|
|
|
}
|
2012-03-30 08:27:01 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// t.remainder is a slice at the beginning of t.inBuf
|
|
|
|
// containing a partial key sequence
|
|
|
|
readBuf := t.inBuf[len(t.remainder):]
|
|
|
|
var n int
|
|
|
|
|
|
|
|
t.lock.Unlock()
|
|
|
|
n, err = t.c.Read(readBuf)
|
|
|
|
t.lock.Lock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.remainder = t.inBuf[:n+len(t.remainder)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrompt sets the prompt to be used when reading subsequent lines.
|
|
|
|
func (t *Terminal) SetPrompt(prompt string) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
t.prompt = []rune(prompt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) clearAndRepaintLinePlusNPrevious(numPrevLines int) {
|
|
|
|
// Move cursor to column zero at the start of the line.
|
|
|
|
t.move(t.cursorY, 0, t.cursorX, 0)
|
|
|
|
t.cursorX, t.cursorY = 0, 0
|
|
|
|
t.clearLineToRight()
|
|
|
|
for t.cursorY < numPrevLines {
|
|
|
|
// Move down a line
|
|
|
|
t.move(0, 1, 0, 0)
|
|
|
|
t.cursorY++
|
|
|
|
t.clearLineToRight()
|
|
|
|
}
|
|
|
|
// Move back to beginning.
|
|
|
|
t.move(t.cursorY, 0, 0, 0)
|
|
|
|
t.cursorX, t.cursorY = 0, 0
|
|
|
|
|
|
|
|
t.queue(t.prompt)
|
|
|
|
t.advanceCursor(visualLength(t.prompt))
|
|
|
|
t.writeLine(t.line)
|
|
|
|
t.moveCursorToPos(t.pos)
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
func (t *Terminal) SetSize(width, height int) error {
|
2012-03-30 08:27:01 +04:00
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2014-10-25 22:16:08 +04:00
|
|
|
if width == 0 {
|
|
|
|
width = 1
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:22:47 +04:00
|
|
|
oldWidth := t.termWidth
|
2012-03-30 08:27:01 +04:00
|
|
|
t.termWidth, t.termHeight = width, height
|
2014-08-01 22:22:47 +04:00
|
|
|
|
|
|
|
switch {
|
2014-10-25 22:16:08 +04:00
|
|
|
case width == oldWidth:
|
2014-08-01 22:22:47 +04:00
|
|
|
// If the width didn't change then nothing else needs to be
|
|
|
|
// done.
|
|
|
|
return nil
|
ssh/terminal: fix SetSize when nothing on current line
SetSize has a problem may cause the following ReadPassword setting
temporary prompt not working, when changing width the current
SetSize will call clearAndRepaintLinePlusNPrevious which would
print an old prompt whatever the current line has, causing a following
ReadPassword with temporary prompt not printing the different prompt.
When running code like this, the nt.SetSize prints a "> " as
prompt then the temporary "Password: " prompt would never show up.
```go
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
width, height, _ = terminal.GetSize(int(os.Stdin.Fd()))
nt := terminal.NewTerminal(os.Stdin, "> ")
nt.SetSize(width, height)
password, err = nt.ReadPassword("Password: ")
```
the new test cases is to test SetSize with different terminal sizes,
either shrinking or expanding, a following ReadPassword should get the
correct temporary prompt.
Change-Id: I33d13b2c732997c0c88670d53545b8c0048b94b6
Reviewed-on: https://go-review.googlesource.com/1861
Reviewed-by: Adam Langley <agl@golang.org>
2014-12-19 08:22:47 +03:00
|
|
|
case len(t.line) == 0 && t.cursorX == 0 && t.cursorY == 0:
|
|
|
|
// If there is nothing on current line and no prompt printed,
|
|
|
|
// just do nothing
|
|
|
|
return nil
|
2014-08-01 22:22:47 +04:00
|
|
|
case width < oldWidth:
|
|
|
|
// Some terminals (e.g. xterm) will truncate lines that were
|
|
|
|
// too long when shinking. Others, (e.g. gnome-terminal) will
|
|
|
|
// attempt to wrap them. For the former, repainting t.maxLine
|
|
|
|
// works great, but that behaviour goes badly wrong in the case
|
|
|
|
// of the latter because they have doubled every full line.
|
|
|
|
|
|
|
|
// We assume that we are working on a terminal that wraps lines
|
|
|
|
// and adjust the cursor position based on every previous line
|
|
|
|
// wrapping and turning into two. This causes the prompt on
|
|
|
|
// xterms to move upwards, which isn't great, but it avoids a
|
|
|
|
// huge mess with gnome-terminal.
|
2014-10-25 22:16:08 +04:00
|
|
|
if t.cursorX >= t.termWidth {
|
|
|
|
t.cursorX = t.termWidth - 1
|
|
|
|
}
|
2014-08-01 22:22:47 +04:00
|
|
|
t.cursorY *= 2
|
|
|
|
t.clearAndRepaintLinePlusNPrevious(t.maxLine * 2)
|
|
|
|
case width > oldWidth:
|
|
|
|
// If the terminal expands then our position calculations will
|
|
|
|
// be wrong in the future because we think the cursor is
|
|
|
|
// |t.pos| chars into the string, but there will be a gap at
|
|
|
|
// the end of any wrapped line.
|
|
|
|
//
|
|
|
|
// But the position will actually be correct until we move, so
|
|
|
|
// we can move back to the beginning and repaint everything.
|
|
|
|
t.clearAndRepaintLinePlusNPrevious(t.maxLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := t.c.Write(t.outBuf)
|
|
|
|
t.outBuf = t.outBuf[:0]
|
|
|
|
return err
|
2012-03-30 08:27:01 +04:00
|
|
|
}
|
2013-06-07 18:21:53 +04:00
|
|
|
|
2014-11-17 01:01:45 +03:00
|
|
|
type pasteIndicatorError struct{}
|
|
|
|
|
|
|
|
func (pasteIndicatorError) Error() string {
|
|
|
|
return "terminal: ErrPasteIndicator not correctly handled"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrPasteIndicator may be returned from ReadLine as the error, in addition
|
|
|
|
// to valid line data. It indicates that bracketed paste mode is enabled and
|
|
|
|
// that the returned line consists only of pasted data. Programs may wish to
|
|
|
|
// interpret pasted data more literally than typed data.
|
|
|
|
var ErrPasteIndicator = pasteIndicatorError{}
|
|
|
|
|
|
|
|
// SetBracketedPasteMode requests that the terminal bracket paste operations
|
|
|
|
// with markers. Not all terminals support this but, if it is supported, then
|
|
|
|
// enabling this mode will stop any autocomplete callback from running due to
|
|
|
|
// pastes. Additionally, any lines that are completely pasted will be returned
|
|
|
|
// from ReadLine with the error set to ErrPasteIndicator.
|
|
|
|
func (t *Terminal) SetBracketedPasteMode(on bool) {
|
|
|
|
if on {
|
|
|
|
io.WriteString(t.c, "\x1b[?2004h")
|
|
|
|
} else {
|
|
|
|
io.WriteString(t.c, "\x1b[?2004l")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:21:53 +04:00
|
|
|
// stRingBuffer is a ring buffer of strings.
|
|
|
|
type stRingBuffer struct {
|
|
|
|
// entries contains max elements.
|
|
|
|
entries []string
|
|
|
|
max int
|
|
|
|
// head contains the index of the element most recently added to the ring.
|
|
|
|
head int
|
|
|
|
// size contains the number of elements in the ring.
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stRingBuffer) Add(a string) {
|
|
|
|
if s.entries == nil {
|
|
|
|
const defaultNumEntries = 100
|
|
|
|
s.entries = make([]string, defaultNumEntries)
|
|
|
|
s.max = defaultNumEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
s.head = (s.head + 1) % s.max
|
|
|
|
s.entries[s.head] = a
|
|
|
|
if s.size < s.max {
|
|
|
|
s.size++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NthPreviousEntry returns the value passed to the nth previous call to Add.
|
|
|
|
// If n is zero then the immediately prior value is returned, if one, then the
|
|
|
|
// next most recent, and so on. If such an element doesn't exist then ok is
|
|
|
|
// false.
|
|
|
|
func (s *stRingBuffer) NthPreviousEntry(n int) (value string, ok bool) {
|
2022-05-26 00:39:43 +03:00
|
|
|
if n < 0 || n >= s.size {
|
2013-06-07 18:21:53 +04:00
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
index := s.head - n
|
|
|
|
if index < 0 {
|
|
|
|
index += s.max
|
|
|
|
}
|
|
|
|
return s.entries[index], true
|
|
|
|
}
|
2016-08-02 05:34:46 +03:00
|
|
|
|
|
|
|
// readPasswordLine reads from reader until it finds \n or io.EOF.
|
|
|
|
// The slice returned does not include the \n.
|
|
|
|
// readPasswordLine also ignores any \r it finds.
|
2020-01-19 01:47:15 +03:00
|
|
|
// Windows uses \r as end of line. So, on Windows, readPasswordLine
|
|
|
|
// reads until it finds \r and ignores any \n it finds during processing.
|
2016-08-02 05:34:46 +03:00
|
|
|
func readPasswordLine(reader io.Reader) ([]byte, error) {
|
|
|
|
var buf [1]byte
|
|
|
|
var ret []byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := reader.Read(buf[:])
|
|
|
|
if n > 0 {
|
|
|
|
switch buf[0] {
|
2019-12-21 21:38:13 +03:00
|
|
|
case '\b':
|
|
|
|
if len(ret) > 0 {
|
|
|
|
ret = ret[:len(ret)-1]
|
|
|
|
}
|
2016-08-02 05:34:46 +03:00
|
|
|
case '\n':
|
2020-01-19 01:47:15 +03:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
// otherwise ignore \n
|
2016-08-02 05:34:46 +03:00
|
|
|
case '\r':
|
2020-01-19 01:47:15 +03:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
// otherwise ignore \r
|
2016-08-02 05:34:46 +03:00
|
|
|
default:
|
|
|
|
ret = append(ret, buf[0])
|
|
|
|
}
|
2017-01-17 03:40:45 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF && len(ret) > 0 {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
return ret, err
|
2016-08-02 05:34:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|