Repository: segmentio/go-prompt Branch: master Commit: f0d19b6901ad Files: 8 Total size: 3.5 KB Directory structure: gitextract_r8jliluu/ ├── History.md ├── Readme.md ├── examples/ │ ├── confirm.go │ ├── list.go │ ├── passwords.go │ ├── string-required.go │ └── string.go └── prompt.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: History.md ================================================ v1.2.0 / 2014-12-10 =================== * remove appending of ? to Confirm() v1.1.0 / 2014-10-22 ================== * add passwords example * add password docs * Merge pull request #2 from nrmitchi/add/gopass * Adding convenience wrappers around howeyc/gopass ================================================ FILE: Readme.md ================================================ # go-prompt Terminal prompts for Go. View the [docs](http://godoc.org/pkg/github.com/segmentio/go-prompt). ## Example ```go package main import "github.com/segmentio/go-prompt" var langs = []string{ "c", "c++", "lua", "go", "js", "ruby", "python", } func main() { i := prompt.Choose("What's your favorite language?", langs) println("picked: " + langs[i]) } ``` ## License MIT ================================================ FILE: examples/confirm.go ================================================ package main import prompt "github.com/segmentio/go-prompt" func main() { if ok := prompt.Confirm("launch %s?", "something"); ok { println("launching") } else { println("not launching") } } ================================================ FILE: examples/list.go ================================================ package main import prompt "github.com/segmentio/go-prompt" var langs = []string{ "c", "c++", "lua", "go", "js", "ruby", "python", } func main() { i := prompt.Choose("What's your favorite language?", langs) println("picked: " + langs[i]) } ================================================ FILE: examples/passwords.go ================================================ package main import prompt "github.com/segmentio/go-prompt" func main() { { name := "Tobi" pass := prompt.Password("hi %s enter your password", name) println(pass) } { pass := prompt.PasswordMasked("masked passwords are cool") println(pass) } } ================================================ FILE: examples/string-required.go ================================================ package main import ( "fmt" prompt "github.com/segmentio/go-prompt" ) func main() { println("need your name!") first := prompt.StringRequired("first") last := prompt.StringRequired("last") fmt.Printf("\nHello %s %s\n", first, last) } ================================================ FILE: examples/string.go ================================================ package main import ( "fmt" prompt "github.com/segmentio/go-prompt" ) func main() { println("need your name!") first := prompt.String("first") last := prompt.String("last") fmt.Printf("\nHello %s %s\n", first, last) } ================================================ FILE: prompt.go ================================================ package prompt import "github.com/howeyc/gopass" import "strings" import "strconv" import "fmt" // String prompt. func String(prompt string, args ...interface{}) string { var s string fmt.Printf(prompt+": ", args...) fmt.Scanln(&s) return s } // String prompt (required). func StringRequired(prompt string, args ...interface{}) (s string) { for strings.Trim(s, " ") == "" { s = String(prompt, args...) } return s } // Confirm continues prompting until the input is boolean-ish. func Confirm(prompt string, args ...interface{}) bool { for { switch String(prompt, args...) { case "Yes", "yes", "y", "Y": return true case "No", "no", "n", "N": return false } } } // Choose prompts for a single selection from `list`, returning in the index. func Choose(prompt string, list []string) int { fmt.Println() for i, val := range list { fmt.Printf(" %d) %s\n", i+1, val) } fmt.Println() i := -1 for { s := String(prompt) // index n, err := strconv.Atoi(s) if err == nil { if n > 0 && n <= len(list) { i = n - 1 break } else { continue } } // value i = indexOf(s, list) if i != -1 { break } } return i } // Password prompt. func Password(prompt string, args ...interface{}) string { fmt.Printf(prompt+": ", 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...) password, _ := gopass.GetPasswdMasked() s := string(password[0:]) return s } // index of `s` in `list`. func indexOf(s string, list []string) int { for i, val := range list { if val == s { return i } } return -1 }