2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2016-09-01 13:13:06 +03:00
|
|
|
"io"
|
2016-04-17 16:05:00 +03:00
|
|
|
"strings"
|
2015-09-24 19:16:49 +03:00
|
|
|
"sync"
|
2015-09-20 18:14:29 +03:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Terminal struct {
|
2017-01-25 06:55:32 +03:00
|
|
|
m sync.Mutex
|
2015-09-24 19:16:49 +03:00
|
|
|
cfg *Config
|
|
|
|
outchan chan rune
|
2015-11-20 12:09:55 +03:00
|
|
|
closed int32
|
2015-09-24 19:16:49 +03:00
|
|
|
stopChan chan struct{}
|
|
|
|
kickChan chan struct{}
|
|
|
|
wg sync.WaitGroup
|
2015-11-20 12:09:55 +03:00
|
|
|
isReading int32
|
2016-04-17 16:05:00 +03:00
|
|
|
sleeping int32
|
2016-08-31 18:51:28 +03:00
|
|
|
|
|
|
|
sizeChan chan string
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func NewTerminal(cfg *Config) (*Terminal, error) {
|
2015-09-27 04:04:50 +03:00
|
|
|
if err := cfg.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-20 18:14:29 +03:00
|
|
|
t := &Terminal{
|
2015-09-24 19:16:49 +03:00
|
|
|
cfg: cfg,
|
|
|
|
kickChan: make(chan struct{}, 1),
|
|
|
|
outchan: make(chan rune),
|
|
|
|
stopChan: make(chan struct{}, 1),
|
2016-08-31 18:51:28 +03:00
|
|
|
sizeChan: make(chan string, 1),
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:22:31 +03:00
|
|
|
t.wg.Add(1)
|
2015-09-20 18:14:29 +03:00
|
|
|
go t.ioloop()
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2016-04-17 16:05:00 +03:00
|
|
|
// SleepToResume will sleep myself, and return only if I'm resumed.
|
|
|
|
func (t *Terminal) SleepToResume() {
|
|
|
|
if !atomic.CompareAndSwapInt32(&t.sleeping, 0, 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&t.sleeping, 0)
|
|
|
|
|
|
|
|
t.ExitRawMode()
|
|
|
|
ch := WaitForResume()
|
|
|
|
SuspendMe()
|
|
|
|
<-ch
|
|
|
|
t.EnterRawMode()
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:20:26 +03:00
|
|
|
func (t *Terminal) EnterRawMode() (err error) {
|
2016-03-13 13:32:48 +03:00
|
|
|
return t.cfg.FuncMakeRaw()
|
2015-11-09 06:20:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) ExitRawMode() (err error) {
|
2016-03-13 13:32:48 +03:00
|
|
|
return t.cfg.FuncExitRaw()
|
2015-11-09 06:20:26 +03:00
|
|
|
}
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) Write(b []byte) (int, error) {
|
2015-09-27 04:04:50 +03:00
|
|
|
return t.cfg.Stdout.Write(b)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 15:43:15 +03:00
|
|
|
// WriteStdin prefill the next Stdin fetch
|
|
|
|
// Next time you call ReadLine() this value will be writen before the user input
|
|
|
|
func (t *Terminal) WriteStdin(b []byte) (int, error) {
|
|
|
|
return t.cfg.StdinWriter.Write(b)
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:51:28 +03:00
|
|
|
type termSize struct {
|
|
|
|
left int
|
|
|
|
top int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) GetOffset(f func(offset string)) {
|
|
|
|
go func() {
|
|
|
|
f(<-t.sizeChan)
|
|
|
|
}()
|
|
|
|
t.Write([]byte("\033[6n"))
|
|
|
|
}
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) Print(s string) {
|
2015-09-27 04:04:50 +03:00
|
|
|
fmt.Fprintf(t.cfg.Stdout, "%s", s)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) PrintRune(r rune) {
|
2015-09-27 04:04:50 +03:00
|
|
|
fmt.Fprintf(t.cfg.Stdout, "%c", r)
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-17 09:08:55 +03:00
|
|
|
// return rune(0) if meet EOF
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) ReadRune() rune {
|
2016-02-17 09:08:55 +03:00
|
|
|
ch, ok := <-t.outchan
|
|
|
|
if !ok {
|
|
|
|
return rune(0)
|
|
|
|
}
|
|
|
|
return ch
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:16:49 +03:00
|
|
|
func (t *Terminal) IsReading() bool {
|
2015-11-20 12:09:55 +03:00
|
|
|
return atomic.LoadInt32(&t.isReading) == 1
|
2015-09-24 19:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) KickRead() {
|
|
|
|
select {
|
|
|
|
case t.kickChan <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) ioloop() {
|
2016-09-01 13:13:06 +03:00
|
|
|
defer func() {
|
|
|
|
t.wg.Done()
|
|
|
|
close(t.outchan)
|
|
|
|
}()
|
|
|
|
|
2015-09-24 19:16:49 +03:00
|
|
|
var (
|
|
|
|
isEscape bool
|
|
|
|
isEscapeEx bool
|
|
|
|
expectNextChar bool
|
|
|
|
)
|
|
|
|
|
2017-01-25 06:55:32 +03:00
|
|
|
buf := bufio.NewReader(t.getStdin())
|
2015-09-20 18:14:29 +03:00
|
|
|
for {
|
2015-09-24 19:16:49 +03:00
|
|
|
if !expectNextChar {
|
2015-11-20 12:09:55 +03:00
|
|
|
atomic.StoreInt32(&t.isReading, 0)
|
2015-09-24 19:16:49 +03:00
|
|
|
select {
|
|
|
|
case <-t.kickChan:
|
2015-11-20 12:09:55 +03:00
|
|
|
atomic.StoreInt32(&t.isReading, 1)
|
2015-09-24 19:16:49 +03:00
|
|
|
case <-t.stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectNextChar = false
|
2015-09-20 18:14:29 +03:00
|
|
|
r, _, err := buf.ReadRune()
|
|
|
|
if err != nil {
|
2016-04-17 16:05:00 +03:00
|
|
|
if strings.Contains(err.Error(), "interrupted system call") {
|
|
|
|
expectNextChar = true
|
|
|
|
continue
|
|
|
|
}
|
2015-09-20 18:14:29 +03:00
|
|
|
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
|
|
|
|
}
|
2016-06-21 12:53:12 +03:00
|
|
|
r = escapeKey(r, buf)
|
2015-09-21 16:14:05 +03:00
|
|
|
} else if isEscapeEx {
|
|
|
|
isEscapeEx = false
|
2016-08-31 18:51:28 +03:00
|
|
|
if key := readEscKey(r, buf); key != nil {
|
|
|
|
r = escapeExKey(key)
|
|
|
|
// offset
|
|
|
|
if key.typ == 'R' {
|
|
|
|
if _, _, ok := key.Get2(); ok {
|
|
|
|
select {
|
|
|
|
case t.sizeChan <- key.attr:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectNextChar = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
expectNextChar = true
|
|
|
|
continue
|
|
|
|
}
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:49:55 +03:00
|
|
|
expectNextChar = true
|
2015-09-20 18:14:29 +03:00
|
|
|
switch r {
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharEsc:
|
2015-10-01 17:44:43 +03:00
|
|
|
if t.cfg.VimMode {
|
|
|
|
t.outchan <- r
|
|
|
|
break
|
|
|
|
}
|
2015-09-21 16:14:05 +03:00
|
|
|
isEscape = true
|
2015-11-19 06:55:07 +03:00
|
|
|
case CharInterrupt, CharEnter, CharCtrlJ, CharDelete:
|
2015-09-24 19:49:55 +03:00
|
|
|
expectNextChar = false
|
|
|
|
fallthrough
|
2015-09-20 18:14:29 +03:00
|
|
|
default:
|
2015-09-23 06:46:56 +03:00
|
|
|
t.outchan <- r
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:44:43 +03:00
|
|
|
func (t *Terminal) Bell() {
|
|
|
|
fmt.Fprintf(t, "%c", CharBell)
|
|
|
|
}
|
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
func (t *Terminal) Close() error {
|
2015-11-20 12:09:55 +03:00
|
|
|
if atomic.SwapInt32(&t.closed, 1) != 0 {
|
2015-09-20 18:14:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
if closer, ok := t.cfg.Stdin.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
close(t.stopChan)
|
2015-09-24 19:16:49 +03:00
|
|
|
t.wg.Wait()
|
2015-11-09 06:20:26 +03:00
|
|
|
return t.ExitRawMode()
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
|
2017-01-25 06:55:32 +03:00
|
|
|
func (t *Terminal) GetConfig() *Config {
|
|
|
|
t.m.Lock()
|
|
|
|
cfg := *t.cfg
|
|
|
|
t.m.Unlock()
|
|
|
|
return &cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) getStdin() io.Reader {
|
|
|
|
t.m.Lock()
|
|
|
|
r := t.cfg.Stdin
|
|
|
|
t.m.Unlock()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (t *Terminal) SetConfig(c *Config) error {
|
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-25 06:55:32 +03:00
|
|
|
t.m.Lock()
|
2015-11-20 15:56:42 +03:00
|
|
|
t.cfg = c
|
2017-01-25 06:55:32 +03:00
|
|
|
t.m.Unlock()
|
2015-11-20 15:56:42 +03:00
|
|
|
return nil
|
|
|
|
}
|