readline/terminal.go

161 lines
2.6 KiB
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package readline
import (
"bufio"
"fmt"
2015-09-24 19:16:49 +03:00
"sync"
2015-09-20 18:14:29 +03:00
"sync/atomic"
"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 int32
2015-09-24 19:16:49 +03:00
stopChan chan struct{}
kickChan chan struct{}
wg sync.WaitGroup
isReading int32
2015-09-20 18:14:29 +03:00
}
2015-09-22 13:16:24 +03:00
func NewTerminal(cfg *Config) (*Terminal, error) {
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),
2015-09-20 18:14:29 +03:00
}
go t.ioloop()
return t, nil
}
func (t *Terminal) EnterRawMode() (err error) {
t.state, err = MakeRaw(StdinFd)
return err
}
func (t *Terminal) ExitRawMode() (err error) {
if t.state == nil {
return
}
err = Restore(StdinFd, t.state)
if err == nil {
t.state = nil
}
return err
}
2015-09-20 18:14:29 +03:00
func (t *Terminal) Write(b []byte) (int, error) {
return t.cfg.Stdout.Write(b)
2015-09-20 18:14:29 +03:00
}
func (t *Terminal) Print(s string) {
fmt.Fprintf(t.cfg.Stdout, "%s", s)
2015-09-20 18:14:29 +03:00
}
func (t *Terminal) PrintRune(r rune) {
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
}
func (t *Terminal) ReadRune() rune {
return <-t.outchan
}
2015-09-24 19:16:49 +03:00
func (t *Terminal) IsReading() bool {
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() {
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-29 18:28:12 +03:00
buf := bufio.NewReader(Stdin)
2015-09-20 18:14:29 +03:00
for {
2015-09-24 19:16:49 +03:00
if !expectNextChar {
atomic.StoreInt32(&t.isReading, 0)
2015-09-24 19:16:49 +03:00
select {
case <-t.kickChan:
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 {
break
}
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
isEscapeEx = true
continue
}
r = escapeKey(r)
} else if isEscapeEx {
isEscapeEx = false
r = escapeExKey(r, buf)
2015-09-20 18:14:29 +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
}
isEscape = true
case CharInterrupt, CharEnter, CharCtrlJ, CharDelete:
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
}
}
}
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 {
if atomic.SwapInt32(&t.closed, 1) != 0 {
2015-09-20 18:14:29 +03:00
return nil
}
2015-09-24 19:16:49 +03:00
t.stopChan <- struct{}{}
t.wg.Wait()
return t.ExitRawMode()
2015-09-20 18:14:29 +03:00
}
2015-11-20 15:56:42 +03:00
func (t *Terminal) SetConfig(c *Config) error {
if err := c.Init(); err != nil {
return err
}
t.cfg = c
return nil
}