-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (71 loc) · 2.25 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
package main
import (
Shell "cmdr/utils/shell"
UI "cmdr/utils/ui"
"flag"
"os"
"time"
)
func exit(code int, startTime time.Time) {
elapsed := time.Since(startTime)
UI.RenderTime(elapsed)
os.Exit(code)
}
func main() {
mainStart := time.Now()
includeArgsShort := flag.Bool("A", false, "Include arguments in the output")
includeArgsLong := flag.Bool("args", false, "Include arguments in the output")
showInvalidLong := flag.Bool("invalid", false, "Show invalid (commands that aren't available on your system) in the output")
showInvalidShort := flag.Bool("I", false, "Show invalid (commands that aren't available on your system) in the output")
showValidLong := flag.Bool("valid", false, "Show valid commands in the output")
showValidShort := flag.Bool("V", false, "Show valid commands in the output")
topN := flag.Int("top", 5, "Number of top commands to display")
flag.Parse()
shell, path, config, err := Shell.DetectShell()
if err != nil {
panic(err)
}
aliases, err := Shell.GetAliases(config)
if err != nil {
panic(err)
}
history, err := Shell.GetCommandHistory(shell, path, aliases)
if err != nil {
panic(err)
}
if *showValidShort || *showValidLong {
uniqueCommands := Shell.GetUniqueCommandCounts(history, 10000, *includeArgsShort || *includeArgsLong)
var validCommands []Shell.CommandCount
for _, command := range uniqueCommands {
if command.Valid {
validCommands = append(validCommands, command)
}
}
if len(validCommands) > *topN {
val := validCommands[:*topN]
UI.RenderValid(val, aliases)
} else {
UI.RenderValid(validCommands, aliases)
}
exit(0, mainStart)
}
if *showInvalidLong || *showInvalidShort {
uniqueCommands := Shell.GetUniqueCommandCounts(history, 10000, *includeArgsShort || *includeArgsLong)
var invalidCommands []Shell.CommandCount
for _, command := range uniqueCommands {
if !command.Valid {
invalidCommands = append(invalidCommands, command)
}
}
if len(invalidCommands) > *topN {
inv := invalidCommands[:*topN]
UI.RenderInvalid(inv)
} else {
UI.RenderInvalid(invalidCommands)
}
exit(0, mainStart)
}
topCommands := Shell.GetUniqueCommandCounts(history, *topN, *includeArgsShort || *includeArgsLong)
UI.RenderTopCommands(topCommands)
exit(0, mainStart)
}