Display prefix and run goimports

This commit is contained in:
c-bata 2017-07-15 22:23:47 +09:00
parent 767616d011
commit c3a68affd7
6 changed files with 40 additions and 33 deletions

@ -7,7 +7,7 @@ import (
)
func executor(b *prompt.Buffer) string {
r := "\n>>> Your input: '" + b.Text() + "' <<<\n"
r := "Your input: " + b.Text()
return r
}
@ -20,8 +20,8 @@ func completer(b *prompt.Buffer) []string {
}
func main() {
pt := prompt.NewPrompt(executor, completer)
pt := prompt.NewPrompt(executor, completer, 8)
defer fmt.Println("\nGoodbye!")
fmt.Print("Hello! This is a example appication using prompt-toolkit.\n\n")
fmt.Print("Hello! This is a example appication using prompt-toolkit.\n")
pt.Run()
}

@ -1,9 +1,9 @@
package prompt
import (
"syscall"
"os"
"os/signal"
"syscall"
"time"
)
@ -23,7 +23,6 @@ func (p *Prompt) Run() {
p.setUp()
defer p.tearDown()
bufCh := make(chan []byte, 128)
go readBuffer(bufCh)
@ -42,7 +41,7 @@ func (p *Prompt) Run() {
res := p.executor(p.buf)
p.renderer.BreakLine(p.buf, res)
p.buf = NewBuffer()
} else if ac.Key == ControlC {
} else if ac.Key == ControlC || ac.Key == ControlD {
return
} else {
InputHandler(ac, p.buf)
@ -119,12 +118,13 @@ func handleSignals(in *VT100Parser, exitCh chan bool, winSizeCh chan *WinSize) {
}
}
func NewPrompt(executor Executor, completer Completer) *Prompt {
func NewPrompt(executor Executor, completer Completer, maxCompletions uint8) *Prompt {
return &Prompt{
in: NewVT100Parser(),
renderer: &Render{
Prefix: ">>> ",
out: NewVT100Writer(),
maxCompletions: maxCompletions,
},
title: "Hello! this is prompt toolkit",
buf: NewBuffer(),

@ -1,6 +1,5 @@
package prompt
type Render struct {
Prefix string
Title string
@ -8,13 +7,15 @@ type Render struct {
row uint16
col uint16 // sigwinchで送られてくる列数を常に見ながら、prefixのlengthとbufferのcursor positionを比べて、completionの表示位置をずらす
chosen uint8 // the index number of a chosen completion
maxCompletions uint8
}
func (r *Render) Setup() {
if r.Title != "" {
r.out.SetTitle(r.Title)
r.out.Flush()
}
r.out.WriteStr(r.Prefix)
r.out.Flush()
}
func (r *Render) TearDown() {
@ -63,8 +64,11 @@ func (r *Render) RenderCompletion(words []string) {
}
func (r *Render) Erase(buffer *Buffer) {
r.out.CursorBackward(len(r.Prefix))
r.out.CursorBackward(buffer.CursorPosition)
r.out.EraseDown()
r.out.WriteStr(r.Prefix)
r.out.Flush()
return
}
@ -78,7 +82,10 @@ func (r *Render) Render(buffer *Buffer, completions []string) {
func (r *Render) BreakLine(buffer *Buffer, result string) {
r.out.WriteStr(buffer.Document().Text)
r.out.WriteStr("\n")
r.out.WriteStr(result)
r.out.WriteStr("\n")
r.out.WriteStr(r.Prefix)
}
func formatCompletions(words []string) ([]string, int) {

@ -1,8 +1,8 @@
package prompt
import (
"testing"
"reflect"
"testing"
)
func TestFormatCompletion(t *testing.T) {