Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlandon authored Aug 9, 2024
2 parents c81f697 + af61a0f commit eb56c11
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
33 changes: 25 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 All @@ -33,6 +35,17 @@ type Console struct {
// know how to handle it in all situations.
NewlineAfter bool

// Leave empty lines with NewlineBefore and NewlineAfter, even if the provided input was empty.
// Empty characters are defined as any number of spaces and tabs. The 'empty' character set
// can be changed by modifying Console.EmptyChars
// This field is false by default.
NewlineWhenEmpty bool

// Characters that are used to determine whether an input line was empty. If a line is not entirely
// made up by any of these characters, then it is not considered empty. The default characters
// are ' ' and '\t'.
EmptyChars []rune

// PreReadlineHooks - All the functions in this list will be executed,
// in their respective orders, before the console starts reading
// any user input (ie, before redrawing the prompt).
Expand Down Expand Up @@ -82,13 +95,17 @@ 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

// Completion
console.shell.Completer = console.complete
console.defaultStyleConfig()

// Defaults
console.EmptyChars = []rune{' ', '\t'}
return console
}

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
11 changes: 11 additions & 0 deletions line.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,14 @@ func trimSpacesMatch(remain []string) (trimmed []string) {

return
}

func (c *Console) lineEmpty(line string) bool {
empty := true
for _, r := range line {
if !strings.ContainsRune(string(c.EmptyChars), r) {
empty = false
break
}
}
return empty
}
29 changes: 25 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@ func (c *Console) StartContext(ctx context.Context) error {
c.printLogo(c)
}

for {
lastLine := "" // used to check if last read line is empty.

for i := 0; ; i++ {
// Identical to printing it at the end of the loop, and
// leaves some space between the logo and the first prompt.

// If NewlineAfter is set but NewlineWhenEmpty is not set, we do a check to see
// if the last line was empty. If it wasn't, then we can print.
//fmt.Println(lastLine, len(lastLine))
if c.NewlineAfter {
fmt.Println()
if !c.NewlineWhenEmpty && i != 0 {
// Print on the condition that the last input wasn't empty.
if !c.lineEmpty(lastLine) {
fmt.Println()
}
} else {
fmt.Println()
}
}

// Always ensure we work with the active menu, with freshly
Expand All @@ -51,13 +64,19 @@ func (c *Console) StartContext(ctx context.Context) error {

// Block and read user input.
line, err := c.shell.Readline()

if c.NewlineBefore {
fmt.Println()
if !c.NewlineWhenEmpty {
if !c.lineEmpty(line) {
fmt.Println()
}
} else {
fmt.Println()
}
}

if err != nil {
menu.handleInterrupt(err)
lastLine = line
continue
}

Expand All @@ -74,6 +93,7 @@ func (c *Console) StartContext(ctx context.Context) error {
}

if len(args) == 0 {
lastLine = line
continue
}

Expand All @@ -93,6 +113,7 @@ func (c *Console) StartContext(ctx context.Context) error {
if err := c.execute(ctx, menu, args, false); err != nil {
menu.ErrorHandler(ExecutionError{newError(err, "")})
}
lastLine = line
}
}

Expand Down

0 comments on commit eb56c11

Please sign in to comment.