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/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 } }