2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
2015-10-26 11:41:48 +03:00
|
|
|
"bufio"
|
2016-03-05 10:27:12 +03:00
|
|
|
"bytes"
|
2016-09-03 06:26:39 +03:00
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-04-24 16:25:55 +03:00
|
|
|
"os/signal"
|
2015-09-29 16:01:17 +03:00
|
|
|
"strconv"
|
2016-08-31 18:51:28 +03:00
|
|
|
"strings"
|
2016-04-17 16:05:00 +03:00
|
|
|
"sync"
|
2022-04-24 16:25:55 +03:00
|
|
|
"syscall"
|
2016-04-17 16:05:00 +03:00
|
|
|
"time"
|
2016-08-31 18:51:28 +03:00
|
|
|
"unicode"
|
2015-09-20 18:14:29 +03:00
|
|
|
)
|
|
|
|
|
2015-09-28 04:46:33 +03:00
|
|
|
var (
|
2015-09-29 16:01:17 +03:00
|
|
|
isWindows = false
|
2015-09-28 04:46:33 +03:00
|
|
|
)
|
|
|
|
|
2016-09-03 06:26:39 +03:00
|
|
|
const (
|
|
|
|
CharLineStart = 1
|
|
|
|
CharBackward = 2
|
|
|
|
CharInterrupt = 3
|
|
|
|
CharDelete = 4
|
|
|
|
CharLineEnd = 5
|
|
|
|
CharForward = 6
|
|
|
|
CharBell = 7
|
|
|
|
CharCtrlH = 8
|
|
|
|
CharTab = 9
|
|
|
|
CharCtrlJ = 10
|
|
|
|
CharKill = 11
|
|
|
|
CharCtrlL = 12
|
|
|
|
CharEnter = 13
|
|
|
|
CharNext = 14
|
|
|
|
CharPrev = 16
|
|
|
|
CharBckSearch = 18
|
|
|
|
CharFwdSearch = 19
|
|
|
|
CharTranspose = 20
|
|
|
|
CharCtrlU = 21
|
|
|
|
CharCtrlW = 23
|
2017-10-02 06:57:50 +03:00
|
|
|
CharCtrlY = 25
|
2016-09-03 06:26:39 +03:00
|
|
|
CharCtrlZ = 26
|
|
|
|
CharEsc = 27
|
2022-05-20 16:29:21 +03:00
|
|
|
CharO = 79
|
2016-09-03 06:26:39 +03:00
|
|
|
CharEscapeEx = 91
|
|
|
|
CharBackspace = 127
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MetaBackward rune = -iota - 1
|
|
|
|
MetaForward
|
|
|
|
MetaDelete
|
|
|
|
MetaBackspace
|
|
|
|
MetaTranspose
|
|
|
|
)
|
|
|
|
|
2016-04-17 16:05:00 +03:00
|
|
|
// WaitForResume need to call before current process got suspend.
|
|
|
|
// It will run a ticker until a long duration is occurs,
|
|
|
|
// which means this process is resumed.
|
|
|
|
func WaitForResume() chan struct{} {
|
|
|
|
ch := make(chan struct{})
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
t := time.Now()
|
|
|
|
wg.Done()
|
|
|
|
for {
|
|
|
|
now := <-ticker.C
|
|
|
|
if now.Sub(t) > 100*time.Millisecond {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t = now
|
|
|
|
}
|
|
|
|
ticker.Stop()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2016-07-21 19:31:42 +03:00
|
|
|
func Restore(fd int, state *State) error {
|
|
|
|
err := restoreTerm(fd, state)
|
2015-09-24 19:16:49 +03:00
|
|
|
if err != nil {
|
2015-09-27 05:50:14 +03:00
|
|
|
// errno 0 means everything is ok :)
|
2015-09-24 19:16:49 +03:00
|
|
|
if err.Error() == "errno 0" {
|
2017-03-13 02:57:45 +03:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
2015-09-24 19:16:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsPrintable(key rune) bool {
|
|
|
|
isInSurrogateArea := key >= 0xd800 && key <= 0xdbff
|
|
|
|
return key >= 32 && !isInSurrogateArea
|
|
|
|
}
|
|
|
|
|
2015-09-27 05:50:14 +03:00
|
|
|
// translate Esc[X
|
2016-08-31 18:51:28 +03:00
|
|
|
func escapeExKey(key *escapeKeyPair) rune {
|
|
|
|
var r rune
|
|
|
|
switch key.typ {
|
2015-09-21 16:14:05 +03:00
|
|
|
case 'D':
|
|
|
|
r = CharBackward
|
|
|
|
case 'C':
|
|
|
|
r = CharForward
|
|
|
|
case 'A':
|
|
|
|
r = CharPrev
|
|
|
|
case 'B':
|
|
|
|
r = CharNext
|
2015-10-23 14:16:45 +03:00
|
|
|
case 'H':
|
|
|
|
r = CharLineStart
|
|
|
|
case 'F':
|
|
|
|
r = CharLineEnd
|
2016-08-31 18:51:28 +03:00
|
|
|
case '~':
|
|
|
|
if key.attr == "3" {
|
|
|
|
r = CharDelete
|
2015-10-28 04:02:52 +03:00
|
|
|
}
|
2016-08-31 18:51:28 +03:00
|
|
|
default:
|
2015-09-21 16:14:05 +03:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:29:21 +03:00
|
|
|
// translate EscOX SS3 codes for up/down/etc.
|
|
|
|
func escapeSS3Key(key *escapeKeyPair) rune {
|
|
|
|
var r rune
|
|
|
|
switch key.typ {
|
|
|
|
case 'D':
|
|
|
|
r = CharBackward
|
|
|
|
case 'C':
|
|
|
|
r = CharForward
|
|
|
|
case 'A':
|
|
|
|
r = CharPrev
|
|
|
|
case 'B':
|
|
|
|
r = CharNext
|
|
|
|
case 'H':
|
|
|
|
r = CharLineStart
|
|
|
|
case 'F':
|
|
|
|
r = CharLineEnd
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:51:28 +03:00
|
|
|
type escapeKeyPair struct {
|
|
|
|
attr string
|
|
|
|
typ rune
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *escapeKeyPair) Get2() (int, int, bool) {
|
|
|
|
sp := strings.Split(e.attr, ";")
|
|
|
|
if len(sp) < 2 {
|
|
|
|
return -1, -1, false
|
|
|
|
}
|
|
|
|
s1, err := strconv.Atoi(sp[0])
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, false
|
|
|
|
}
|
|
|
|
s2, err := strconv.Atoi(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, false
|
|
|
|
}
|
|
|
|
return s1, s2, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func readEscKey(r rune, reader *bufio.Reader) *escapeKeyPair {
|
|
|
|
p := escapeKeyPair{}
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
for {
|
|
|
|
if r == ';' {
|
|
|
|
} else if unicode.IsNumber(r) {
|
|
|
|
} else {
|
|
|
|
p.typ = r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf.WriteRune(r)
|
|
|
|
r, _, _ = reader.ReadRune()
|
|
|
|
}
|
|
|
|
p.attr = buf.String()
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2015-09-27 05:50:14 +03:00
|
|
|
// translate EscX to Meta+X
|
2016-06-21 12:53:12 +03:00
|
|
|
func escapeKey(r rune, reader *bufio.Reader) rune {
|
2015-09-20 18:14:29 +03:00
|
|
|
switch r {
|
|
|
|
case 'b':
|
2015-10-01 17:44:43 +03:00
|
|
|
r = MetaBackward
|
2015-09-20 18:14:29 +03:00
|
|
|
case 'f':
|
2015-10-01 17:44:43 +03:00
|
|
|
r = MetaForward
|
2015-09-20 18:14:29 +03:00
|
|
|
case 'd':
|
|
|
|
r = MetaDelete
|
2015-09-23 08:03:13 +03:00
|
|
|
case CharTranspose:
|
|
|
|
r = MetaTranspose
|
2015-09-21 16:00:48 +03:00
|
|
|
case CharBackspace:
|
|
|
|
r = MetaBackspace
|
2016-06-21 12:53:12 +03:00
|
|
|
case 'O':
|
|
|
|
d, _, _ := reader.ReadRune()
|
|
|
|
switch d {
|
|
|
|
case 'H':
|
|
|
|
r = CharLineStart
|
|
|
|
case 'F':
|
|
|
|
r = CharLineEnd
|
|
|
|
default:
|
|
|
|
reader.UnreadRune()
|
|
|
|
}
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharEsc:
|
2015-09-21 08:13:30 +03:00
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-05-16 04:56:06 +03:00
|
|
|
// split prompt + runes into lines by screenwidth starting from an offset.
|
|
|
|
// the prompt should be filtered before passing to only its display runes.
|
|
|
|
// if you know the width of the next character, pass it in as it is used
|
|
|
|
// to decide if we generate an extra empty rune array to show next is new
|
|
|
|
// line.
|
|
|
|
func SplitByLine(prompt, rs []rune, offset, screenWidth, nextWidth int) [][]rune {
|
|
|
|
ret := make([][]rune, 0)
|
|
|
|
prs := append(prompt, rs...)
|
|
|
|
si := 0
|
|
|
|
currentWidth := offset
|
|
|
|
for i, r := range prs {
|
2016-03-05 10:27:12 +03:00
|
|
|
w := runes.Width(r)
|
2022-05-16 04:56:06 +03:00
|
|
|
if currentWidth + w > screenWidth {
|
|
|
|
ret = append(ret, prs[si:i])
|
|
|
|
si = i
|
2016-03-05 10:27:12 +03:00
|
|
|
currentWidth = 0
|
|
|
|
}
|
2022-05-16 04:56:06 +03:00
|
|
|
currentWidth += w
|
|
|
|
}
|
|
|
|
ret = append(ret, prs[si:len(prs)])
|
|
|
|
if currentWidth + nextWidth > screenWidth {
|
|
|
|
ret = append(ret, []rune{})
|
2016-03-05 10:27:12 +03:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-09-27 05:50:14 +03:00
|
|
|
// calculate how many lines for N character
|
2016-03-05 10:27:12 +03:00
|
|
|
func LineCount(screenWidth, w int) int {
|
2015-09-22 18:01:15 +03:00
|
|
|
r := w / screenWidth
|
|
|
|
if w%screenWidth != 0 {
|
|
|
|
r++
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-09-23 06:59:39 +03:00
|
|
|
func IsWordBreak(i rune) bool {
|
2016-03-25 12:07:55 +03:00
|
|
|
switch {
|
|
|
|
case i >= 'a' && i <= 'z':
|
|
|
|
case i >= 'A' && i <= 'Z':
|
|
|
|
case i >= '0' && i <= '9':
|
|
|
|
default:
|
|
|
|
return true
|
2015-09-23 06:59:39 +03:00
|
|
|
}
|
2016-03-25 12:07:55 +03:00
|
|
|
return false
|
2015-09-23 06:59:39 +03:00
|
|
|
}
|
2015-09-25 07:45:39 +03:00
|
|
|
|
2015-09-29 16:01:17 +03:00
|
|
|
func GetInt(s []string, def int) int {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
c, err := strconv.Atoi(s[0])
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2016-03-05 05:46:11 +03:00
|
|
|
|
2016-03-13 13:32:48 +03:00
|
|
|
type RawMode struct {
|
2016-07-21 19:31:42 +03:00
|
|
|
state *State
|
2016-03-13 13:32:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RawMode) Enter() (err error) {
|
|
|
|
r.state, err = MakeRaw(GetStdin())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RawMode) Exit() error {
|
|
|
|
if r.state == nil {
|
|
|
|
return nil
|
2016-03-05 05:46:11 +03:00
|
|
|
}
|
2016-03-13 13:32:48 +03:00
|
|
|
return Restore(GetStdin(), r.state)
|
2016-03-05 05:46:11 +03:00
|
|
|
}
|
2016-09-03 06:26:39 +03:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func sleep(n int) {
|
|
|
|
Debug(n)
|
|
|
|
time.Sleep(2000 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// print a linked list to Debug()
|
|
|
|
func debugList(l *list.List) {
|
|
|
|
idx := 0
|
|
|
|
for e := l.Front(); e != nil; e = e.Next() {
|
|
|
|
Debug(idx, fmt.Sprintf("%+v", e.Value))
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append log info to another file
|
|
|
|
func Debug(o ...interface{}) {
|
|
|
|
f, _ := os.OpenFile("debug.tmp", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
fmt.Fprintln(f, o...)
|
|
|
|
f.Close()
|
|
|
|
}
|
2022-04-24 16:25:55 +03:00
|
|
|
|
|
|
|
func CaptureExitSignal(f func()) {
|
|
|
|
cSignal := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(cSignal, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
for range cSignal {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|