go-prompt/prompt.go

256 lines
5.4 KiB
Go
Raw Normal View History

2017-07-15 08:37:54 +00:00
package prompt
import (
2017-07-23 15:35:13 +00:00
"context"
2017-08-03 07:57:29 +00:00
"io/ioutil"
"log"
2017-07-15 08:37:54 +00:00
"os"
"os/signal"
2017-07-15 13:23:47 +00:00
"syscall"
2017-07-15 09:03:18 +00:00
"time"
2017-07-15 08:37:54 +00:00
)
2017-08-03 07:57:29 +00:00
const (
logfile = "/tmp/go-prompt-debug.log"
envEnableLog = "GO_PROMPT_ENABLE_LOG"
)
2017-07-23 15:35:13 +00:00
type Executor func(context.Context, string) string
2017-07-18 15:38:08 +00:00
type Completer func(string) []Completion
type Completion struct {
2017-07-18 00:53:59 +00:00
Text string
Description string
}
2017-07-15 08:37:54 +00:00
type Prompt struct {
2017-07-17 15:52:55 +00:00
in ConsoleParser
buf *Buffer
renderer *Render
executor Executor
completer Completer
maxCompletions uint16
2017-07-17 17:01:24 +00:00
selected int // -1 means nothing one is selected.
2017-08-03 07:57:29 +00:00
history []string
2017-07-15 08:37:54 +00:00
}
2017-08-04 07:22:55 +00:00
type Exec struct {
input string
ctx context.Context
}
func (e *Exec) Context() context.Context {
if e.ctx == nil {
e.ctx = context.Background()
}
return e.ctx
}
2017-07-15 08:37:54 +00:00
func (p *Prompt) Run() {
p.setUp()
defer p.tearDown()
2017-08-03 07:57:29 +00:00
if os.Getenv(envEnableLog) != "true" {
log.SetOutput(ioutil.Discard)
} else if f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666); err != nil {
log.SetOutput(ioutil.Discard)
} else {
defer f.Close()
log.SetOutput(f)
2017-08-04 07:22:55 +00:00
log.Println("[INFO] Logging is enabled.")
2017-08-03 07:57:29 +00:00
}
p.renderer.Render(p.buf, p.completer(p.buf.Text()), p.maxCompletions, p.selected)
2017-07-15 08:37:54 +00:00
bufCh := make(chan []byte, 128)
go readBuffer(bufCh)
2017-08-04 02:22:26 +00:00
exitCh := make(chan int)
2017-07-23 15:35:13 +00:00
winSizeCh := make(chan *WinSize)
2017-07-15 09:03:18 +00:00
go handleSignals(p.in, exitCh, winSizeCh)
2017-07-15 08:37:54 +00:00
for {
2017-07-15 09:03:18 +00:00
select {
2017-07-15 13:23:47 +00:00
case b := <-bufCh:
2017-08-04 07:22:55 +00:00
if shouldExit, exec := p.feed(b); shouldExit {
2017-07-15 09:03:18 +00:00
return
2017-08-04 07:22:55 +00:00
} else if exec != nil {
p.history = append(p.history, exec.input)
p.runExecutor(exec, bufCh)
2017-07-23 15:35:13 +00:00
completions := p.completer(p.buf.Text())
p.updateSelectedCompletion(completions)
p.renderer.Render(p.buf, completions, p.maxCompletions, p.selected)
2017-07-15 09:03:18 +00:00
} else {
2017-07-23 15:35:13 +00:00
completions := p.completer(p.buf.Text())
p.updateSelectedCompletion(completions)
p.renderer.Render(p.buf, completions, p.maxCompletions, p.selected)
2017-07-15 09:03:18 +00:00
}
2017-07-15 13:23:47 +00:00
case w := <-winSizeCh:
2017-07-15 09:03:18 +00:00
p.renderer.UpdateWinSize(w)
completions := p.completer(p.buf.Text())
2017-07-17 15:52:55 +00:00
p.renderer.Render(p.buf, completions, p.maxCompletions, p.selected)
2017-08-04 02:22:26 +00:00
case code := <-exitCh:
p.tearDown()
os.Exit(code)
2017-07-15 09:03:18 +00:00
default:
time.Sleep(10 * time.Millisecond)
2017-07-15 08:37:54 +00:00
}
}
}
2017-08-04 07:22:55 +00:00
func (p *Prompt) runExecutor(exec *Exec, bufCh chan []byte) {
resCh := make(chan string, 1)
ctx, cancel := context.WithCancel(exec.Context())
go func() {
resCh <- p.executor(ctx, exec.input)
}()
for {
select {
case r := <-resCh:
p.renderer.RenderResult(r)
return
case b := <-bufCh:
if p.in.GetKey(b) == ControlC {
log.Println("[INFO] Executor is canceled.")
cancel()
}
}
}
return
}
func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
2017-08-03 06:53:38 +00:00
key := p.in.GetKey(b)
2017-07-23 15:56:41 +00:00
2017-08-03 06:53:38 +00:00
switch key {
2017-08-03 07:57:29 +00:00
case ControlJ, Enter:
2017-07-23 15:35:13 +00:00
if p.selected != -1 {
c := p.completer(p.buf.Text())[p.selected]
w := p.buf.Document().GetWordBeforeCursor()
if w != "" {
p.buf.DeleteBeforeCursor(len([]rune(w)))
}
p.buf.InsertText(c.Text, false, true)
}
p.renderer.BreakLine(p.buf)
2017-08-04 07:22:55 +00:00
exec = &Exec{input: p.buf.Text()}
log.Printf("[History] %s", p.buf.Text())
2017-07-23 15:35:13 +00:00
p.buf = NewBuffer()
p.selected = -1
2017-07-23 15:56:41 +00:00
case ControlC:
2017-07-23 15:35:13 +00:00
p.renderer.BreakLine(p.buf)
p.buf = NewBuffer()
p.selected = -1
2017-07-23 15:56:41 +00:00
case ControlD:
2017-07-23 15:35:13 +00:00
shouldExit = true
2017-07-23 15:56:41 +00:00
case Up:
fallthrough
case BackTab:
2017-07-23 15:35:13 +00:00
p.selected -= 1
2017-07-23 15:56:41 +00:00
case Down:
fallthrough
2017-08-04 02:22:26 +00:00
case Tab, ControlI:
2017-07-23 15:35:13 +00:00
p.selected += 1
2017-08-03 06:53:38 +00:00
case Left:
p.buf.CursorLeft(1)
case Right:
p.buf.CursorRight(1)
case Backspace:
2017-08-04 07:22:55 +00:00
if p.selected != -1 {
c := p.completer(p.buf.Text())[p.selected]
w := p.buf.Document().GetWordBeforeCursor()
if w != "" {
p.buf.DeleteBeforeCursor(len([]rune(w)))
}
p.buf.InsertText(c.Text, false, true)
p.selected = -1
}
2017-08-03 06:53:38 +00:00
p.buf.DeleteBeforeCursor(1)
case NotDefined:
if p.selected != -1 {
c := p.completer(p.buf.Text())[p.selected]
w := p.buf.Document().GetWordBeforeCursor()
if w != "" {
p.buf.DeleteBeforeCursor(len([]rune(w)))
}
p.buf.InsertText(c.Text, false, true)
}
p.selected = -1
p.buf.InsertText(string(b), false, true)
2017-07-23 15:56:41 +00:00
default:
2017-07-23 15:35:13 +00:00
p.selected = -1
}
return
}
2017-07-18 15:38:08 +00:00
func (p *Prompt) updateSelectedCompletion(completions []Completion) {
2017-07-17 15:52:55 +00:00
max := int(p.maxCompletions)
if len(completions) < max {
max = len(completions)
}
if p.selected >= max {
p.selected = -1
} else if p.selected < -1 {
p.selected = max - 1
}
}
2017-07-15 08:37:54 +00:00
func (p *Prompt) setUp() {
p.in.Setup()
2017-07-15 11:22:56 +00:00
p.renderer.Setup()
2017-07-15 16:04:18 +00:00
p.renderer.UpdateWinSize(p.in.GetWinSize())
2017-07-17 16:14:03 +00:00
p.selected = -1 // -1 means nothing one is selected.
2017-07-15 08:37:54 +00:00
}
func (p *Prompt) tearDown() {
p.in.TearDown()
2017-07-15 11:22:56 +00:00
p.renderer.TearDown()
2017-07-15 08:37:54 +00:00
}
func readBuffer(bufCh chan []byte) {
buf := make([]byte, 1024)
for {
if n, err := syscall.Read(syscall.Stdin, buf); err == nil {
bufCh <- buf[:n]
}
}
}
2017-08-04 02:22:26 +00:00
func handleSignals(in ConsoleParser, exitCh chan int, winSizeCh chan *WinSize) {
2017-07-15 08:37:54 +00:00
sigCh := make(chan os.Signal, 1)
signal.Notify(
sigCh,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGWINCH,
)
for {
s := <-sigCh
switch s {
2017-08-04 02:22:26 +00:00
case syscall.SIGINT: // kill -SIGINT XXXX or Ctrl+c
2017-08-04 07:22:55 +00:00
log.Println("[SIGNAL] Catch SIGINT")
2017-08-04 02:22:26 +00:00
exitCh <- 0
2017-07-15 08:37:54 +00:00
2017-08-04 02:22:26 +00:00
case syscall.SIGTERM: // kill -SIGTERM XXXX
2017-08-04 07:22:55 +00:00
log.Println("[SIGNAL] Catch SIGTERM")
2017-08-04 02:22:26 +00:00
exitCh <- 1
2017-07-15 08:37:54 +00:00
2017-08-04 02:22:26 +00:00
case syscall.SIGQUIT: // kill -SIGQUIT XXXX
2017-08-04 07:22:55 +00:00
log.Println("[SIGNAL] Catch SIGQUIT")
2017-08-04 02:22:26 +00:00
exitCh <- 0
2017-07-15 08:37:54 +00:00
case syscall.SIGWINCH:
2017-08-04 07:22:55 +00:00
log.Println("[SIGNAL] Catch SIGWINCH")
2017-07-15 08:37:54 +00:00
winSizeCh <- in.GetWinSize()
2017-08-04 02:22:26 +00:00
// TODO: SIGUSR1 -> Reopen log file.
2017-07-15 08:37:54 +00:00
default:
}
}
}