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/Readme.md b/Readme.md index af14a05..e152697 100644 --- a/Readme.md +++ b/Readme.md @@ -1,16 +1,18 @@ +forked from segmentio/go-prompt + # go-prompt Terminal prompts for Go. View the [docs](http://godoc.org/pkg/github.com/segmentio/go-prompt). -## Example +## How to use: ```go package main -import "github.com/segmentio/go-prompt" +import "github.com/riteshpradhan/go-prompt" var langs = []string{ "c", @@ -28,6 +30,15 @@ func main() { } ``` -## License - - MIT \ No newline at end of file +## Functions: +* String +* StringRequired +* StringDefault +* Stringln +* Integer +* IntegerRequired +* Confirm +* Choose +* ChooseInterface +* Password +* PasswordMasked 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 new file mode 100644 index 0000000..bd7c960 --- /dev/null +++ b/examples/list-interface.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + + "github.com/riteshpradhan/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/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 new file mode 100644 index 0000000..85757cd --- /dev/null +++ b/examples/string-line.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "os" + + "bufio" + + "github.com/riteshpradhan/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/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 8979e79..b564ad4 100644 --- a/examples/string.go +++ b/examples/string.go @@ -3,12 +3,14 @@ package main import ( "fmt" - prompt "github.com/segmentio/go-prompt" + "github.com/riteshpradhan/go-prompt" ) 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) } diff --git a/prompt.go b/prompt.go index 412736e..1b98361 100644 --- a/prompt.go +++ b/prompt.go @@ -1,30 +1,68 @@ 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 { +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(msg string, dvalue string) (s string) { + fmt.Printf("%s (default value: %s)", msg, dvalue) + fmt.Scanln(&s) + if strings.Trim(s, " ") == "" { + return dvalue } return s } +// Stringln reads multi spaced sentence until newline +func Stringln(scanner *bufio.Scanner, msg string) string { + prompt(msg) + scanner.Scan() + return scanner.Text() +} + +// Integer prompt (required). +func IntegerRequired(msg string, args ...interface{}) int { + for { + n := Integer(msg, args...) + if n != 0 { + return n + } + } +} + +// Integer prompt +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": @@ -34,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) @@ -44,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) @@ -67,17 +105,48 @@ func Choose(prompt string, list []string) int { return i } +// Choose prompts for a single selection from `list`, returning in the index. +func ChooseInterface(msg 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(msg) + + // 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...) +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 @@ -92,3 +161,7 @@ func indexOf(s string, list []string) int { } return -1 } + +func prompt(msg string, args ...interface{}) { + fmt.Printf(msg+": ", args...) +}