-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmain.go
244 lines (216 loc) · 5.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/abiosoft/ishell/v2"
"github.com/fatih/color"
)
func main() {
shell := ishell.New()
// display info.
shell.Println("Sample Interactive Shell")
// Consider the unicode characters supported by the users font
// shell.SetMultiChoicePrompt(" >>"," - ")
// shell.SetChecklistOptions("[ ] ","[X] ")
// handle login.
shell.AddCmd(&ishell.Cmd{
Name: "login",
Func: func(c *ishell.Context) {
c.ShowPrompt(false)
defer c.ShowPrompt(true)
c.Println("Let's simulate login")
// prompt for input
c.Print("Username: ")
username := c.ReadLine()
c.Print("Password: ")
password := c.ReadPassword()
// do something with username and password
c.Println("Your inputs were", username, "and", password+".")
},
Help: "simulate a login",
})
// handle "greet".
shell.AddCmd(&ishell.Cmd{
Name: "greet",
Aliases: []string{"hello", "welcome"},
Help: "greet user",
Func: func(c *ishell.Context) {
name := "Stranger"
if len(c.Args) > 0 {
name = strings.Join(c.Args, " ")
}
c.Println("Hello", name)
},
})
// handle "default".
shell.AddCmd(&ishell.Cmd{
Name: "default",
Help: "readline with default input",
Func: func(c *ishell.Context) {
c.ShowPrompt(false)
defer c.ShowPrompt(true)
defaultInput := "default input, you can edit this"
if len(c.Args) > 0 {
defaultInput = strings.Join(c.Args, " ")
}
c.Print("input: ")
read := c.ReadLineWithDefault(defaultInput)
if read == defaultInput {
c.Println("you left the default input intact")
} else {
c.Printf("you modified input to '%s'", read)
c.Println()
}
},
})
// read multiple lines with "multi" command
shell.AddCmd(&ishell.Cmd{
Name: "multi",
Help: "input in multiple lines",
Func: func(c *ishell.Context) {
c.Println("Input multiple lines and end with semicolon ';'.")
lines := c.ReadMultiLines(";")
c.Println("Done reading. You wrote:")
c.Println(lines)
},
})
// multiple choice
shell.AddCmd(&ishell.Cmd{
Name: "choice",
Help: "multiple choice prompt",
Func: func(c *ishell.Context) {
choice := c.MultiChoice([]string{
"Golangers",
"Go programmers",
"Gophers",
"Goers",
}, "What are Go programmers called ?")
if choice == 2 {
c.Println("You got it!")
} else {
c.Println("Sorry, you're wrong.")
}
},
})
// multiple choice
shell.AddCmd(&ishell.Cmd{
Name: "checklist",
Help: "checklist prompt",
Func: func(c *ishell.Context) {
languages := []string{"Python", "Go", "Haskell", "Rust"}
choices := c.Checklist(languages,
"What are your favourite programming languages ?",
nil)
out := func() (c []string) {
for _, v := range choices {
c = append(c, languages[v])
}
return
}
c.Println("Your choices are", strings.Join(out(), ", "))
},
})
// progress bars
{
// determinate
shell.AddCmd(&ishell.Cmd{
Name: "det",
Help: "determinate progress bar",
Func: func(c *ishell.Context) {
c.ProgressBar().Start()
for i := 0; i < 101; i++ {
c.ProgressBar().Suffix(fmt.Sprint(" ", i, "%"))
c.ProgressBar().Progress(i)
time.Sleep(time.Millisecond * 100)
}
c.ProgressBar().Stop()
},
})
// indeterminate
shell.AddCmd(&ishell.Cmd{
Name: "ind",
Help: "indeterminate progress bar",
Func: func(c *ishell.Context) {
c.ProgressBar().Indeterminate(true)
c.ProgressBar().Start()
time.Sleep(time.Second * 10)
c.ProgressBar().Stop()
},
})
}
// subcommands and custom autocomplete.
{
var words []string
autoCmd := &ishell.Cmd{
Name: "suggest",
Help: "try auto complete",
LongHelp: `Try dynamic autocomplete by adding and removing words.
Then view the autocomplete by tabbing after "words" subcommand.
This is an example of a long help.`,
}
autoCmd.AddCmd(&ishell.Cmd{
Name: "add",
Help: "add words to autocomplete",
Func: func(c *ishell.Context) {
if len(c.Args) == 0 {
c.Err(errors.New("missing word(s)"))
return
}
words = append(words, c.Args...)
},
})
autoCmd.AddCmd(&ishell.Cmd{
Name: "clear",
Help: "clear words in autocomplete",
Func: func(c *ishell.Context) {
words = nil
},
})
autoCmd.AddCmd(&ishell.Cmd{
Name: "words",
Help: "add words with 'suggest add', then tab after typing 'suggest words '",
Completer: func([]string) []string {
return words
},
})
shell.AddCmd(autoCmd)
}
shell.AddCmd(&ishell.Cmd{
Name: "paged",
Help: "show paged text",
Func: func(c *ishell.Context) {
lines := ""
line := `%d. This is a paged text input.
This is another line of it.
`
for i := 0; i < 100; i++ {
lines += fmt.Sprintf(line, i+1)
}
c.ShowPaged(lines)
},
})
cyan := color.New(color.FgCyan).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
shell.AddCmd(&ishell.Cmd{
Name: "color",
Help: "color print",
Func: func(c *ishell.Context) {
c.Print(cyan("cyan\n"))
c.Println(yellow("yellow"))
c.Printf("%s\n", boldRed("bold red"))
},
})
// when started with "exit" as first argument, assume non-interactive execution
if len(os.Args) > 1 && os.Args[1] == "exit" {
shell.Process(os.Args[2:]...)
} else {
// start shell
shell.Run()
// teardown
shell.Close()
}
}