Skip to content

Commit a4d8614

Browse files
committed
Make box last line linefeed optional
There are scenarios where the box takes up the whole terminal height. In those use cases, the linefeed at the last line takes away the possibility to use the full terminal. Make the linefeed at the last line configurable by introducing a box style function called `NoFinalEndOfLine`. By default, a linefeed is added.
1 parent 2f44aef commit a4d8614

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

box.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ import (
3535
type BoxStyle func(*boxOptions)
3636

3737
type boxOptions struct {
38-
headlineColor *colorful.Color
39-
contentColor *colorful.Color
40-
headlineStyles []bunt.StyleOption
38+
headlineColor *colorful.Color
39+
contentColor *colorful.Color
40+
headlineStyles []bunt.StyleOption
41+
noClosingEndOfLine bool
4142
}
4243

4344
// HeadlineColor sets the color of the headline text
@@ -61,6 +62,13 @@ func ContentColor(color colorful.Color) BoxStyle {
6162
}
6263
}
6364

65+
// NoFinalEndOfLine specifies that the rendering does not add a closing linefeed
66+
func NoFinalEndOfLine() BoxStyle {
67+
return func(options *boxOptions) {
68+
options.noClosingEndOfLine = true
69+
}
70+
}
71+
6472
// ContentBox creates a string for the terminal where content is printed inside
6573
// a simple box shape.
6674
func ContentBox(headline string, content string, opts ...BoxStyle) string {
@@ -120,6 +128,11 @@ func Box(out io.Writer, headline string, content io.Reader, opts ...BoxStyle) {
120128
}
121129

122130
if linewritten {
123-
outprint("%s\n", lastline)
131+
outprint(lastline)
132+
133+
// If not configured otherwise, end with a linefeed
134+
if !options.noClosingEndOfLine {
135+
outprint("\n")
136+
}
124137
}
125138
}

box_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ DodgerBlue{│} DimGray{content}
123123
DodgerBlue{╵}
124124
`)))
125125
})
126+
127+
It("should create a content box with no trailing line feed", func() {
128+
Expect("\n" + ContentBox(
129+
headline,
130+
content,
131+
NoFinalEndOfLine(),
132+
)).To(BeEquivalentTo(Sprintf(`
133+
╭ headline
134+
│ multi
135+
│ line
136+
│ content
137+
╵`)))
138+
})
126139
})
127140

128141
Context("rendering content boxes with already colored content", func() {

0 commit comments

Comments
 (0)