Skip to content

Commit e608d68

Browse files
authored
feat: newline when empty flag (#49)
* no large gaps between prompts on empty input due to NewlineBefore and NewlineAfter * removed lastLine placeholder * don't print lastLine * check firstRead in newlineAfter * set lastLine every iteration
1 parent c93dbdc commit e608d68

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

console.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ type Console struct {
3333
// know how to handle it in all situations.
3434
NewlineAfter bool
3535

36+
// Leave empty lines with NewlineBefore and NewlineAfter, even if the provided input was empty.
37+
// Empty characters are defined as any number of spaces and tabs. The 'empty' character set
38+
// can be changed by modifying Console.EmptyChars
39+
// This field is false by default.
40+
NewlineWhenEmpty bool
41+
42+
// Characters that are used to determine whether an input line was empty. If a line is not entirely
43+
// made up by any of these characters, then it is not considered empty. The default characters
44+
// are ' ' and '\t'.
45+
EmptyChars []rune
46+
3647
// PreReadlineHooks - All the functions in this list will be executed,
3748
// in their respective orders, before the console starts reading
3849
// any user input (ie, before redrawing the prompt).
@@ -89,6 +100,8 @@ func New(app string) *Console {
89100
console.shell.Completer = console.complete
90101
console.defaultStyleConfig()
91102

103+
// Defaults
104+
console.EmptyChars = []rune{' ', '\t'}
92105
return console
93106
}
94107

line.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,14 @@ func trimSpacesMatch(remain []string) (trimmed []string) {
267267

268268
return
269269
}
270+
271+
func (c *Console) lineEmpty(line string) bool {
272+
empty := true
273+
for _, r := range line {
274+
if !strings.ContainsRune(string(c.EmptyChars), r) {
275+
empty = false
276+
break
277+
}
278+
}
279+
return empty
280+
}

run.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@ func (c *Console) Start() error {
2323
c.printLogo(c)
2424
}
2525

26-
for {
26+
lastLine := "" // used to check if last read line is empty.
27+
28+
for i := 0; ; i++ {
2729
// Identical to printing it at the end of the loop, and
2830
// leaves some space between the logo and the first prompt.
31+
32+
// If NewlineAfter is set but NewlineWhenEmpty is not set, we do a check to see
33+
// if the last line was empty. If it wasn't, then we can print.
34+
//fmt.Println(lastLine, len(lastLine))
2935
if c.NewlineAfter {
30-
fmt.Println()
36+
if !c.NewlineWhenEmpty && i != 0 {
37+
// Print on the condition that the last input wasn't empty.
38+
if !c.lineEmpty(lastLine) {
39+
fmt.Println()
40+
}
41+
} else {
42+
fmt.Println()
43+
}
3144
}
3245

3346
// Always ensure we work with the active menu, with freshly
@@ -44,13 +57,19 @@ func (c *Console) Start() error {
4457

4558
// Block and read user input.
4659
line, err := c.shell.Readline()
47-
4860
if c.NewlineBefore {
49-
fmt.Println()
61+
if !c.NewlineWhenEmpty {
62+
if !c.lineEmpty(line) {
63+
fmt.Println()
64+
}
65+
} else {
66+
fmt.Println()
67+
}
5068
}
5169

5270
if err != nil {
5371
menu.handleInterrupt(err)
72+
lastLine = line
5473
continue
5574
}
5675

@@ -63,10 +82,12 @@ func (c *Console) Start() error {
6382
args, err := c.parse(line)
6483
if err != nil {
6584
fmt.Printf("Parsing error: %s\n", err.Error())
85+
lastLine = line
6686
continue
6787
}
6888

6989
if len(args) == 0 {
90+
lastLine = line
7091
continue
7192
}
7293

@@ -75,6 +96,7 @@ func (c *Console) Start() error {
7596
args, err = c.runLineHooks(args)
7697
if err != nil {
7798
fmt.Printf("Line error: %s\n", err.Error())
99+
lastLine = line
78100
continue
79101
}
80102

@@ -83,9 +105,10 @@ func (c *Console) Start() error {
83105
// the library user is responsible for setting
84106
// the cobra behavior.
85107
// If it's an interrupt, we take care of it.
86-
if err := c.execute(menu, args, false); err != nil {
108+
if err = c.execute(menu, args, false); err != nil {
87109
fmt.Println(err)
88110
}
111+
lastLine = line
89112
}
90113
}
91114

0 commit comments

Comments
 (0)