Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
===================
Expand Down
21 changes: 16 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -28,6 +30,15 @@ func main() {
}
```

## License

MIT
## Functions:
* String
* StringRequired
* StringDefault
* Stringln
* Integer
* IntegerRequired
* Confirm
* Choose
* ChooseInterface
* Password
* PasswordMasked
2 changes: 1 addition & 1 deletion examples/confirm.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions examples/list-interface.go
Original file line number Diff line number Diff line change
@@ -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])
}
2 changes: 1 addition & 1 deletion examples/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import prompt "github.com/segmentio/go-prompt"
import "github.com/riteshpradhan/go-prompt"

var langs = []string{
"c",
Expand Down
2 changes: 1 addition & 1 deletion examples/passwords.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import prompt "github.com/segmentio/go-prompt"
import "github.com/riteshpradhan/go-prompt"

func main() {
{
Expand Down
18 changes: 18 additions & 0 deletions examples/string-line.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion examples/string-required.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

prompt "github.com/segmentio/go-prompt"
"github.com/riteshpradhan/go-prompt"
)

func main() {
Expand Down
6 changes: 4 additions & 2 deletions examples/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
105 changes: 89 additions & 16 deletions prompt.go
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -92,3 +161,7 @@ func indexOf(s string, list []string) int {
}
return -1
}

func prompt(msg string, args ...interface{}) {
fmt.Printf(msg+": ", args...)
}