Skip to content

Commit

Permalink
fx
Browse files Browse the repository at this point in the history
  • Loading branch information
zhousak committed Dec 16, 2023
1 parent 7f26448 commit 5948727
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 40 deletions.
26 changes: 15 additions & 11 deletions input.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Package input reads user input at the console. http://github.com/tcnksm/go-input
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
query := "What is your name?"
name, err := ui.Ask(query, &input.Options{
Default: "tcnksm",
Required: true,
Loop: true,
})
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
query := "What is your name?"
name, err := ui.Ask(query, &input.Options{
Default: "tcnksm",
Required: true,
Loop: true,
})
*/
package input

Expand Down Expand Up @@ -96,6 +96,10 @@ type Options struct {
// is input.
Default string

// DefaultSelected is the default index which is used when no thing
// is select. Start from 1
DefaultSelected int

// Loop loops asking user to input until getting valid input.
Loop bool

Expand Down
51 changes: 30 additions & 21 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//
// If the user sends SIGINT (Ctrl+C) while reading input, it catches
// it and return it as a error.
func (i *UI) Select(query string, list []string, opts *Options) (string, error) {
func (i *UI) Select(query string, list []string, opts *Options) (string, int, error) {
// Set default val
i.once.Do(i.setDefault)

Expand All @@ -24,21 +24,28 @@ func (i *UI) Select(query string, list []string, opts *Options) (string, error)
// If empty, can not transform it to int.
opts.Required = true

// Find default index which opts.Default indicates
defaultIndex := -1
defaultVal := opts.Default
if defaultVal != "" {
for i, item := range list {
if item == defaultVal {
defaultIndex = i
var defaultIndex int

if opts.DefaultSelected > 0 {
defaultIndex = opts.DefaultSelected - 1
//defaultVal := list[defaultIndex]
} else {
// Find default index which opts.Default indicates
defaultIndex = -1
defaultVal := opts.Default
if defaultVal != "" {
for i, item := range list {
if item == defaultVal {
defaultIndex = i
}
}
}

// DefaultVal is set but doesn't exist in list
if defaultIndex == -1 {
// This error message is not for user
// Should be found while development
return "", fmt.Errorf("opt.Default is specified but item does not exist in list")
// DefaultVal is set but doesn't exist in list
if defaultIndex == -1 {
// This error message is not for user
// Should be found while development
return "", 0, fmt.Errorf("opt.Default is specified but item does not exist in list")
}
}
}

Expand All @@ -53,7 +60,7 @@ func (i *UI) Select(query string, list []string, opts *Options) (string, error)
fmt.Fprintf(i.Writer, buf.String())

// resultStr and resultErr are return val of this function
var resultStr string
var resultIndex int
var resultErr error
for {

Expand All @@ -76,9 +83,13 @@ func (i *UI) Select(query string, list []string, opts *Options) (string, error)
break
}

line = strings.TrimRightFunc(line, func(r rune) bool {
return r == '\r' || r == '\n'
})

// line is empty but default is provided returns it
if line == "" && defaultIndex >= 0 {
resultStr = list[defaultIndex]
resultIndex = defaultIndex
break
}

Expand All @@ -93,9 +104,7 @@ func (i *UI) Select(query string, list []string, opts *Options) (string, error)
}

// Convert user input string to int val
line = strings.TrimRightFunc(line, func(r rune) bool {
return r == '\r' || r == '\n'
})

n, err := strconv.Atoi(line)
if err != nil {
if !opts.Loop {
Expand Down Expand Up @@ -134,12 +143,12 @@ func (i *UI) Select(query string, list []string, opts *Options) (string, error)
}

// Reach here means it gets ideal input.
resultStr = list[n-1]
resultIndex = n - 1
break
}

// Insert the new line for next output
fmt.Fprintf(i.Writer, "\n")

return resultStr, resultErr
return list[resultIndex], resultIndex, resultErr
}
60 changes: 52 additions & 8 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestSelect(t *testing.T) {
Reader: c.userInput,
}

ans, err := ui.Select("", c.list, c.opts)
ans, _, err := ui.Select("", c.list, c.opts)
if err != nil {
t.Fatalf("#%d expect not to occurr error: %s", i, err)
}
Expand All @@ -101,7 +101,7 @@ func TestSelect_invalidDefault(t *testing.T) {
ui := &UI{
Writer: io.Discard,
}
_, err := ui.Select("Which?", []string{"A", "B", "C"}, &Options{
_, _, err := ui.Select("Which?", []string{"A", "B", "C"}, &Options{
// "D" is not in select target list
Default: "D",
})
Expand All @@ -111,6 +111,50 @@ func TestSelect_invalidDefault(t *testing.T) {
}
}

func TestSelect_SelectDefault(t *testing.T) {
ui := &UI{
Reader: bytes.NewBufferString("\r"),
Writer: io.Discard,
}
rslt, n, err := ui.Select("Which?", []string{"A", "B", "C"}, &Options{
// "D" is not in select target list
Default: "A",
})

if err != nil {
t.Fatal("expect err to be nil, but got", err)
}

if rslt != "A" {
t.Fatal("expect rslt to be A")
}
if n != 0 {
t.Fatal("expect n to be 0")
}
}

func TestSelect_SelectDefault2(t *testing.T) {
ui := &UI{
Reader: bytes.NewBufferString("\r"),
Writer: io.Discard,
}
rslt, n, err := ui.Select("Which?", []string{"A", "B", "C"}, &Options{
// "D" is not in select target list
DefaultSelected: 2,
})

if err != nil {
t.Fatal("expect err to be nil, but got", err)
}

if rslt != "B" {
t.Fatal("expect rslt to be B")
}
if n != 1 {
t.Fatal("expect n to be 1")
}
}

func ExampleUI_Select() {
ui := &UI{
// In real world, Reader is os.Stdin and input comes
Expand All @@ -120,12 +164,12 @@ func ExampleUI_Select() {
}

query := "Which language do you prefer to use?"
lang, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
lang, n, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
Default: "Go",
})

fmt.Println(lang)
// Output: golang
fmt.Println(lang, n)
// Output: golang 2
}

func ExampleUI_Select_Win() {
Expand All @@ -137,10 +181,10 @@ func ExampleUI_Select_Win() {
}

query := "Which language do you prefer to use?"
lang, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
lang, n, _ := ui.Select(query, []string{"go", "Go", "golang"}, &Options{
Default: "Go",
})

fmt.Println(lang)
// Output: golang
fmt.Println(lang, n)
// Output: golang 2
}

0 comments on commit 5948727

Please sign in to comment.