From af61a0f3601ddeaf90b3ad3aa748b71e388f4dbb Mon Sep 17 00:00:00 2001 From: Pygrum <119087231+pygrum@users.noreply.github.com> Date: Wed, 10 Jul 2024 21:35:54 +0100 Subject: [PATCH] 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 --- console.go | 20 ++++++++++++-------- highlighter.go | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/console.go b/console.go index 764b57b..390e6f3 100644 --- a/console.go +++ b/console.go @@ -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 @@ -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 diff --git a/highlighter.go b/highlighter.go index 436b7c0..48f53a6 100644 --- a/highlighter.go +++ b/highlighter.go @@ -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 @@ -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 @@ -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) }