diff --git a/Makefile b/Makefile index bf4b172..2783cde 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,3 @@ -NAME := prompt - .DEFAULT_GOAL := help .PHONY: setup diff --git a/README.md b/README.md index f46d781..168c215 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ -# go-prompt-toolkit +# go-prompt ![demo](./_resources/demo.gif) -Library for building powerful interactive command lines in Golang. +Library for building powerful interactive prompt in Go, inspired by python-prompt-toolkit. #### Similar Projects -* [jonathanslenders/python-prompt-toolkit](https://github.com/jonathanslenders/python-prompt-toolkit): **go-prompt-toolkit** is inspired by this library. +* [jonathanslenders/python-prompt-toolkit](https://github.com/jonathanslenders/python-prompt-toolkit): **go-prompt** is inspired by this library. * [peterh/liner](https://github.com/peterh/liner): The most similar project in golang is **liner** that I've ever seen. -#### Projects using go-prompt-toolkit +#### Projects using go-prompt -* [kube-prompt : An interactive kubernetes client featuring autocomplete using prompt-toolkit.](https://github.com/c-bata/kube-prompt) +* [kube-prompt : An interactive kubernetes client featuring auto-complete written in Go.](https://github.com/c-bata/kube-prompt) ## Getting Started @@ -19,42 +19,35 @@ Library for building powerful interactive command lines in Golang. package main import ( - "fmt" - "time" + "fmt" - "github.com/c-bata/go-prompt-toolkit/prompt" + "github.com/c-bata/go-prompt" ) // executor executes command and print the output. -// 1. Execute sql -// 2. Get response and print it -func executor(in string) { - out := "something response from db." - fmt.Println(out) - return +func executor(in string) { + fmt.Println("Your input: " + in) } // completer returns the completion items from user input. func completer(in string) []prompt.Suggest { - return []primpt.Suggest{ - {Text: "users", Description: "user collections."}, - {Text: "articles", Description: "article is posted by users."}, - {Text: "comments", Description: "comment is inserted with each articles."}, - {Text: "groups", Description: "group is the collection of users."}, - {Text: "tags", Description: "tag contains hash tag like #prompt"}, - } + s := []prompt.Suggest{ + {Text: "users", Description: "user table"}, + {Text: "sites", Description: "sites table"}, + {Text: "articles", Description: "articles table"}, + {Text: "comments", Description: "comments table"}, + } + return prompt.FilterHasPrefix(s, in, true) } func main() { - pt := prompt.NewPrompt( - executor, - completer, - prompt.OptionTitle("sqlite3-prompt"), - prompt.OptionPrefix(">>> "), - prompt.OptionPrefixColor(prompt.Blue), - ) - defer fmt.Println("\nGoodbye!") - pt.Run() + p := prompt.New( + executor, + completer, + prompt.OptionPrefix(">>> "), + prompt.OptionTitle("sql-prompt"), + ) + p.Run() } ``` diff --git a/_example/echo/main.go b/_example/echo/main.go index 1b4da64..a9517df 100644 --- a/_example/echo/main.go +++ b/_example/echo/main.go @@ -3,29 +3,29 @@ package main import ( "fmt" - "github.com/c-bata/go-prompt-toolkit" + "github.com/c-bata/go-prompt" ) -func executor(t string) { - fmt.Println("Your input: " + t) +func executor(in string) { + fmt.Println("Your input: " + in) } -func completer(t string) []prompt.Suggest { - return []prompt.Suggest{ +func completer(in string) []prompt.Suggest { + s := []prompt.Suggest{ {Text: "users", Description: "user table"}, {Text: "sites", Description: "sites table"}, {Text: "articles", Description: "articles table"}, {Text: "comments", Description: "comments table"}, } + return prompt.FilterHasPrefix(s, in, true) } func main() { - pt := prompt.NewPrompt( + p := prompt.New( executor, completer, prompt.OptionPrefix(">>> "), - prompt.OptionTitle("sqlite3-cli"), + prompt.OptionTitle("sql-prompt"), ) - defer fmt.Println("\nGoodbye!") - pt.Run() + p.Run() } diff --git a/_example/exec-command/main.go b/_example/exec-command/main.go index 676b7e7..14e7016 100644 --- a/_example/exec-command/main.go +++ b/_example/exec-command/main.go @@ -5,7 +5,7 @@ import ( "os/exec" "os" - "github.com/c-bata/go-prompt-toolkit" + "github.com/c-bata/go-prompt" ) func executor(t string) { @@ -26,10 +26,9 @@ func completer(t string) []prompt.Suggest { } func main() { - pt := prompt.NewPrompt( + p := prompt.New( executor, completer, ) - defer fmt.Println("\nGoodbye!") - pt.Run() + p.Run() } diff --git a/_example/sleep/main.go b/_example/sleep/main.go index b45f155..992ca7c 100644 --- a/_example/sleep/main.go +++ b/_example/sleep/main.go @@ -6,7 +6,7 @@ import ( "context" "os/exec" - "github.com/c-bata/go-prompt-toolkit" + "github.com/c-bata/go-prompt" ) func executor(t string) { @@ -16,11 +16,9 @@ func executor(t string) { if t == "sleep 5s" { cmd := exec.CommandContext(ctx, "sleep", "5") cmd.Run() - fmt.Println("Foo") } else if t == "sleep 20s" { cmd := exec.CommandContext(ctx, "sleep", "20") cmd.Run() - fmt.Println("Foo") } return } @@ -33,10 +31,10 @@ func completer(t string) []prompt.Suggest { } func main() { - pt := prompt.NewPrompt( + p := prompt.New( executor, completer, ) defer fmt.Println("\nGoodbye!") - pt.Run() + p.Run() } diff --git a/_example/sqlite3-cli/.gitignore b/_example/sqlite3-cli/.gitignore deleted file mode 100644 index ba520cc..0000000 --- a/_example/sqlite3-cli/.gitignore +++ /dev/null @@ -1 +0,0 @@ -db.sqlite3 \ No newline at end of file diff --git a/_example/sqlite3-cli/README.md b/_example/sqlite3-cli/README.md deleted file mode 100644 index 2fc1d2e..0000000 --- a/_example/sqlite3-cli/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# sqlite3-cli - -## Prepare database. - -```console -$ sqlite3 db.sqlite3 -sqlite> .header on -sqlite> .mode column -sqlite> create table tasks ( - ...> id integer primary key, - ...> name text - ...> ); -sqlite> select * from tasks; -sqlite> insert into tasks(name) values('Create go-prompt-toolkit.'); -sqlite> insert into tasks(name) values('Use sqlite3 from golang'); -sqlite> select * from tasks; -id name ----------- ---------------------- -1 Create go-prompt-toolkit. -2 Use sqlite3 from golang -sqlite> .quit -``` - diff --git a/_example/sqlite3-cli/main.go b/_example/sqlite3-cli/main.go deleted file mode 100644 index bb99090..0000000 --- a/_example/sqlite3-cli/main.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - - _ "github.com/mattn/go-sqlite3" -) - -func main() { - db, err := sql.Open("sqlite3", "./db.sqlite3") - if err != nil { - fmt.Println("Cannot open db.sqlite3.") - return - } - - rows, err := db.Query("select id, name from tasks") - if err != nil { - fmt.Println(err) - return - } - defer rows.Close() - - for rows.Next() { - var id int - var name string - rows.Scan(&id, &name) - fmt.Println(id, name) - } -} diff --git a/_tools/erasing.go b/_tools/erasing.go index acb24a0..2047227 100644 --- a/_tools/erasing.go +++ b/_tools/erasing.go @@ -1,6 +1,6 @@ package main -import "github.com/c-bata/go-prompt-toolkit" +import "github.com/c-bata/go-prompt" func main() { l := 20 diff --git a/_tools/vt100_debug.go b/_tools/vt100_debug.go index 9feb606..6625ed5 100644 --- a/_tools/vt100_debug.go +++ b/_tools/vt100_debug.go @@ -4,7 +4,7 @@ import ( "fmt" "syscall" - "github.com/c-bata/go-prompt-toolkit" + "github.com/c-bata/go-prompt" "github.com/pkg/term/termios" ) @@ -13,17 +13,17 @@ const fd = 0 var orig syscall.Termios func SetRawMode() { - var new syscall.Termios + var n syscall.Termios if err := termios.Tcgetattr(uintptr(fd), &orig); err != nil { fmt.Println("Failed to get attribute") return } - new = orig + n = orig // "&^=" used like: https://play.golang.org/p/8eJw3JxS4O - new.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG - new.Cc[syscall.VMIN] = 1 - new.Cc[syscall.VTIME] = 0 - termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*syscall.Termios)(&new)) + n.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG + n.Cc[syscall.VMIN] = 1 + n.Cc[syscall.VTIME] = 0 + termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*syscall.Termios)(&n)) } func Restore() { diff --git a/option.go b/option.go index 2e4ecb2..7e83d6a 100644 --- a/option.go +++ b/option.go @@ -137,7 +137,7 @@ func OptionMaxCompletions(x uint16) option { } } -func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt { +func New(executor Executor, completer Completer, opts ...option) *Prompt { pt := &Prompt{ in: &VT100Parser{fd: syscall.Stdin}, renderer: &Render{