Skip to content

Commit

Permalink
set lastLine every iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
pygrum committed Jul 10, 2024
1 parent 20b2910 commit 3f95325
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func (c *Console) Start() error {
c.printLogo(c)
}

firstRead := true // leave space between logo and first prompt on first read without fail, if NewlineAfter is set.
lastLine := "" // used to check if last read line is empty.
lastLine := "" // used to check if last read line is empty.

for {
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 {
if !c.NewlineWhenEmpty && !firstRead {
if !c.NewlineWhenEmpty && i != 0 {
// Print on the condition that the last input wasn't empty.
if !c.lineEmpty(lastLine) {
fmt.Println()
Expand All @@ -58,8 +58,8 @@ func (c *Console) Start() error {
// Block and read user input.
line, err := c.shell.Readline()
if c.NewlineBefore {
if !c.NewlineWhenEmpty && !firstRead {
if !c.lineEmpty(lastLine) {
if !c.NewlineWhenEmpty {
if !c.lineEmpty(line) {
fmt.Println()
}
} else {
Expand All @@ -69,6 +69,7 @@ func (c *Console) Start() error {

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

Expand All @@ -81,10 +82,12 @@ func (c *Console) Start() error {
args, err := c.parse(line)
if err != nil {
fmt.Printf("Parsing error: %s\n", err.Error())
lastLine = line
continue
}

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

Expand All @@ -93,6 +96,7 @@ func (c *Console) Start() error {
args, err = c.runLineHooks(args)
if err != nil {
fmt.Printf("Line error: %s\n", err.Error())
lastLine = line
continue
}

Expand All @@ -101,12 +105,9 @@ func (c *Console) Start() error {
// the library user is responsible for setting
// the cobra behavior.
// If it's an interrupt, we take care of it.
if err := c.execute(menu, args, false); err != nil {
if err = c.execute(menu, args, false); err != nil {
fmt.Println(err)
}
if firstRead {
firstRead = false
}
lastLine = line
}
}
Expand Down

0 comments on commit 3f95325

Please sign in to comment.