Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WithoutEmptyRenders #796

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ func WithANSICompressor() ProgramOption {
}
}

// WithoutEmptyRenders makes bubbletea do nothing when the View method return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible typo 😊

Suggested change
// WithoutEmptyRenders makes bubbletea do nothing when the View method return
// WithoutEmptyRenders makes bubbletea do nothing when the View method returns

// an empty string.
//
// By default, it will render a space instead.
func WithoutEmptyRenders() ProgramOption {
return func(p *Program) {
p.startupOptions |= withoutEmptyRenders
}
}

// WithFilter supplies an event filter that will be invoked before Bubble Tea
// processes a tea.Msg. The event filter can return any tea.Msg which will then
// get handled by Bubble Tea instead of the original event. If the event filter
Expand Down
5 changes: 4 additions & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func TestOptions(t *testing.T) {
var b bytes.Buffer
exercise(t, WithInput(&b), customInput)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: change does not seem to be directly related, perhaps it was committed accidentally?

})

t.Run("startup options", func(t *testing.T) {
Expand All @@ -92,6 +91,10 @@ func TestOptions(t *testing.T) {
exercise(t, WithoutSignalHandler(), withoutSignalHandler)
})

t.Run("without empty renders", func(t *testing.T) {
exercise(t, WithoutEmptyRenders(), withoutEmptyRenders)
})

t.Run("mouse cell motion", func(t *testing.T) {
p := NewProgram(nil, WithMouseAllMotion(), WithMouseCellMotion())
if !p.startupOptions.has(withMouseCellMotion) {
Expand Down
6 changes: 4 additions & 2 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type standardRenderer struct {
lastRender string
linesRendered int
useANSICompressor bool
renderEmpty bool
once sync.Once

// cursor visibility state
Expand All @@ -55,7 +56,7 @@ type standardRenderer struct {

// newRenderer creates a new renderer. Normally you'll want to initialize it
// with os.Stdout as the first argument.
func newRenderer(out *termenv.Output, useANSICompressor bool, fps int) renderer {
func newRenderer(out *termenv.Output, useANSICompressor, renderEmpty bool, fps int) renderer {
if fps < 1 {
fps = defaultFPS
} else if fps > maxFPS {
Expand All @@ -67,6 +68,7 @@ func newRenderer(out *termenv.Output, useANSICompressor bool, fps int) renderer
done: make(chan struct{}),
framerate: time.Second / time.Duration(fps),
useANSICompressor: useANSICompressor,
renderEmpty: renderEmpty,
queuedMessageLines: []string{},
}
if r.useANSICompressor {
Expand Down Expand Up @@ -271,7 +273,7 @@ func (r *standardRenderer) write(s string) {
// rendering nothing. Rather than introduce additional state to manage
// this, we render a single space as a simple (albeit less correct)
// solution.
if s == "" {
if s == "" && r.renderEmpty {
s = " "
}

Expand Down
9 changes: 8 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const (
// recover from panics, print the stack trace, and disable raw mode. This
// feature is on by default.
withoutCatchPanics

withoutEmptyRenders
)

// handlers manages series of channels returned by various processes. It allows
Expand Down Expand Up @@ -470,7 +472,12 @@ func (p *Program) Run() (Model, error) {

// If no renderer is set use the standard one.
if p.renderer == nil {
p.renderer = newRenderer(p.output, p.startupOptions.has(withANSICompressor), p.fps)
p.renderer = newRenderer(
p.output,
p.startupOptions.has(withANSICompressor),
!p.startupOptions.has(withoutEmptyRenders),
p.fps,
)
}

// Check if output is a TTY before entering raw mode, hiding the cursor and
Expand Down
Loading