forked from tigergraph/promptui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved documentation further and the SelectWithAdd example
- Loading branch information
minivera
committed
Jul 19, 2018
1 parent
170b792
commit 5ff80d1
Showing
14 changed files
with
296 additions
and
171 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
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,54 @@ | ||
package promptui | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// This is an example for the Prompt mode of promptui. In this example, a prompt is created | ||
// with a validator function that validates the given value to make sure its a number. | ||
// If successful, it will output the chosen number in a formatted message. | ||
func Example() { | ||
validate := func(input string) error { | ||
_, err := strconv.ParseFloat(input, 64) | ||
if err != nil { | ||
return errors.New("Invalid number") | ||
} | ||
return nil | ||
} | ||
|
||
prompt := Prompt{ | ||
Label: "Number", | ||
Validate: validate, | ||
} | ||
|
||
result, err := prompt.Run() | ||
|
||
if err != nil { | ||
fmt.Printf("Prompt failed %v\n", err) | ||
return | ||
} | ||
|
||
fmt.Printf("You choose %q\n", result) | ||
} | ||
|
||
// This is an example for the Select mode of promptui. In this example, a select is created with | ||
// the days of the week as its items. When an item is selected, the selected day will be displayed | ||
// in a formatted message. | ||
func Example_aux() { | ||
prompt := Select{ | ||
Label: "Select Day", | ||
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | ||
"Saturday", "Sunday"}, | ||
} | ||
|
||
_, result, err := prompt.Run() | ||
|
||
if err != nil { | ||
fmt.Printf("Prompt failed %v\n", err) | ||
return | ||
} | ||
|
||
fmt.Printf("You choose %q\n", result) | ||
} |
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,42 @@ | ||
package promptui | ||
|
||
import ( | ||
"strconv" | ||
"fmt" | ||
) | ||
|
||
// This example shows how to use the prompt validator and templates to create a stylized prompt. | ||
// The validator will make sure the value entered is a parseable float while the templates will | ||
// color the value to show validity. | ||
func ExamplePrompt() { | ||
// The validate function follows the required validator signature. | ||
validate := func(input string) error { | ||
_, err := strconv.ParseFloat(input, 64) | ||
return err | ||
} | ||
|
||
// Each template displays the data received from the prompt with some formatting. | ||
templates := &PromptTemplates{ | ||
Prompt: "{{ . }} ", | ||
Valid: "{{ . | green }} ", | ||
Invalid: "{{ . | red }} ", | ||
Success: "{{ . | bold }} ", | ||
} | ||
|
||
prompt := Prompt{ | ||
Label: "Spicy Level", | ||
Templates: templates, | ||
Validate: validate, | ||
} | ||
|
||
result, err := prompt.Run() | ||
|
||
if err != nil { | ||
fmt.Printf("Prompt failed %v\n", err) | ||
return | ||
} | ||
|
||
// The result of the prompt, if valid, is displayed in a formatted message. | ||
fmt.Printf("You answered %s\n", result) | ||
} | ||
|
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,75 @@ | ||
package promptui | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Any type can be given to the select's item as long as the templates properly implement the dot notation | ||
// to display it. | ||
type pepper struct { | ||
Name string | ||
HeatUnit int | ||
Peppers int | ||
} | ||
|
||
// This examples shows a complex and customized select. | ||
func ExampleSelect() { | ||
// The select will show a series of peppers stored inside a slice of structs. To display the content of the struct, | ||
// the usual dot notation is used inside the templates to select the fields and color them. | ||
peppers := []pepper{ | ||
{Name: "Bell Pepper", HeatUnit: 0, Peppers: 0}, | ||
{Name: "Banana Pepper", HeatUnit: 100, Peppers: 1}, | ||
{Name: "Poblano", HeatUnit: 1000, Peppers: 2}, | ||
{Name: "Jalapeño", HeatUnit: 3500, Peppers: 3}, | ||
{Name: "Aleppo", HeatUnit: 10000, Peppers: 4}, | ||
{Name: "Tabasco", HeatUnit: 30000, Peppers: 5}, | ||
{Name: "Malagueta", HeatUnit: 50000, Peppers: 6}, | ||
{Name: "Habanero", HeatUnit: 100000, Peppers: 7}, | ||
{Name: "Red Savina Habanero", HeatUnit: 350000, Peppers: 8}, | ||
{Name: "Dragon’s Breath", HeatUnit: 855000, Peppers: 9}, | ||
} | ||
|
||
// The Active and Selected templates set a small pepper icon next to the name colored and the heat unit for the | ||
// active template. The details template is show at the bottom of the select's list and displays the full info | ||
// for that pepper in a multi-line template. | ||
templates := &SelectTemplates{ | ||
Label: "{{ . }}?", | ||
Active: "\U0001F336 {{ .Name | cyan }} ({{ .HeatUnit | red }})", | ||
Inactive: " {{ .Name | cyan }} ({{ .HeatUnit | red }})", | ||
Selected: "\U0001F336 {{ .Name | red | cyan }}", | ||
Details: ` | ||
--------- Pepper ---------- | ||
{{ "Name:" | faint }} {{ .Name }} | ||
{{ "Heat Unit:" | faint }} {{ .HeatUnit }} | ||
{{ "Peppers:" | faint }} {{ .Peppers }}`, | ||
} | ||
|
||
// A searcher function is implemented which enabled the search mode for the select. The function follows | ||
// the required searcher signature and finds any pepper whose name contains the searched string. | ||
searcher := func(input string, index int) bool { | ||
pepper := peppers[index] | ||
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1) | ||
input = strings.Replace(strings.ToLower(input), " ", "", -1) | ||
|
||
return strings.Contains(name, input) | ||
} | ||
|
||
prompt := Select{ | ||
Label: "Spicy Level", | ||
Items: peppers, | ||
Templates: templates, | ||
Size: 4, | ||
Searcher: searcher, | ||
} | ||
|
||
i, _, err := prompt.Run() | ||
|
||
if err != nil { | ||
fmt.Printf("Prompt failed %v\n", err) | ||
return | ||
} | ||
|
||
// The selected pepper will be displayed with its name and index in a formatted message. | ||
fmt.Printf("You choose number %d: %s\n", i+1, peppers[i].Name) | ||
} |
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,33 @@ | ||
package promptui | ||
|
||
import "fmt" | ||
|
||
// This example shows how to create a SelectWithAdd that will add each new item it is given to the | ||
// list of items until one is chosen. | ||
func ExampleSelectWithAdd() { | ||
items := []string{"Vim", "Emacs", "Sublime", "VSCode", "Atom"} | ||
index := -1 | ||
var result string | ||
var err error | ||
|
||
for index < 0 { | ||
prompt := SelectWithAdd{ | ||
Label: "What's your text editor", | ||
Items: items, | ||
AddLabel: "Add your own", | ||
} | ||
|
||
index, result, err = prompt.Run() | ||
|
||
if index == -1 { | ||
items = append(items, result) | ||
} | ||
} | ||
|
||
if err != nil { | ||
fmt.Printf("Prompt failed %v\n", err) | ||
return | ||
} | ||
|
||
fmt.Printf("You choose %s\n", result) | ||
} |
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
Oops, something went wrong.