Skip to content

Commit

Permalink
fix: remove standard renderer mutex
Browse files Browse the repository at this point in the history
We no longer need a mutex for standardRenderer since the program uses a
safeWriter.
  • Loading branch information
aymanbagabas committed Sep 12, 2024
1 parent 1e530ba commit 31e7e04
Showing 1 changed file with 0 additions and 35 deletions.
35 changes: 0 additions & 35 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"strings"
"sync"

"github.com/charmbracelet/x/ansi"
)
Expand All @@ -23,7 +22,6 @@ const (
// In cases where very high performance is needed the renderer can be told
// to exclude ranges of lines, allowing them to be written to directly.
type standardRenderer struct {
mtx *sync.Mutex
out io.Writer

buf bytes.Buffer
Expand All @@ -49,17 +47,14 @@ type standardRenderer struct {
// with os.Stdout as the first argument.
func newStandardRenderer() renderer {
r := &standardRenderer{
mtx: &sync.Mutex{},
queuedMessageLines: []string{},
}
return r
}

// setOutput sets the output for the renderer.
func (r *standardRenderer) setOutput(out io.Writer) {
r.mtx.Lock()
r.out = out
r.mtx.Unlock()
}

// close closes the renderer and flushes any remaining data.
Expand All @@ -73,16 +68,11 @@ func (r *standardRenderer) close() (err error) {

// execute writes the given sequence to the output.
func (r *standardRenderer) execute(seq string) {
r.mtx.Lock()
_, _ = io.WriteString(r.out, seq)
r.mtx.Unlock()
}

// flush renders the buffer.
func (r *standardRenderer) flush() (err error) {
r.mtx.Lock()
defer r.mtx.Unlock()

if r.buf.Len() == 0 || r.buf.String() == r.lastRender {
// Nothing to do
return nil
Expand Down Expand Up @@ -213,8 +203,6 @@ func (r *standardRenderer) flush() (err error) {
// render renders the frame to the internal buffer. The buffer will be
// outputted via the ticker which calls flush().
func (r *standardRenderer) render(s string) {
r.mtx.Lock()
defer r.mtx.Unlock()
r.buf.Reset()

// If an empty string was passed we should clear existing output and
Expand Down Expand Up @@ -247,13 +235,6 @@ func (r *standardRenderer) clearScreen() {
// setIgnoredLines specifies lines not to be touched by the standard Bubble Tea
// renderer.
func (r *standardRenderer) setIgnoredLines(from int, to int) {
// Lock if we're going to be clearing some lines since we don't want
// anything jacking our cursor.
if r.linesRendered > 0 {
r.mtx.Lock()
defer r.mtx.Unlock()
}

if r.ignoreLines == nil {
r.ignoreLines = make(map[int]struct{})
}
Expand Down Expand Up @@ -305,9 +286,6 @@ func (r *standardRenderer) clearIgnoredLines() {
// Deprecated: This option is deprecated and will be removed in a future
// version of this package.
func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary int) {
r.mtx.Lock()
defer r.mtx.Unlock()

buf := &bytes.Buffer{}

buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))
Expand Down Expand Up @@ -335,9 +313,6 @@ func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary
// Deprecated: This option is deprecated and will be removed in a future
// version of this package.
func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBoundary int) {
r.mtx.Lock()
defer r.mtx.Unlock()

buf := &bytes.Buffer{}

buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))
Expand Down Expand Up @@ -403,18 +378,14 @@ func (r *standardRenderer) update(msg Msg) {
case repaintMsg:
// Force a repaint by clearing the render cache as we slide into a
// render.
r.mtx.Lock()
r.repaint()
r.mtx.Unlock()

case clearScrollAreaMsg:
r.clearIgnoredLines()

// Force a repaint on the area where the scrollable stuff was in this
// update cycle
r.mtx.Lock()
r.repaint()
r.mtx.Unlock()

case syncScrollAreaMsg:
// Re-render scrolling area
Expand All @@ -423,9 +394,7 @@ func (r *standardRenderer) update(msg Msg) {
r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)

// Force non-scrolling stuff to repaint in this update cycle
r.mtx.Lock()
r.repaint()
r.mtx.Unlock()

case scrollUpMsg:
r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)
Expand All @@ -437,11 +406,9 @@ func (r *standardRenderer) update(msg Msg) {

// resize sets the size of the terminal.
func (r *standardRenderer) resize(w int, h int) {
r.mtx.Lock()
r.width = w
r.height = h
r.repaint()
r.mtx.Unlock()
}

// insertAbove inserts lines above the current frame. This only works in
Expand All @@ -452,10 +419,8 @@ func (r *standardRenderer) insertAbove(s string) {
}

lines := strings.Split(s, "\n")
r.mtx.Lock()
r.queuedMessageLines = append(r.queuedMessageLines, lines...)
r.repaint()
r.mtx.Unlock()
}

// HIGH-PERFORMANCE RENDERING STUFF
Expand Down

0 comments on commit 31e7e04

Please sign in to comment.