From 7d76a0e5a84e9ed41da418fea27a81f79ee213c8 Mon Sep 17 00:00:00 2001 From: maxlandon Date: Thu, 15 Jan 2026 21:58:46 +0100 Subject: [PATCH 1/2] fix: NewlineBefore and NewlineAfter behavior Signed-off-by: maxlandon --- example/main.go | 2 +- run.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/example/main.go b/example/main.go index 12f6061..060290b 100644 --- a/example/main.go +++ b/example/main.go @@ -17,7 +17,7 @@ func main() { app := console.New("example") // Global Setup ------------------------------------------------- // - app.NewlineBefore = true + app.NewlineBefore = false app.NewlineAfter = true app.SetPrintLogo(func(_ *console.Console) { diff --git a/run.go b/run.go index a237449..ab9340b 100644 --- a/run.go +++ b/run.go @@ -33,6 +33,8 @@ func (c *Console) StartContext(ctx context.Context) error { lastLine := "" // used to check if last read line is empty. for { + // Print a newline after the last output if NewlineAfter is true + // and the last line was not empty. c.displayPostRun(lastLine) // Always ensure we work with the active menu, with freshly @@ -47,15 +49,10 @@ func (c *Console) StartContext(ctx context.Context) error { } // Block and read user input. - input , err := c.shell.Readline() - - c.displayPostRun(input) - + input, err := c.shell.Readline() if err != nil { menu.handleInterrupt(err) - - lastLine = input - + lastLine = input continue } @@ -72,7 +69,7 @@ func (c *Console) StartContext(ctx context.Context) error { } if len(args) == 0 { - lastLine = input + lastLine = input continue } @@ -84,6 +81,10 @@ func (c *Console) StartContext(ctx context.Context) error { continue } + // Print a newline before executing the command if NewlineBefore is true + // and the last line was not empty. + c.displayPreRun(input) + // Run all pre-run hooks and the command itself // Don't check the error: if its a cobra error, // the library user is responsible for setting @@ -93,7 +94,7 @@ func (c *Console) StartContext(ctx context.Context) error { menu.ErrorHandler(ExecutionError{newError(err, "")}) } - lastLine = input + lastLine = input } } From 3ea900edcfa0a3c982cbfd21afabf9b3b29ab1e8 Mon Sep 17 00:00:00 2001 From: maxlandon Date: Thu, 15 Jan 2026 22:00:08 +0100 Subject: [PATCH 2/2] Update example --- .gitignore | 1 + example/main-commands.go | 6 +++--- example/main.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ae85226..1f086c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ console.wiki +.gemini diff --git a/example/main-commands.go b/example/main-commands.go index 56a5010..b51f137 100644 --- a/example/main-commands.go +++ b/example/main-commands.go @@ -63,7 +63,7 @@ func mainMenuCommands(app *console.Console) console.Commands { message, _ := cmd.Flags().GetString("message") count, _ := cmd.Flags().GetInt("count") - for i := 0; i < count; i++ { + for range count { fmt.Println(message) } }, @@ -403,7 +403,7 @@ func mainMenuCommands(app *console.Console) console.Commands { rootCmd.AddCommand(searchCmd) - var mySlice = []string{"a", "b", "c"} + mySlice := []string{"a", "b", "c"} backupCmd := &cobra.Command{ Use: "backup [flags] SOURCE DESTINATION", @@ -590,7 +590,7 @@ func mainMenuCommands(app *console.Console) console.Commands { if cmd.Name() == "ssh" { // Generate a list of random hosts to use as positional arguments hosts := make([]string, 0) - for i := 0; i < 10; i++ { + for i := range 10 { hosts = append(hosts, fmt.Sprintf("host%d", i)) } diff --git a/example/main.go b/example/main.go index 060290b..12f6061 100644 --- a/example/main.go +++ b/example/main.go @@ -17,7 +17,7 @@ func main() { app := console.New("example") // Global Setup ------------------------------------------------- // - app.NewlineBefore = false + app.NewlineBefore = true app.NewlineAfter = true app.SetPrintLogo(func(_ *console.Console) {