Skip to content

Commit af61a0f

Browse files
authored
feat: custom shell highlight (#50)
* no large gaps between prompts on empty input due to NewlineBefore and NewlineAfter * removed lastLine placeholder * don't print lastLine * check firstRead in newlineAfter * set lastLine every iteration * custom ansi code for flag and command highlight color * docstring
1 parent e608d68 commit af61a0f

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

console.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import (
1212
// Console is an integrated console application instance.
1313
type Console struct {
1414
// Application
15-
name string // Used in the prompt, and for readline `.inputrc` application-specific settings.
16-
shell *readline.Shell // Provides readline functionality (inputs, completions, hints, history)
17-
printLogo func(c *Console) // Simple logo printer.
18-
menus map[string]*Menu // Different command trees, prompt engines, etc.
19-
filters []string // Hide commands based on their attributes and current context.
20-
isExecuting bool // Used by log functions, which need to adapt behavior (print the prompt, , etc)
21-
printed bool // Used to adjust asynchronous messages too.
22-
mutex *sync.RWMutex // Concurrency management.
15+
name string // Used in the prompt, and for readline `.inputrc` application-specific settings.
16+
shell *readline.Shell // Provides readline functionality (inputs, completions, hints, history)
17+
printLogo func(c *Console) // Simple logo printer.
18+
cmdHighlight string // Ansi code for highlighting of command in default highlighter. Green by default.
19+
flagHighlight string // Ansi code for highlighting of flag in default highlighter. Grey by default.
20+
menus map[string]*Menu // Different command trees, prompt engines, etc.
21+
filters []string // Hide commands based on their attributes and current context.
22+
isExecuting bool // Used by log functions, which need to adapt behavior (print the prompt, etc.)
23+
printed bool // Used to adjust asynchronous messages too.
24+
mutex *sync.RWMutex // Concurrency management.
2325

2426
// Execution
2527

@@ -93,6 +95,8 @@ func New(app string) *Console {
9395
}
9496

9597
// Syntax highlighting, multiline callbacks, etc.
98+
console.cmdHighlight = seqFgGreen
99+
console.flagHighlight = seqBrightWigth
96100
console.shell.AcceptMultiline = console.acceptMultiline
97101
console.shell.SyntaxHighlighter = console.highlightSyntax
98102

highlighter.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ var (
3131
reverseReset = "\x1b[27m"
3232
)
3333

34+
// SetDefaultCommandHighlight allows the user to change the highlight color for a command in the default syntax
35+
// highlighter using an ansi code.
36+
// This action has no effect if a custom syntax highlighter for the shell is set.
37+
// By default, the highlight code is green ("\x1b[32m").
38+
func (c *Console) SetDefaultCommandHighlight(seq string) {
39+
c.cmdHighlight = seq
40+
}
41+
42+
// SetDefaultFlagHighlight allows the user to change the highlight color for a flag in the default syntax
43+
// highlighter using an ansi color code.
44+
// This action has no effect if a custom syntax highlighter for the shell is set.
45+
// By default, the highlight code is grey ("\x1b[38;05;244m").
46+
func (c *Console) SetDefaultFlagHighlight(seq string) {
47+
c.flagHighlight = seq
48+
}
49+
3450
// highlightSyntax - Entrypoint to all input syntax highlighting in the Wiregost console.
3551
func (c *Console) highlightSyntax(input []rune) (line string) {
3652
// Split the line as shellwords
@@ -82,7 +98,7 @@ func (c *Console) highlightCommand(done, args []string, _ *cobra.Command) ([]str
8298
}
8399

84100
if cmdFound {
85-
highlighted = append(highlighted, bold+seqFgGreen+args[0]+seqFgReset+boldReset)
101+
highlighted = append(highlighted, bold+c.cmdHighlight+args[0]+seqFgReset+boldReset)
86102
rest = args[1:]
87103

88104
return append(done, highlighted...), rest
@@ -102,7 +118,7 @@ func (c *Console) highlightCommandFlags(done, args []string, _ *cobra.Command) (
102118

103119
for _, arg := range args {
104120
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
105-
highlighted = append(highlighted, bold+seqBrightWigth+arg+seqFgReset+boldReset)
121+
highlighted = append(highlighted, bold+c.flagHighlight+arg+seqFgReset+boldReset)
106122
} else {
107123
highlighted = append(highlighted, arg)
108124
}

0 commit comments

Comments
 (0)