-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
2,840 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/demo* | ||
/profile/ | ||
.coverprofile | ||
*.swp | ||
*.log | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/jedib0t/go-pretty/v6/prompt" | ||
"github.com/jedib0t/go-pretty/v6/text" | ||
"github.com/nsf/termbox-go" | ||
) | ||
|
||
var ( | ||
flagDebug = flag.Bool("debug", false, "Write debug logs to prompt.log?") | ||
flagNoTermBox = flag.Bool("no-termbox", false, "Do not use TermBox?") | ||
flagTestTermBox = flag.Bool("test-termbox", false, "Test TermBox?") | ||
) | ||
|
||
func demoPrompt() { | ||
colorFaint := text.FgHiBlack | ||
colorTitle := text.Colors{text.FgHiCyan, text.Bold} | ||
count := 0 | ||
errStop := errors.New("demo.stop") | ||
inputHandler := prompt.Callback(func(userInput string, err error) error { | ||
// handle errors and user exit attempts | ||
if err != nil { | ||
if err == prompt.ErrAbort { | ||
return errStop | ||
} | ||
return err | ||
} else if userInput == "/quit" { | ||
return errStop | ||
} | ||
|
||
// deal with user input: to keep it a simple demo, just print it out | ||
count++ | ||
fmt.Println(colorFaint.Sprintf("userInput: %#v", userInput)) | ||
return nil | ||
}) | ||
timeStart := time.Now() | ||
title := "" + | ||
colorTitle.Sprint("********************************************************************") + "\n" + | ||
colorTitle.Sprint("*** SQL prompt powered by github.com/jedib0t/go-pretty/v6/prompt ***") + "\n" + | ||
colorTitle.Sprint("********************************************************************") + "\n" + | ||
colorFaint.Sprint("(press Ctrl+D or type '/quit' and press Enter to terminate)") | ||
|
||
p := prompt.NewWriter() | ||
if *flagDebug { | ||
p.Debug() | ||
} | ||
if *flagNoTermBox { | ||
p.DoNotUseTermbox() | ||
} | ||
p.SetAutoCompleter(prompt.AutoCompleteSQLKeywords()) | ||
p.SetHistory([]string{ | ||
"SELECT * FROM employees WHERE id = 1;", | ||
"SELECT * FROM employees WHERE\n\tid BETWEEN (1, 101) AND\n\temployees.salary > 13000;", | ||
}) | ||
p.SetStyle(prompt.StyleColored) | ||
p.SetTerminationChecker(prompt.TerminationCheckerSQL) | ||
p.SetTitle(title) | ||
p.Style().SyntaxHighlighter = prompt.SyntaxHighlighterOptionsSQL | ||
defer func() { | ||
fmt.Printf(colorFaint.Sprintf( | ||
"Bye! (evaluated %d command(s) in %s)\n", count, time.Since(timeStart).Round(time.Second), | ||
)) | ||
}() | ||
|
||
if err := p.Render(inputHandler); err != nil { | ||
if !errors.Is(err, errStop) { | ||
fmt.Printf("ERROR: %v\n", err) | ||
os.Exit(-1) | ||
} | ||
} | ||
} | ||
|
||
func testTermBoxPolling() { | ||
err := termbox.Init() | ||
if err != nil { | ||
panic(err) | ||
} | ||
count := 0 | ||
fmt.Printf("Polling for events... (press Ctrl+D to exit)\n") | ||
for { | ||
event := termbox.PollEvent() | ||
count++ | ||
fmt.Printf(">> %#v [ch: %c]\n", event, event.Ch) | ||
if event.Key == termbox.KeyCtrlD { | ||
break | ||
} | ||
} | ||
termbox.Close() | ||
|
||
fmt.Printf("Polling for events... done! [found %d events]\n", count) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *flagTestTermBox { | ||
testTermBoxPolling() | ||
} else { | ||
demoPrompt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Prompt | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/jedib0t/go-pretty/v6/prompt.svg)](https://pkg.go.dev/github.com/jedib0t/go-pretty/v6/prompt) | ||
|
||
A customizable prompt for building your own CLIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package prompt | ||
|
||
// Action defines an activity that is done based on a key sequence. | ||
type Action string | ||
|
||
// Supported Actions. | ||
const ( | ||
None Action = "" // no action | ||
Abort Action = "Abort" // abort the prompt completely and return to caller | ||
|
||
// insert-mode actions | ||
DeleteCharCurrent Action = "DeleteCharCurrent" // delete the character at the cursor | ||
DeleteCharPrevious Action = "DeleteCharPrevious" // delete the character before the cursor | ||
DeleteWordNext Action = "DeleteWordNext" // delete the next work | ||
DeleteWordPrevious Action = "DeleteWordPrevious" // delete the previous word | ||
EraseEverything Action = "EraseEverything" // erase the entire prompt | ||
EraseToBeginningOfLine Action = "EraseToBeginningOfLine" // erase from cursor to the the beginning of current line | ||
EraseToEndOfLine Action = "EraseToEndOfLine" // erase from cursor to the the end of current line | ||
HistoryDown Action = "HistoryDown" // show command executed after current command if any | ||
HistoryUp Action = "HistoryUp" // show previously executed command if any | ||
MakeWordCapitalCase Action = "MakeWordCapitalCase" // make the word at the cursor capitalized | ||
MakeWordLowerCase Action = "MakeWordLowerCase" // make the word at the cursor lower case | ||
MakeWordUpperCase Action = "MakeWordUpperCase" // make the word at the cursor upper case | ||
MoveDownOneLine Action = "MoveDownOneLine" // move the cursor down one line | ||
MoveLeftOneCharacter Action = "MoveLeftOneCharacter" // move the cursor left one character | ||
MoveRightOneCharacter Action = "MoveRightOneCharacter" // move the cursor right one character | ||
MoveUpOneLine Action = "MoveUpOneLine" // move the cursor up one line | ||
MoveToBeginning Action = "MoveToBeginning" // move to the beginning of the entire prompt text | ||
MoveToBeginningOfLine Action = "MoveToBeginningOfLine" // move to the beginning of the current line | ||
MoveToEnd Action = "MoveToEnd" // move to the end of the entire prompt text | ||
MoveToEndOfLine Action = "MoveToEndOfLine" // move to the end of the current line | ||
MoveToWordNext Action = "MoveToWordNext" // move to the beginning of the next word | ||
MoveToWordPrevious Action = "MoveToWordPrevious" // move to the beginning of the previous word | ||
SwapCharacterNext Action = "SwapCharacterNext" // swap the character at the cursor with the next | ||
SwapCharacterPrevious Action = "SwapCharacterPrevious" // swap the character at the cursor with the previous | ||
SwapWordNext Action = "SwapWordNext" // swap the word at the cursor with the next | ||
SwapWordPrevious Action = "SwapWordPrevious" // swap the word at the cursor with the previous | ||
Terminate Action = "Terminate" // trigger the termination checker if any, or call the callback function | ||
|
||
// auto-complete specific actions | ||
AutoCompleteChooseNext Action = "AutoCompleteChooseNext" // choose the next suggestion | ||
AutoCompleteChoosePrevious Action = "AutoCompleteChoosePrevious" // choose the previous suggestion | ||
AutoCompleteHide Action = "AutoCompleteHide" // hide the auto-complete suggestions pop-up | ||
AutoCompleteSelect Action = "AutoCompleteSelect" // select the current suggestion | ||
) |
Oops, something went wrong.