Skip to content

Commit d2bd206

Browse files
committed
refactor, add example for default input
1 parent e012787 commit d2bd206

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

actions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func (s *shellActionsImpl) ReadLineErr() (string, error) {
8888
}
8989

9090
func (s *shellActionsImpl) ReadLineWithDefault(defaultValue string) string {
91-
s.reader.defaultValue = defaultValue
91+
s.reader.defaultInput = defaultValue
9292
line, _ := s.readLine()
93-
s.reader.defaultValue = ""
93+
s.reader.defaultInput = ""
9494
return line
9595
}
9696

example/main.go

+24
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ func main() {
5757
},
5858
})
5959

60+
// handle "default".
61+
shell.AddCmd(&ishell.Cmd{
62+
Name: "default",
63+
Help: "readline with default input",
64+
Func: func(c *ishell.Context) {
65+
c.ShowPrompt(false)
66+
defer c.ShowPrompt(true)
67+
68+
defaultInput := "default input, you can edit this"
69+
if len(c.Args) > 0 {
70+
defaultInput = strings.Join(c.Args, " ")
71+
}
72+
73+
c.Print("input: ")
74+
read := c.ReadLineWithDefault(defaultInput)
75+
76+
if read == defaultInput {
77+
c.Println("you left the default input intact")
78+
} else {
79+
c.Printf("you modified input to '%s'", read)
80+
c.Println()
81+
}
82+
},
83+
})
6084
// read multiple lines with "multi" command
6185
shell.AddCmd(&ishell.Cmd{
6286
Name: "multi",

reader.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type (
2424
multiPrompt string
2525
showPrompt bool
2626
completer readline.AutoCompleter
27+
defaultInput string
2728
sync.Mutex
28-
defaultValue string
2929
}
3030
)
3131

@@ -88,7 +88,7 @@ func (s *shellReader) readLine(consumer chan lineString) {
8888
// use printed statement as prompt
8989
s.scanner.SetPrompt(prompt)
9090

91-
line, err := s.scanner.ReadlineWithDefault(s.defaultValue)
91+
line, err := s.scanner.ReadlineWithDefault(s.defaultInput)
9292

9393
// reset prompt
9494
s.scanner.SetPrompt(shellPrompt)

0 commit comments

Comments
 (0)