forked from reeflective/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.go
112 lines (88 loc) · 2.79 KB
/
highlighter.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
package console
import (
"strings"
"github.com/spf13/cobra"
)
var (
seqFgGreen = "\x1b[32m"
seqFgYellow = "\x1b[33m"
seqFgReset = "\x1b[39m"
seqBrightWigth = "\x1b[38;05;244m"
)
// Base text effects.
var (
reset = "\x1b[0m"
bold = "\x1b[1m"
dim = "\x1b[2m"
underscore = "\x1b[4m"
blink = "\x1b[5m"
reverse = "\x1b[7m"
// Effects reset.
boldReset = "\x1b[22m" // 21 actually causes underline instead
dimReset = "\x1b[22m"
underscoreReset = "\x1b[24m"
blinkReset = "\x1b[25m"
reverseReset = "\x1b[27m"
)
// highlightSyntax - Entrypoint to all input syntax highlighting in the Wiregost console.
func (c *Console) highlightSyntax(input []rune) (line string) {
// Split the line as shellwords
args, unprocessed, err := split(string(input), true)
if err != nil {
args = append(args, unprocessed)
}
highlighted := make([]string, 0) // List of processed words, append to
remain := args // List of words to process, draw from
trimmed := trimSpacesMatch(remain) // Match stuff against trimmed words
// Highlight the root command when found.
cmd, _, _ := c.activeMenu().Find(trimmed)
if cmd != nil {
highlighted, remain = c.highlightCommand(highlighted, args, cmd)
}
// Highlight command flags
highlighted, remain = c.highlightCommandFlags(highlighted, remain, cmd)
// Done with everything, add remainind, non-processed words
highlighted = append(highlighted, remain...)
// Join all words.
line = strings.Join(highlighted, "")
return line
}
func (c *Console) highlightCommand(done, args []string, _ *cobra.Command) ([]string, []string) {
highlighted := make([]string, 0)
var rest []string
if len(args) == 0 {
return done, args
}
// Highlight the root command when found, or any of its aliases.
for _, cmd := range c.activeMenu().Commands() {
// Change 1: Highlight based on first arg in usage rather than the entire usage itself
cmdFound := strings.Split(cmd.Use, " ")[0] == strings.TrimSpace(args[0])
for _, alias := range cmd.Aliases {
if alias == strings.TrimSpace(args[0]) {
cmdFound = true
break
}
}
if cmdFound {
highlighted = append(highlighted, bold+seqFgGreen+args[0]+seqFgReset+boldReset)
rest = args[1:]
return append(done, highlighted...), rest
}
}
return append(done, highlighted...), args
}
func (c *Console) highlightCommandFlags(done, args []string, _ *cobra.Command) ([]string, []string) {
highlighted := make([]string, 0)
var rest []string
if len(args) == 0 {
return done, args
}
for _, arg := range args {
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
highlighted = append(highlighted, bold+seqBrightWigth+arg+seqFgReset+boldReset)
} else {
highlighted = append(highlighted, arg)
}
}
return append(done, highlighted...), rest
}