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 { func executor(b *prompt.Buffer) string {
r := "\n>>> Your input: '" + b.Text() + "' <<<\n" r := "Your input: " + b.Text()
return r return r
} }
@ -20,8 +20,8 @@ func completer(b *prompt.Buffer) []string {
} }
func main() { func main() {
pt := prompt.NewPrompt(executor, completer) pt := prompt.NewPrompt(executor, completer, 8)
defer fmt.Println("\nGoodbye!") 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() pt.Run()
} }

@ -2,7 +2,7 @@ package prompt
type Completion struct { type Completion struct {
// The new string that will be inserted into document. // The new string that will be inserted into document.
text string text string
// Position relative to the cursor position where the new text will start. // Position relative to the cursor position where the new text will start.
startPosition int startPosition int
} }
@ -13,7 +13,7 @@ func (c *Completion) NewCompletionFromPosition(position int) *Completion {
} }
return &Completion{ return &Completion{
text: c.text[position - c.startPosition:], text: c.text[position-c.startPosition:],
} }
} }

@ -31,10 +31,10 @@ func TestDocument_TextBeforeCursor(t *testing.T) {
} }
func TestDocument_TextAfterCursor(t *testing.T) { func TestDocument_TextAfterCursor(t *testing.T) {
pattern := []struct{ pattern := []struct {
document *Document document *Document
expected string expected string
} { }{
{ {
document: &Document{ document: &Document{
Text: "line 1\nline 2\nline 3\nline 4\n", Text: "line 1\nline 2\nline 3\nline 4\n",
@ -60,10 +60,10 @@ func TestDocument_TextAfterCursor(t *testing.T) {
} }
func TestDocument_GetWordBeforeCursor(t *testing.T) { func TestDocument_GetWordBeforeCursor(t *testing.T) {
pattern := []struct{ pattern := []struct {
document *Document document *Document
expected string expected string
} { }{
{ {
document: &Document{ document: &Document{
Text: "apple bana", Text: "apple bana",
@ -89,10 +89,10 @@ func TestDocument_GetWordBeforeCursor(t *testing.T) {
} }
func TestDocument_FindStartOfPreviousWord(t *testing.T) { func TestDocument_FindStartOfPreviousWord(t *testing.T) {
pattern := []struct{ pattern := []struct {
document *Document document *Document
expected int expected int
} { }{
{ {
document: &Document{ document: &Document{
Text: "apple bana", Text: "apple bana",

@ -1,9 +1,9 @@
package prompt package prompt
import ( import (
"syscall"
"os" "os"
"os/signal" "os/signal"
"syscall"
"time" "time"
) )
@ -23,7 +23,6 @@ func (p *Prompt) Run() {
p.setUp() p.setUp()
defer p.tearDown() defer p.tearDown()
bufCh := make(chan []byte, 128) bufCh := make(chan []byte, 128)
go readBuffer(bufCh) go readBuffer(bufCh)
@ -33,7 +32,7 @@ func (p *Prompt) Run() {
for { for {
select { select {
case b := <- bufCh: case b := <-bufCh:
p.renderer.Erase(p.buf) p.renderer.Erase(p.buf)
ac := p.in.GetASCIICode(b) ac := p.in.GetASCIICode(b)
if ac == nil { if ac == nil {
@ -42,7 +41,7 @@ func (p *Prompt) Run() {
res := p.executor(p.buf) res := p.executor(p.buf)
p.renderer.BreakLine(p.buf, res) p.renderer.BreakLine(p.buf, res)
p.buf = NewBuffer() p.buf = NewBuffer()
} else if ac.Key == ControlC { } else if ac.Key == ControlC || ac.Key == ControlD {
return return
} else { } else {
InputHandler(ac, p.buf) InputHandler(ac, p.buf)
@ -50,9 +49,9 @@ func (p *Prompt) Run() {
completions := p.completer(p.buf) completions := p.completer(p.buf)
p.renderer.Render(p.buf, completions) p.renderer.Render(p.buf, completions)
case w := <- winSizeCh: case w := <-winSizeCh:
p.renderer.UpdateWinSize(w) p.renderer.UpdateWinSize(w)
case e := <- exitCh: case e := <-exitCh:
if e { if e {
return return
} }
@ -119,16 +118,17 @@ 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{ return &Prompt{
in: NewVT100Parser(), in: NewVT100Parser(),
renderer: &Render{ renderer: &Render{
Prefix: ">>> ", Prefix: ">>> ",
out: NewVT100Writer(), out: NewVT100Writer(),
maxCompletions: maxCompletions,
}, },
title: "Hello! this is prompt toolkit", title: "Hello! this is prompt toolkit",
buf: NewBuffer(), buf: NewBuffer(),
executor: executor, executor: executor,
completer: completer, completer: completer,
} }
} }

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

@ -1,12 +1,12 @@
package prompt package prompt
import ( import (
"testing"
"reflect" "reflect"
"testing"
) )
func TestFormatCompletion(t *testing.T) { func TestFormatCompletion(t *testing.T) {
in := []string { in := []string{
"select", "select",
"from", "from",
"insert", "insert",