2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MetaPrev = -iota - 1
|
|
|
|
MetaNext
|
|
|
|
MetaDelete
|
2015-09-21 16:00:48 +03:00
|
|
|
MetaBackspace
|
2015-09-20 18:14:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-21 16:14:05 +03:00
|
|
|
KeyPrevChar = 2
|
|
|
|
KeyInterrupt = 3
|
|
|
|
KeyNextChar = 6
|
|
|
|
KeyDelete = 4
|
|
|
|
KeyEsc = 27
|
|
|
|
KeyEscapeEx = 91
|
2015-09-20 18:14:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Terminal struct {
|
2015-09-22 13:16:24 +03:00
|
|
|
cfg *Config
|
2015-09-20 18:14:29 +03:00
|
|
|
state *terminal.State
|
|
|
|
outchan chan rune
|
|
|
|
closed int64
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func NewTerminal(cfg *Config) (*Terminal, error) {
|
2015-09-20 18:14:29 +03:00
|
|
|
state, err := MakeRaw(syscall.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t := &Terminal{
|
2015-09-22 13:16:24 +03:00
|
|
|
cfg: cfg,
|
2015-09-20 18:14:29 +03:00
|
|
|
state: state,
|
|
|
|
outchan: make(chan rune),
|
|
|
|
}
|
|
|
|
|
|
|
|
go t.ioloop()
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Write(b []byte) (int, error) {
|
|
|
|
return os.Stdout.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Print(s string) {
|
|
|
|
fmt.Fprintf(os.Stdout, "%s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) PrintRune(r rune) {
|
|
|
|
fmt.Fprintf(os.Stdout, "%c", r)
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func (t *Terminal) Readline() *Operation {
|
|
|
|
return NewOperation(t, t.cfg)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) ReadRune() rune {
|
|
|
|
return <-t.outchan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) ioloop() {
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
2015-09-21 16:14:05 +03:00
|
|
|
isEscape := false
|
|
|
|
isEscapeEx := false
|
2015-09-20 18:14:29 +03:00
|
|
|
for {
|
|
|
|
r, _, err := buf.ReadRune()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:14:05 +03:00
|
|
|
if isEscape {
|
|
|
|
isEscape = false
|
|
|
|
if r == KeyEscapeEx {
|
|
|
|
isEscapeEx = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r = escapeKey(r)
|
|
|
|
} else if isEscapeEx {
|
|
|
|
isEscapeEx = false
|
|
|
|
r = escapeExKey(r)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if IsPrintable(r) || r < 0 {
|
|
|
|
t.outchan <- r
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch r {
|
|
|
|
case KeyInterrupt:
|
|
|
|
t.outchan <- r
|
|
|
|
goto exit
|
|
|
|
case KeyEsc:
|
2015-09-21 16:14:05 +03:00
|
|
|
isEscape = true
|
2015-09-21 08:13:30 +03:00
|
|
|
case CharEnter, CharEnter2, KeyPrevChar, KeyNextChar, KeyDelete:
|
2015-09-20 18:14:29 +03:00
|
|
|
fallthrough
|
2015-09-22 18:01:15 +03:00
|
|
|
case CharFwdSearch, CharBckSearch, CharCannel:
|
|
|
|
fallthrough
|
2015-09-21 17:51:48 +03:00
|
|
|
case CharLineEnd, CharLineStart, CharNext, CharPrev, CharKill:
|
2015-09-20 18:14:29 +03:00
|
|
|
t.outchan <- r
|
|
|
|
default:
|
|
|
|
println("np:", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Close() error {
|
|
|
|
if atomic.SwapInt64(&t.closed, 1) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return Restore(syscall.Stdin, t.state)
|
|
|
|
}
|