Skip to content

Commit 1f810de

Browse files
authored
Merge pull request #76 from reeflective/port-newline-fix
port newline fix
2 parents 4d38cb4 + 3ea900e commit 1f810de

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
console.wiki
2+
.gemini

example/main-commands.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
6363
message, _ := cmd.Flags().GetString("message")
6464
count, _ := cmd.Flags().GetInt("count")
6565

66-
for i := 0; i < count; i++ {
66+
for range count {
6767
fmt.Println(message)
6868
}
6969
},
@@ -403,7 +403,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
403403

404404
rootCmd.AddCommand(searchCmd)
405405

406-
var mySlice = []string{"a", "b", "c"}
406+
mySlice := []string{"a", "b", "c"}
407407

408408
backupCmd := &cobra.Command{
409409
Use: "backup [flags] SOURCE DESTINATION",
@@ -590,7 +590,7 @@ func mainMenuCommands(app *console.Console) console.Commands {
590590
if cmd.Name() == "ssh" {
591591
// Generate a list of random hosts to use as positional arguments
592592
hosts := make([]string, 0)
593-
for i := 0; i < 10; i++ {
593+
for i := range 10 {
594594
hosts = append(hosts, fmt.Sprintf("host%d", i))
595595
}
596596

run.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func (c *Console) StartContext(ctx context.Context) error {
3333
lastLine := "" // used to check if last read line is empty.
3434

3535
for {
36+
// Print a newline after the last output if NewlineAfter is true
37+
// and the last line was not empty.
3638
c.displayPostRun(lastLine)
3739

3840
// Always ensure we work with the active menu, with freshly
@@ -47,15 +49,10 @@ func (c *Console) StartContext(ctx context.Context) error {
4749
}
4850

4951
// Block and read user input.
50-
input , err := c.shell.Readline()
51-
52-
c.displayPostRun(input)
53-
52+
input, err := c.shell.Readline()
5453
if err != nil {
5554
menu.handleInterrupt(err)
56-
57-
lastLine = input
58-
55+
lastLine = input
5956
continue
6057
}
6158

@@ -72,7 +69,7 @@ func (c *Console) StartContext(ctx context.Context) error {
7269
}
7370

7471
if len(args) == 0 {
75-
lastLine = input
72+
lastLine = input
7673
continue
7774
}
7875

@@ -84,6 +81,10 @@ func (c *Console) StartContext(ctx context.Context) error {
8481
continue
8582
}
8683

84+
// Print a newline before executing the command if NewlineBefore is true
85+
// and the last line was not empty.
86+
c.displayPreRun(input)
87+
8788
// Run all pre-run hooks and the command itself
8889
// Don't check the error: if its a cobra error,
8990
// the library user is responsible for setting
@@ -93,7 +94,7 @@ func (c *Console) StartContext(ctx context.Context) error {
9394
menu.ErrorHandler(ExecutionError{newError(err, "")})
9495
}
9596

96-
lastLine = input
97+
lastLine = input
9798
}
9899
}
99100

0 commit comments

Comments
 (0)