From ffe73ca679a08d732b678230f578dec6e66d6da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=88=D2=88=D2=88Luiz=20Branco?= Date: Thu, 23 Nov 2017 14:26:04 -0400 Subject: [PATCH] Improve documentation (#27) --- README.md | 70 ++++++++++++++++++++++++++++++--- _examples/custom_select/main.go | 2 +- _examples/prompt/main.go | 6 ++- _examples/select/main.go | 9 ++--- 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index eddc500..a11294d 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,66 @@ Interactive prompt for command-line applications. [![Go Report Card](https://goreportcard.com/badge/github.com/manifoldco/promptui)](https://goreportcard.com/report/github.com/manifoldco/promptui) [![License](https://img.shields.io/badge/license-BSD-blue.svg)](./LICENSE.md) -## Usage +## Overview -### Selection +![promptui](https://media.giphy.com/media/xUNda0Ngb5qsogLsBi/giphy.gif) + +Promptui is a library providing a simple interface to create command-line +prompts for go. It can be easily integrated into +[spf13/cobra](https://github.com/spf13/cobra), +[urfave/cli](https://github.com/urfave/cli) or any cli go application. + +Promptui has two main input modes: + +- `Prompt` provides a single line for user input. Prompt supports + optional live validation, confirmation and masking the input. + +- `Select` provides a list of options to choose from. Select supports + pagination, search, detailed view and custom templates. + +For a full list of options check [GoDoc](https://godoc.org/github.com/manifoldco/promptui). + +## Basic Usage + +### Prompt + +```go +package main + +import ( + "errors" + "fmt" + "strconv" + + "github.com/manifoldco/promptui" +) + +func main() { + validate := func(input string) error { + _, err := strconv.ParseFloat(input, 64) + if err != nil { + return errors.New("Invalid number") + } + return nil + } + + prompt := promptui.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) +} +``` + +### Select ```go package main @@ -25,11 +82,10 @@ import ( ) func main() { - values := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"} - prompt := promptui.Select{ Label: "Select Day", - Items: values, + Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", + "Saturday", "Sunday"}, } _, result, err := prompt.Run() @@ -42,3 +98,7 @@ func main() { fmt.Printf("You choose %q\n", result) } ``` + +### More Examples + +See full list of [examples](https://github.com/manifoldco/promptui/tree/master/_examples) diff --git a/_examples/custom_select/main.go b/_examples/custom_select/main.go index bd26839..c2078bc 100644 --- a/_examples/custom_select/main.go +++ b/_examples/custom_select/main.go @@ -62,5 +62,5 @@ func main() { return } - fmt.Printf("You choose number %d: %+v\n", i+1, peppers[i]) + fmt.Printf("You choose number %d: %s\n", i+1, peppers[i].Name) } diff --git a/_examples/prompt/main.go b/_examples/prompt/main.go index 4d409c5..e7e7509 100644 --- a/_examples/prompt/main.go +++ b/_examples/prompt/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "strconv" @@ -10,7 +11,10 @@ import ( func main() { validate := func(input string) error { _, err := strconv.ParseFloat(input, 64) - return err + if err != nil { + return errors.New("Invalid number") + } + return nil } prompt := promptui.Prompt{ diff --git a/_examples/select/main.go b/_examples/select/main.go index 3078b72..c25aff3 100644 --- a/_examples/select/main.go +++ b/_examples/select/main.go @@ -7,11 +7,10 @@ import ( ) func main() { - values := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} - prompt := promptui.Select{ - Label: "Select Number", - Items: values, + Label: "Select Day", + Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", + "Saturday", "Sunday"}, } _, result, err := prompt.Run() @@ -21,5 +20,5 @@ func main() { return } - fmt.Printf("You choose %s\n", result) + fmt.Printf("You choose %q\n", result) }