From b362b2070ccfa4559b94e18d5662be96580f4c33 Mon Sep 17 00:00:00 2001 From: Ritesh Pradhan Date: Fri, 22 Sep 2017 13:07:47 -0400 Subject: [PATCH 1/8] * add Stringln for multi spaced sentence * add ChooseInterface for list of variable types * examples added --- History.md | 8 ++++ examples/list-interface.go | 31 +++++++++++++++ examples/string-line.go | 18 +++++++++ prompt.go | 77 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 examples/list-interface.go create mode 100644 examples/string-line.go diff --git a/History.md b/History.md index ef79ef1..8d293cd 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,11 @@ +v2.0.0 / 2017-09-22 +=================== + + * add Stringln for multi spaced sentence + * add ChooseInterface for list of variable types + * examples added + + v1.2.0 / 2014-12-10 =================== diff --git a/examples/list-interface.go b/examples/list-interface.go new file mode 100644 index 0000000..92b5a0b --- /dev/null +++ b/examples/list-interface.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + + "ritesh.pradhan/go-prompt" +) + +// MyFriend is custom type +type MyFriend struct { + Name string + Age int +} + +func createListInterface() []interface{} { + friend1 := MyFriend{Name: "Matt", Age: 33} + friend2 := MyFriend{Name: "Jing", Age: 25} + friend3 := MyFriend{Name: "Arya", Age: 20} + + return []interface{}{ + friend1, + friend2, + friend3, + } +} + +func main() { + myList := createListInterface() + i := prompt.ChooseInterface("What's your favorite friend?", myList) + fmt.Printf("picked: %+v\n", myList[i]) +} diff --git a/examples/string-line.go b/examples/string-line.go new file mode 100644 index 0000000..6f1d6ce --- /dev/null +++ b/examples/string-line.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "os" + + "bufio" + + prompt "github.com/segmentio/go-prompt" +) + +func main() { + println("Getting Started!") + scanner := bufio.NewScanner(os.Stdin) + longLine := prompt.Stringln(scanner, "Enter a long senctence") + singleWord := prompt.String("Single Word") + fmt.Printf("\nHello \n long: %s \n short: %s\n", longLine, singleWord) +} diff --git a/prompt.go b/prompt.go index 412736e..0a3af5d 100644 --- a/prompt.go +++ b/prompt.go @@ -1,9 +1,13 @@ package prompt -import "github.com/howeyc/gopass" -import "strings" -import "strconv" -import "fmt" +import ( + "bufio" + "fmt" + "strconv" + "strings" + + "github.com/howeyc/gopass" +) // String prompt. func String(prompt string, args ...interface{}) string { @@ -21,6 +25,40 @@ func StringRequired(prompt string, args ...interface{}) (s string) { return s } +// String default (required). +func StringDefault(prompt string, dvalue string) (s string) { + fmt.Println() + fmt.Printf("%s (default value: %s)", prompt, dvalue) + fmt.Scanln(&s) + if strings.Trim(s, " ") == "" { + return dvalue + } + return s +} + +// Stringln reads multi spaced sentence until newline +func Stringln(scanner *bufio.Scanner, prompt string) string { + fmt.Printf(prompt + ": ") + scanner.Scan() + return scanner.Text() +} + +// Integer prompt (required). +func IntegerRequired(prompt string, args ...interface{}) (in int) { + fmt.Println() + in = 0 + for { + s := String(prompt) + n, err := strconv.Atoi(s) + if err != nil { + continue + } + in = n + break + } + return +} + // Confirm continues prompting until the input is boolean-ish. func Confirm(prompt string, args ...interface{}) bool { for { @@ -67,6 +105,37 @@ func Choose(prompt string, list []string) int { return i } +// Choose prompts for a single selection from `list`, returning in the index. +func ChooseInterface(prompt string, list []interface{}) int { + fmt.Println() + for i, val := range list { + fmt.Printf(" %d) %+v\n", i+1, val) + } + + fmt.Println() + i := -1 + + for { + s := String(prompt) + + // index + n, err := strconv.Atoi(s) + if err != nil { + continue + } + + if n > 0 && n <= len(list) { + i = n - 1 + break + } else { + continue + } + + } + + return i +} + // Password prompt. func Password(prompt string, args ...interface{}) string { fmt.Printf(prompt+": ", args...) From a5270a66e619e528673ab1035788b353157a3d94 Mon Sep 17 00:00:00 2001 From: Ritesh Pradhan Date: Fri, 22 Sep 2017 13:18:49 -0400 Subject: [PATCH 2/8] * add examples with default string value --- examples/list-interface.go | 2 +- examples/string.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/list-interface.go b/examples/list-interface.go index 92b5a0b..c12d0ad 100644 --- a/examples/list-interface.go +++ b/examples/list-interface.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "ritesh.pradhan/go-prompt" + prompt "github.com/segmentio/go-prompt" ) // MyFriend is custom type diff --git a/examples/string.go b/examples/string.go index 8979e79..faf3ea0 100644 --- a/examples/string.go +++ b/examples/string.go @@ -10,5 +10,7 @@ func main() { println("need your name!") first := prompt.String("first") last := prompt.String("last") - fmt.Printf("\nHello %s %s\n", first, last) + def := prompt.StringDefault("Want default", "defaultValue") + fmt.Printf("\nHello %s %s \n", first, last) + fmt.Println("Default value is : ", def) } From d6952e28294f057d150b00aeb735b2b49d84b39d Mon Sep 17 00:00:00 2001 From: Ritesh Pradhan Date: Tue, 24 Oct 2017 10:52:58 -0400 Subject: [PATCH 3/8] update readme changed readme --- Readme.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index af14a05..c210034 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,6 @@ +forked from segmentio/go-prompt + # go-prompt Terminal prompts for Go. @@ -7,6 +9,7 @@ ## Example +1. ```go package main @@ -28,6 +31,24 @@ func main() { } ``` -## License +2. +``` +package main + +import ( + "fmt" + "os" + + "bufio" - MIT \ No newline at end of file + prompt "github.com/segmentio/go-prompt" +) + +func main() { + println("Getting Started!") + scanner := bufio.NewScanner(os.Stdin) + longLine := prompt.Stringln(scanner, "Enter a long senctence") + singleWord := prompt.String("Single Word") + fmt.Printf("\nHello \n long: %s \n short: %s\n", longLine, singleWord) +} +``` From da04a62374f79cff06dca7f239b331105aa722d5 Mon Sep 17 00:00:00 2001 From: Ritesh Pradhan Date: Tue, 24 Oct 2017 10:53:39 -0400 Subject: [PATCH 4/8] update readmen go color coding --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c210034..46e49ae 100644 --- a/Readme.md +++ b/Readme.md @@ -32,7 +32,7 @@ func main() { ``` 2. -``` +```go package main import ( From 2ab9789659d39684d70e94c37dd5d4215260efca Mon Sep 17 00:00:00 2001 From: Mohammad Mehdi Habibi Date: Mon, 6 Nov 2017 17:05:58 +0100 Subject: [PATCH 5/8] Add Integer func and refactor IntegerRequired (#1) --- prompt.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/prompt.go b/prompt.go index 0a3af5d..aebca9f 100644 --- a/prompt.go +++ b/prompt.go @@ -44,19 +44,20 @@ func Stringln(scanner *bufio.Scanner, prompt string) string { } // Integer prompt (required). -func IntegerRequired(prompt string, args ...interface{}) (in int) { - fmt.Println() - in = 0 +func IntegerRequired(prompt string, args ...interface{}) int { for { - s := String(prompt) - n, err := strconv.Atoi(s) - if err != nil { - continue + n := Integer(prompt, args...) + if n != 0 { + return n } - in = n - break } - return +} + +// Integer prompt +func Integer(prompt string, args ...interface{}) int { + s := String(prompt, args...) + n, _ := strconv.Atoi(s) + return n } // Confirm continues prompting until the input is boolean-ish. From 632fa8602a92e4f6bd2093cb619fec6ea99b846b Mon Sep 17 00:00:00 2001 From: Mohammad Mehdi Habibi Date: Tue, 7 Nov 2017 20:58:52 +0100 Subject: [PATCH 6/8] printing prompt message separated as a function. For a better understanding, renamed prompt variables to msg (#2) --- prompt.go | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/prompt.go b/prompt.go index aebca9f..1b98361 100644 --- a/prompt.go +++ b/prompt.go @@ -10,25 +10,24 @@ import ( ) // String prompt. -func String(prompt string, args ...interface{}) string { +func String(msg string, args ...interface{}) string { var s string - fmt.Printf(prompt+": ", args...) + prompt(msg, args...) fmt.Scanln(&s) return s } // String prompt (required). -func StringRequired(prompt string, args ...interface{}) (s string) { +func StringRequired(msg string, args ...interface{}) (s string) { for strings.Trim(s, " ") == "" { - s = String(prompt, args...) + s = String(msg, args...) } return s } // String default (required). -func StringDefault(prompt string, dvalue string) (s string) { - fmt.Println() - fmt.Printf("%s (default value: %s)", prompt, dvalue) +func StringDefault(msg string, dvalue string) (s string) { + fmt.Printf("%s (default value: %s)", msg, dvalue) fmt.Scanln(&s) if strings.Trim(s, " ") == "" { return dvalue @@ -37,16 +36,16 @@ func StringDefault(prompt string, dvalue string) (s string) { } // Stringln reads multi spaced sentence until newline -func Stringln(scanner *bufio.Scanner, prompt string) string { - fmt.Printf(prompt + ": ") +func Stringln(scanner *bufio.Scanner, msg string) string { + prompt(msg) scanner.Scan() return scanner.Text() } // Integer prompt (required). -func IntegerRequired(prompt string, args ...interface{}) int { +func IntegerRequired(msg string, args ...interface{}) int { for { - n := Integer(prompt, args...) + n := Integer(msg, args...) if n != 0 { return n } @@ -54,16 +53,16 @@ func IntegerRequired(prompt string, args ...interface{}) int { } // Integer prompt -func Integer(prompt string, args ...interface{}) int { - s := String(prompt, args...) +func Integer(msg string, args ...interface{}) int { + s := String(msg, args...) n, _ := strconv.Atoi(s) return n } // Confirm continues prompting until the input is boolean-ish. -func Confirm(prompt string, args ...interface{}) bool { +func Confirm(msg string, args ...interface{}) bool { for { - switch String(prompt, args...) { + switch String(msg, args...) { case "Yes", "yes", "y", "Y": return true case "No", "no", "n", "N": @@ -73,7 +72,7 @@ func Confirm(prompt string, args ...interface{}) bool { } // Choose prompts for a single selection from `list`, returning in the index. -func Choose(prompt string, list []string) int { +func Choose(msg string, list []string) int { fmt.Println() for i, val := range list { fmt.Printf(" %d) %s\n", i+1, val) @@ -83,7 +82,7 @@ func Choose(prompt string, list []string) int { i := -1 for { - s := String(prompt) + s := String(msg) // index n, err := strconv.Atoi(s) @@ -107,7 +106,7 @@ func Choose(prompt string, list []string) int { } // Choose prompts for a single selection from `list`, returning in the index. -func ChooseInterface(prompt string, list []interface{}) int { +func ChooseInterface(msg string, list []interface{}) int { fmt.Println() for i, val := range list { fmt.Printf(" %d) %+v\n", i+1, val) @@ -117,7 +116,7 @@ func ChooseInterface(prompt string, list []interface{}) int { i := -1 for { - s := String(prompt) + s := String(msg) // index n, err := strconv.Atoi(s) @@ -138,16 +137,16 @@ func ChooseInterface(prompt string, list []interface{}) int { } // Password prompt. -func Password(prompt string, args ...interface{}) string { - fmt.Printf(prompt+": ", args...) +func Password(msg string, args ...interface{}) string { + prompt(msg, args...) password, _ := gopass.GetPasswd() s := string(password[0:]) return s } // Password prompt with mask. -func PasswordMasked(prompt string, args ...interface{}) string { - fmt.Printf(prompt+": ", args...) +func PasswordMasked(msg string, args ...interface{}) string { + prompt(msg, args...) password, _ := gopass.GetPasswdMasked() s := string(password[0:]) return s @@ -162,3 +161,7 @@ func indexOf(s string, list []string) int { } return -1 } + +func prompt(msg string, args ...interface{}) { + fmt.Printf(msg+": ", args...) +} From d5310b3dbd9b1dc7b52af0ec3b5d09fdde20a17b Mon Sep 17 00:00:00 2001 From: Mohammad Mehdi Habibi Date: Tue, 7 Nov 2017 21:01:21 +0100 Subject: [PATCH 7/8] Update readme (#3) * Update readme * fix --- Readme.md | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index 46e49ae..e152697 100644 --- a/Readme.md +++ b/Readme.md @@ -7,13 +7,12 @@ forked from segmentio/go-prompt View the [docs](http://godoc.org/pkg/github.com/segmentio/go-prompt). -## Example +## How to use: -1. ```go package main -import "github.com/segmentio/go-prompt" +import "github.com/riteshpradhan/go-prompt" var langs = []string{ "c", @@ -31,24 +30,15 @@ func main() { } ``` -2. -```go -package main - -import ( - "fmt" - "os" - - "bufio" - - prompt "github.com/segmentio/go-prompt" -) - -func main() { - println("Getting Started!") - scanner := bufio.NewScanner(os.Stdin) - longLine := prompt.Stringln(scanner, "Enter a long senctence") - singleWord := prompt.String("Single Word") - fmt.Printf("\nHello \n long: %s \n short: %s\n", longLine, singleWord) -} -``` +## Functions: +* String +* StringRequired +* StringDefault +* Stringln +* Integer +* IntegerRequired +* Confirm +* Choose +* ChooseInterface +* Password +* PasswordMasked From 04fd3290fe2240eb21d9fb2f196ee3733b6f74b7 Mon Sep 17 00:00:00 2001 From: Ritesh Pradhan Date: Tue, 7 Nov 2017 15:06:49 -0500 Subject: [PATCH 8/8] examples updated --- examples/confirm.go | 2 +- examples/list-interface.go | 2 +- examples/list.go | 2 +- examples/passwords.go | 2 +- examples/string-line.go | 2 +- examples/string-required.go | 2 +- examples/string.go | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/confirm.go b/examples/confirm.go index 315fa88..044bfda 100644 --- a/examples/confirm.go +++ b/examples/confirm.go @@ -1,6 +1,6 @@ package main -import prompt "github.com/segmentio/go-prompt" +import "github.com/riteshpradhan/go-prompt" func main() { if ok := prompt.Confirm("launch %s?", "something"); ok { diff --git a/examples/list-interface.go b/examples/list-interface.go index c12d0ad..bd7c960 100644 --- a/examples/list-interface.go +++ b/examples/list-interface.go @@ -3,7 +3,7 @@ package main import ( "fmt" - prompt "github.com/segmentio/go-prompt" + "github.com/riteshpradhan/go-prompt" ) // MyFriend is custom type diff --git a/examples/list.go b/examples/list.go index 94d7e4c..66afd2d 100644 --- a/examples/list.go +++ b/examples/list.go @@ -1,6 +1,6 @@ package main -import prompt "github.com/segmentio/go-prompt" +import "github.com/riteshpradhan/go-prompt" var langs = []string{ "c", diff --git a/examples/passwords.go b/examples/passwords.go index 5696d4f..04b17b9 100644 --- a/examples/passwords.go +++ b/examples/passwords.go @@ -1,6 +1,6 @@ package main -import prompt "github.com/segmentio/go-prompt" +import "github.com/riteshpradhan/go-prompt" func main() { { diff --git a/examples/string-line.go b/examples/string-line.go index 6f1d6ce..85757cd 100644 --- a/examples/string-line.go +++ b/examples/string-line.go @@ -6,7 +6,7 @@ import ( "bufio" - prompt "github.com/segmentio/go-prompt" + "github.com/riteshpradhan/go-prompt" ) func main() { diff --git a/examples/string-required.go b/examples/string-required.go index b38338f..a75e206 100644 --- a/examples/string-required.go +++ b/examples/string-required.go @@ -3,7 +3,7 @@ package main import ( "fmt" - prompt "github.com/segmentio/go-prompt" + "github.com/riteshpradhan/go-prompt" ) func main() { diff --git a/examples/string.go b/examples/string.go index faf3ea0..b564ad4 100644 --- a/examples/string.go +++ b/examples/string.go @@ -3,7 +3,7 @@ package main import ( "fmt" - prompt "github.com/segmentio/go-prompt" + "github.com/riteshpradhan/go-prompt" ) func main() {