Rename package
This commit is contained in:
parent
17282c599f
commit
b53127c970
53
README.md
53
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()
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
db.sqlite3
|
|
@ -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
|
||||
```
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Reference in New Issue