Skip to content

Commit

Permalink
feat: custom shell highlight (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pygrum authored Jul 10, 2024
1 parent e608d68 commit af61a0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 12 additions & 8 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (
// Console is an integrated console application instance.
type Console struct {
// Application
name string // Used in the prompt, and for readline `.inputrc` application-specific settings.
shell *readline.Shell // Provides readline functionality (inputs, completions, hints, history)
printLogo func(c *Console) // Simple logo printer.
menus map[string]*Menu // Different command trees, prompt engines, etc.
filters []string // Hide commands based on their attributes and current context.
isExecuting bool // Used by log functions, which need to adapt behavior (print the prompt, , etc)
printed bool // Used to adjust asynchronous messages too.
mutex *sync.RWMutex // Concurrency management.
name string // Used in the prompt, and for readline `.inputrc` application-specific settings.
shell *readline.Shell // Provides readline functionality (inputs, completions, hints, history)
printLogo func(c *Console) // Simple logo printer.
cmdHighlight string // Ansi code for highlighting of command in default highlighter. Green by default.
flagHighlight string // Ansi code for highlighting of flag in default highlighter. Grey by default.
menus map[string]*Menu // Different command trees, prompt engines, etc.
filters []string // Hide commands based on their attributes and current context.
isExecuting bool // Used by log functions, which need to adapt behavior (print the prompt, etc.)
printed bool // Used to adjust asynchronous messages too.
mutex *sync.RWMutex // Concurrency management.

// Execution

Expand Down Expand Up @@ -93,6 +95,8 @@ func New(app string) *Console {
}

// Syntax highlighting, multiline callbacks, etc.
console.cmdHighlight = seqFgGreen
console.flagHighlight = seqBrightWigth
console.shell.AcceptMultiline = console.acceptMultiline
console.shell.SyntaxHighlighter = console.highlightSyntax

Expand Down
20 changes: 18 additions & 2 deletions highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ var (
reverseReset = "\x1b[27m"
)

// SetDefaultCommandHighlight allows the user to change the highlight color for a command in the default syntax
// highlighter using an ansi code.
// This action has no effect if a custom syntax highlighter for the shell is set.
// By default, the highlight code is green ("\x1b[32m").
func (c *Console) SetDefaultCommandHighlight(seq string) {
c.cmdHighlight = seq
}

// SetDefaultFlagHighlight allows the user to change the highlight color for a flag in the default syntax
// highlighter using an ansi color code.
// This action has no effect if a custom syntax highlighter for the shell is set.
// By default, the highlight code is grey ("\x1b[38;05;244m").
func (c *Console) SetDefaultFlagHighlight(seq string) {
c.flagHighlight = seq
}

// highlightSyntax - Entrypoint to all input syntax highlighting in the Wiregost console.
func (c *Console) highlightSyntax(input []rune) (line string) {
// Split the line as shellwords
Expand Down Expand Up @@ -82,7 +98,7 @@ func (c *Console) highlightCommand(done, args []string, _ *cobra.Command) ([]str
}

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

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

for _, arg := range args {
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
highlighted = append(highlighted, bold+seqBrightWigth+arg+seqFgReset+boldReset)
highlighted = append(highlighted, bold+c.flagHighlight+arg+seqFgReset+boldReset)
} else {
highlighted = append(highlighted, arg)
}
Expand Down

0 comments on commit af61a0f

Please sign in to comment.