Skip to content

Commit

Permalink
up: refactor use pkg IO replace os.Std
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 4, 2022
1 parent 93988d8 commit 3d6cf1f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
17 changes: 17 additions & 0 deletions interact/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package interact

import "github.com/gookit/goutil/maputil"

// Collector information collector 信息收集者
type Collector struct {
qs []*Question
ans maputil.Data
}

func NewCollector() *Collector {
return &Collector{}
}

func (c *Collector) Run() error {
return nil
}
9 changes: 5 additions & 4 deletions interact/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"fmt"
"os"
"strings"
)

Expand All @@ -16,14 +15,16 @@ type result struct {
// Prompt query and read user answer.
//
// Usage:
// answer,err := Prompt(context.TODO(), "your name?", "")
//
// answer,err := Prompt(context.Background(), "your name?", "")
//
// from package golang.org/x/tools/cmd/getgo
func Prompt(ctx context.Context, query, defaultAnswer string) (string, error) {
fmt.Printf("%s [%s]: ", query, defaultAnswer)
_, _ = fmt.Fprintf(Output, "%s [%s]: ", query, defaultAnswer)

ch := make(chan result, 1)
go func() {
s := bufio.NewScanner(os.Stdin)
s := bufio.NewScanner(Input)
if !s.Scan() { // reading
ch <- result{"", s.Err()}
return
Expand Down
3 changes: 1 addition & 2 deletions interact/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ DoASK:

// has validator func
if q.Func != nil {
err := q.Func(ans)
if err != nil {
if err := q.Func(ans); err != nil {
q.checkErrTimes()
echoErr(err.Error())
goto DoASK
Expand Down
12 changes: 12 additions & 0 deletions interact/question_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package interact_test

import (
"testing"

"github.com/gookit/gcli/v3/interact"
)

func TestQuestion_Run(t *testing.T) {
q := interact.NewQuestion("your name")
q.Run()
}
10 changes: 5 additions & 5 deletions interact/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
// ReadInput read user input form Stdin
func ReadInput(question string) (string, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

scanner := bufio.NewScanner(os.Stdin)
scanner := bufio.NewScanner(Input)
if !scanner.Scan() { // reading
return "", scanner.Err()
}
Expand All @@ -34,10 +34,10 @@ func ReadInput(question string) (string, error) {
// ans, _ := ReadLine("your name?")
func ReadLine(question string) (string, error) {
if len(question) > 0 {
color.Print(question)
color.Fprint(Output, question)
}

reader := bufio.NewReader(os.Stdin)
reader := bufio.NewReader(Input)
answer, _, err := reader.ReadLine()
return strings.TrimSpace(string(answer)), err
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func AnswerIsYes(defVal ...bool) bool {
return defVal[0]
}

fmt.Print("Please try again")
_, _ = fmt.Fprint(Output, "Please try again")
return AnswerIsYes()
}

Expand Down
6 changes: 3 additions & 3 deletions interact/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Select struct {
// Title message for select. e.g "Your city?"
Title string
// Options the options data for select. allow: []int,[]string,map[string]string
// Options the items data for select. allow: []int, []string, map[string]string
Options any
// DefOpt default option when not input answer
DefOpt string
Expand All @@ -41,10 +41,10 @@ type Select struct {
// r := s.Run()
// key := r.KeyString() // "1"
// val := r.String() // "beijing"
func NewSelect(title string, options any) *Select {
func NewSelect(title string, items any) *Select {
return &Select{
Title: title,
Options: options,
Options: items,
}
}

Expand Down

0 comments on commit 3d6cf1f

Please sign in to comment.