2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-09-24 19:16:49 +03:00
|
|
|
"sync"
|
2015-09-20 18:14:29 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Terminal struct {
|
2015-09-24 19:16:49 +03:00
|
|
|
cfg *Config
|
|
|
|
state *terminal.State
|
|
|
|
outchan chan rune
|
|
|
|
closed int64
|
|
|
|
stopChan chan struct{}
|
|
|
|
kickChan chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
isReading bool
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
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-24 19:16:49 +03:00
|
|
|
cfg: cfg,
|
|
|
|
state: state,
|
|
|
|
kickChan: make(chan struct{}, 1),
|
|
|
|
outchan: make(chan rune),
|
|
|
|
stopChan: make(chan struct{}, 1),
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-24 19:16:49 +03:00
|
|
|
func (t *Terminal) IsReading() bool {
|
|
|
|
return t.isReading
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) KickRead() {
|
|
|
|
select {
|
|
|
|
case t.kickChan <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) ioloop() {
|
2015-09-24 19:16:49 +03:00
|
|
|
t.wg.Add(1)
|
|
|
|
defer t.wg.Done()
|
|
|
|
var (
|
|
|
|
isEscape bool
|
|
|
|
isEscapeEx bool
|
|
|
|
expectNextChar bool
|
|
|
|
)
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
2015-09-24 19:16:49 +03:00
|
|
|
if !expectNextChar {
|
|
|
|
t.isReading = false
|
|
|
|
select {
|
|
|
|
case <-t.kickChan:
|
|
|
|
t.isReading = true
|
|
|
|
case <-t.stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectNextChar = false
|
2015-09-20 18:14:29 +03:00
|
|
|
r, _, err := buf.ReadRune()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:14:05 +03:00
|
|
|
if isEscape {
|
|
|
|
isEscape = false
|
2015-09-23 06:46:56 +03:00
|
|
|
if r == CharEscapeEx {
|
2015-09-24 19:16:49 +03:00
|
|
|
expectNextChar = true
|
2015-09-21 16:14:05 +03:00
|
|
|
isEscapeEx = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r = escapeKey(r)
|
|
|
|
} else if isEscapeEx {
|
|
|
|
isEscapeEx = false
|
|
|
|
r = escapeExKey(r)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch r {
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharInterrupt:
|
2015-09-20 18:14:29 +03:00
|
|
|
t.outchan <- r
|
|
|
|
goto exit
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharEsc:
|
2015-09-21 16:14:05 +03:00
|
|
|
isEscape = true
|
2015-09-24 19:16:49 +03:00
|
|
|
expectNextChar = true
|
|
|
|
case CharEnter, CharCtrlJ:
|
|
|
|
t.outchan <- r
|
2015-09-20 18:14:29 +03:00
|
|
|
default:
|
2015-09-24 19:16:49 +03:00
|
|
|
expectNextChar = true
|
2015-09-23 06:46:56 +03:00
|
|
|
t.outchan <- r
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Close() error {
|
|
|
|
if atomic.SwapInt64(&t.closed, 1) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-24 19:16:49 +03:00
|
|
|
t.stopChan <- struct{}{}
|
|
|
|
t.wg.Wait()
|
2015-09-20 18:14:29 +03:00
|
|
|
return Restore(syscall.Stdin, t.state)
|
|
|
|
}
|