-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
244 lines (204 loc) · 5.68 KB
/
Copy pathcli.go
File metadata and controls
244 lines (204 loc) · 5.68 KB
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 (
"flag"
"fmt"
"os"
"strings"
"github.com/alecthomas/chroma/quick"
"github.com/alecthomas/chroma/styles"
"github.com/c-bata/go-prompt"
colour "github.com/fatih/color"
)
var availableStyles = styles.Names()
// Suggestion for available options available within the live REPL session
func completer(d prompt.Document) []prompt.Suggest {
sliced := strings.Split(d.Text, " ")
var suggestions []prompt.Suggest
if len(sliced) > 1 {
cmd := sliced[0]
if cmd == "browse" {
suggestions = []prompt.Suggest{
{Text: "last-day", Description: "Daily"},
{Text: "last-month", Description: "Hot this month"},
{Text: "last-week", Description: "Weekly"},
}
}
if cmd == "browse" || cmd == "match" {
s := []prompt.Suggest{
{Text: "sort-by-votes", Description: "All-time Greats"},
{Text: "latest", Description: "Latest"},
}
suggestions = append(suggestions, s...)
}
if cmd == "settheme" {
for _, theme := range availableStyles {
suggestions = append(suggestions, prompt.Suggest{
Text: theme,
Description: theme,
})
}
}
} else {
suggestions = []prompt.Suggest{
{Text: "browse", Description: "Browse all commands, sorted by days, month, weekly, all time etc"},
{Text: "exit", Description: "Exit the current repl session"},
{Text: "forthewicked", Description: "Commands for the wicked, be warned!"},
{Text: "help", Description: "Prints help information"},
{Text: "match", Description: "Match all commands for the given query (searches on comments also)"},
{Text: "random", Description: "Get random tips"},
{Text: "search", Description: "Search for commands that matches the given query"},
{Text: "settheme", Description: "Set syntax highlight theme"},
{Text: "version", Description: "Prints version information"},
}
}
return prompt.FilterFuzzy(suggestions, d.GetWordBeforeCursor(), true)
}
// Cli Represent our cli as a struct
type Cli struct {
// Start a REPL session
repl bool
// Query string if repl is not started
query string
// App's version
version bool
// Preview available themes
previewThemes bool
// Specify theme to use
theme string
}
// NewCli Initialize a new instance of `Cli`
func NewCli() Cli {
repl := flag.Bool("repl", true, fmt.Sprintf("Starts a %s repl", AppName))
query := flag.String("query", "", "Command or question to search")
version := flag.Bool("version", false, "Prints version information")
previewThemes := flag.Bool("preview-themes", false, "Preview available themes")
var theme string = "dracula"
flag.Func("theme", "Set syntax highlight theme", func(q string) error {
hasTheme, err := HasTheme(q)
if hasTheme {
theme = q
} else {
return err
}
return nil
})
flag.Parse()
return Cli{repl: *repl, query: *query, version: *version, theme: theme, previewThemes: *previewThemes}
}
func HasTheme(name string) (bool, error) {
for _, styleName := range availableStyles {
if styleName == name {
return true, nil
}
}
//lint:ignore ST1005 I like the way this looks
return false, fmt.Errorf("\nValue must be one of\n%s\n", strings.Join(availableStyles, ", "))
}
// Version Show App's version
func (app *App) Version() {
fmt.Println(AppName + " " + AppVersion)
}
// Repl Start a new REPL session
func (app *App) Repl() {
var header strings.Builder
header.WriteString(fmt.Sprintf("A cli and REPL for %s.com (%s)\n", AppName, AppVersion))
header.WriteString("Please use `exit` or `Ctrl-D` to exit this program\n")
header.WriteString("Type help to see all available commands and parameter\n")
fmt.Println(header.String())
repl := prompt.New(
func(input string) {
var (
cmd string
param string
)
sliced := strings.Split(input, " ")
if len(sliced) > 1 {
cmd = sliced[0]
param = strings.Join(sliced[1:], " ")
} else {
cmd = sliced[0]
}
switch cmd {
case "random":
run(func() error {
return app.random()
})
case "forthewicked":
run(func() error {
return app.wicked()
})
case "browse":
run(func() error {
return app.browse(param)
})
case "match":
run(func() error {
return app.matching(param)
})
case "search":
run(func() error {
return app.search(param)
})
case "settheme":
hasTheme, err := HasTheme(param)
if hasTheme {
app.cli.theme = param
} else {
fmt.Print(err)
}
case "version":
app.Version()
case "exit":
// deferred functions are not run of os.Exit is called
restoreTermState()
os.Exit(0)
case "help":
help("")
default:
help(input)
}
},
completer,
prompt.OptionMaxSuggestion(20),
prompt.OptionTitle(AppName),
prompt.OptionPrefixTextColor(prompt.DarkGreen),
prompt.OptionInputTextColor(prompt.Green),
prompt.OptionSuggestionBGColor(prompt.Green),
prompt.OptionSuggestionTextColor(prompt.Black),
prompt.OptionSelectedSuggestionTextColor(prompt.White),
)
repl.Run()
defer restoreTermState()
}
// Search whatever query (-query flag) was passed
func (app *App) Search() {
run(func() error {
return app.search(app.cli.query)
})
}
// PreviewThemes List available themes
func (app *App) PreviewThemes() {
source := `#!/usr/bin/env sh
# All fits on one line
command1 | command2
# Long commands
command1 \
| command2 \
| command3 \
| command4
# log to stderr
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
if ! do_something; then
err "Unable to do_something"
exit 1
fi
`
cl := colour.New(colour.FgWhite).Add(colour.Underline).Add(colour.Bold)
length := len(availableStyles) - 1
for index, style := range availableStyles {
cl.Printf("[%d/%d] %s\n\n", index, length, style)
quick.Highlight(os.Stdout, source, "bash", "terminal256", style)
}
}