2021-02-09 13:33:25 -05:00
|
|
|
package tea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-22 12:53:02 -04:00
|
|
|
"fmt"
|
2021-02-09 13:33:25 -05:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-18 05:40:08 -04:00
|
|
|
"github.com/muesli/ansi/compressor"
|
2021-02-09 13:33:25 -05:00
|
|
|
"github.com/muesli/reflow/truncate"
|
2022-06-04 09:14:03 -04:00
|
|
|
"github.com/muesli/termenv"
|
2021-02-09 13:33:25 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// defaultFramerate specifies the maximum interval at which we should
|
|
|
|
// update the view.
|
|
|
|
defaultFramerate = time.Second / 60
|
|
|
|
)
|
|
|
|
|
2021-05-07 15:11:57 -04:00
|
|
|
// standardRenderer is a framerate-based terminal renderer, updating the view
|
|
|
|
// at a given framerate to avoid overloading the terminal emulator.
|
2021-02-09 13:33:25 -05:00
|
|
|
//
|
|
|
|
// 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 {
|
2022-10-03 18:04:05 -04:00
|
|
|
mtx *sync.Mutex
|
|
|
|
out *termenv.Output
|
|
|
|
|
2022-06-22 12:53:02 -04:00
|
|
|
buf bytes.Buffer
|
|
|
|
queuedMessageLines []string
|
|
|
|
framerate time.Duration
|
|
|
|
ticker *time.Ticker
|
|
|
|
done chan struct{}
|
|
|
|
lastRender string
|
|
|
|
linesRendered int
|
|
|
|
useANSICompressor bool
|
|
|
|
once sync.Once
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
// essentially whether or not we're using the full size of the terminal
|
|
|
|
altScreenActive bool
|
|
|
|
|
|
|
|
// renderer dimensions; usually the size of the window
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2021-05-07 15:11:57 -04:00
|
|
|
// lines explicitly set not to render
|
2021-02-09 13:33:25 -05:00
|
|
|
ignoreLines map[int]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRenderer creates a new renderer. Normally you'll want to initialize it
|
|
|
|
// with os.Stdout as the first argument.
|
2022-10-03 18:04:05 -04:00
|
|
|
func newRenderer(out *termenv.Output, useANSICompressor bool) renderer {
|
2021-10-30 12:29:30 -04:00
|
|
|
r := &standardRenderer{
|
2022-06-22 12:53:02 -04:00
|
|
|
out: out,
|
2022-10-03 18:04:05 -04:00
|
|
|
mtx: &sync.Mutex{},
|
2022-06-22 12:53:02 -04:00
|
|
|
framerate: defaultFramerate,
|
|
|
|
useANSICompressor: useANSICompressor,
|
|
|
|
queuedMessageLines: []string{},
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
2021-10-30 12:29:30 -04:00
|
|
|
if r.useANSICompressor {
|
2022-06-04 09:14:03 -04:00
|
|
|
r.out = termenv.NewOutput(&compressor.Writer{Forward: out})
|
2021-10-30 12:29:30 -04:00
|
|
|
}
|
|
|
|
return r
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the renderer.
|
|
|
|
func (r *standardRenderer) start() {
|
|
|
|
if r.ticker == nil {
|
|
|
|
r.ticker = time.NewTicker(r.framerate)
|
|
|
|
}
|
|
|
|
r.done = make(chan struct{})
|
|
|
|
go r.listen()
|
|
|
|
}
|
|
|
|
|
2021-05-19 21:35:36 -04:00
|
|
|
// stop permanently halts the renderer, rendering the final frame.
|
2021-02-09 13:33:25 -05:00
|
|
|
func (r *standardRenderer) stop() {
|
2022-10-08 23:53:02 -04:00
|
|
|
// flush locks the mutex
|
2021-02-09 13:33:25 -05:00
|
|
|
r.flush()
|
2022-10-08 23:53:02 -04:00
|
|
|
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
r.out.ClearLine()
|
2022-02-04 10:09:17 -05:00
|
|
|
r.once.Do(func() {
|
|
|
|
close(r.done)
|
|
|
|
})
|
2021-10-30 12:29:30 -04:00
|
|
|
|
|
|
|
if r.useANSICompressor {
|
2022-06-04 09:14:03 -04:00
|
|
|
if w, ok := r.out.TTY().(io.WriteCloser); ok {
|
2021-10-30 12:29:30 -04:00
|
|
|
_ = w.Close()
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2021-05-19 21:35:36 -04:00
|
|
|
// kill halts the renderer. The final frame will not be rendered.
|
|
|
|
func (r *standardRenderer) kill() {
|
2022-10-08 23:53:02 -04:00
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
r.out.ClearLine()
|
2022-02-04 10:09:17 -05:00
|
|
|
r.once.Do(func() {
|
|
|
|
close(r.done)
|
|
|
|
})
|
2021-05-19 21:35:36 -04:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// listen waits for ticks on the ticker, or a signal to stop the renderer.
|
|
|
|
func (r *standardRenderer) listen() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.ticker.C:
|
|
|
|
if r.ticker != nil {
|
|
|
|
r.flush()
|
|
|
|
}
|
|
|
|
case <-r.done:
|
|
|
|
r.ticker.Stop()
|
|
|
|
r.ticker = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush renders the buffer.
|
|
|
|
func (r *standardRenderer) flush() {
|
2021-03-12 22:27:16 -05:00
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
if r.buf.Len() == 0 || r.buf.String() == r.lastRender {
|
|
|
|
// Nothing to do
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:11:57 -04:00
|
|
|
// Output buffer
|
2022-06-04 09:14:03 -04:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out := termenv.NewOutput(buf)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2021-05-07 15:11:57 -04:00
|
|
|
newLines := strings.Split(r.buf.String(), "\n")
|
2022-06-22 12:53:02 -04:00
|
|
|
numLinesThisFlush := len(newLines)
|
2021-05-07 15:11:57 -04:00
|
|
|
oldLines := strings.Split(r.lastRender, "\n")
|
|
|
|
skipLines := make(map[int]struct{})
|
2022-06-22 12:53:02 -04:00
|
|
|
flushQueuedMessages := len(r.queuedMessageLines) > 0 && !r.altScreenActive
|
|
|
|
|
|
|
|
// Add any queued messages to this render
|
|
|
|
if flushQueuedMessages {
|
|
|
|
newLines = append(r.queuedMessageLines, newLines...)
|
|
|
|
r.queuedMessageLines = []string{}
|
|
|
|
}
|
2021-05-07 15:11:57 -04:00
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// Clear any lines we painted in the last render.
|
|
|
|
if r.linesRendered > 0 {
|
|
|
|
for i := r.linesRendered - 1; i > 0; i-- {
|
2021-06-21 16:17:52 -04:00
|
|
|
// If the number of lines we want to render hasn't increased and
|
|
|
|
// new line is the same as the old line we can skip rendering for
|
|
|
|
// this line as a performance optimization.
|
|
|
|
if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {
|
2021-05-07 15:11:57 -04:00
|
|
|
skipLines[i] = struct{}{}
|
2021-06-21 16:17:52 -04:00
|
|
|
} else if _, exists := r.ignoreLines[i]; !exists {
|
2022-06-04 09:14:03 -04:00
|
|
|
out.ClearLine()
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
out.CursorUp(1)
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:17:52 -04:00
|
|
|
if _, exists := r.ignoreLines[0]; !exists {
|
2021-02-09 13:33:25 -05:00
|
|
|
// We need to return to the start of the line here to properly
|
|
|
|
// erase it. Going back the entire width of the terminal will
|
|
|
|
// usually be farther than we need to go, but terminal emulators
|
|
|
|
// will stop the cursor at the start of the line as a rule.
|
|
|
|
//
|
|
|
|
// We use this sequence in particular because it's part of the ANSI
|
|
|
|
// standard (whereas others are proprietary to, say, VT100/VT52).
|
|
|
|
// If cursor previous line (ESC[ + <n> + F) were better supported
|
|
|
|
// we could use that above to eliminate this step.
|
2022-06-04 09:14:03 -04:00
|
|
|
out.CursorBack(r.width)
|
|
|
|
out.ClearLine()
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:17:52 -04:00
|
|
|
// Merge the set of lines we're skipping as a rendering optimization with
|
|
|
|
// the set of lines we've explicitly asked the renderer to ignore.
|
|
|
|
if r.ignoreLines != nil {
|
|
|
|
for k, v := range r.ignoreLines {
|
|
|
|
skipLines[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// Paint new lines
|
2021-05-07 15:11:57 -04:00
|
|
|
for i := 0; i < len(newLines); i++ {
|
2022-06-22 12:53:02 -04:00
|
|
|
if _, skip := skipLines[i]; skip {
|
2021-05-07 15:11:57 -04:00
|
|
|
// Unless this is the last line, move the cursor down.
|
|
|
|
if i < len(newLines)-1 {
|
2022-06-04 09:14:03 -04:00
|
|
|
out.CursorDown(1)
|
2021-05-07 15:11:57 -04:00
|
|
|
}
|
2021-02-09 13:33:25 -05:00
|
|
|
} else {
|
2021-05-07 15:11:57 -04:00
|
|
|
line := newLines[i]
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
// Truncate lines wider than the width of the window to avoid
|
2021-05-07 15:11:57 -04:00
|
|
|
// wrapping, which will mess up rendering. If we don't have the
|
|
|
|
// width of the window this will be ignored.
|
2021-02-09 13:33:25 -05:00
|
|
|
//
|
2021-05-07 15:11:57 -04:00
|
|
|
// Note that on Windows we only get the width of the window on
|
|
|
|
// program initialization, so after a resize this won't perform
|
|
|
|
// correctly (signal SIGWINCH is not supported on Windows).
|
2021-02-09 13:33:25 -05:00
|
|
|
if r.width > 0 {
|
|
|
|
line = truncate.String(line, uint(r.width))
|
|
|
|
}
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
_, _ = out.WriteString(line)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2021-05-14 19:53:22 -04:00
|
|
|
if i < len(newLines)-1 {
|
2022-06-04 09:14:03 -04:00
|
|
|
_, _ = out.WriteString("\r\n")
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 12:53:02 -04:00
|
|
|
r.linesRendered = numLinesThisFlush
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
// Make sure the cursor is at the start of the last line to keep rendering
|
|
|
|
// behavior consistent.
|
|
|
|
if r.altScreenActive {
|
2021-05-07 15:11:57 -04:00
|
|
|
// This case fixes a bug in macOS terminal. In other terminals the
|
|
|
|
// other case seems to do the job regardless of whether or not we're
|
2021-02-09 13:33:25 -05:00
|
|
|
// using the full terminal window.
|
2022-06-04 09:14:03 -04:00
|
|
|
out.MoveCursor(r.linesRendered, 0)
|
2021-02-09 13:33:25 -05:00
|
|
|
} else {
|
2022-06-04 09:14:03 -04:00
|
|
|
out.CursorBack(r.width)
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
_, _ = r.out.Write(buf.Bytes())
|
2021-02-09 13:33:25 -05:00
|
|
|
r.lastRender = r.buf.String()
|
|
|
|
r.buf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes to the internal buffer. The buffer will be outputted via the
|
|
|
|
// ticker which calls flush().
|
|
|
|
func (r *standardRenderer) write(s string) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
r.buf.Reset()
|
2021-06-02 13:20:30 -04:00
|
|
|
|
|
|
|
// If an empty string was passed we should clear existing output and
|
|
|
|
// rendering nothing. Rather than introduce additional state to manage
|
|
|
|
// this, we render a single space as a simple (albeit less correct)
|
|
|
|
// solution.
|
|
|
|
if s == "" {
|
|
|
|
s = " "
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
_, _ = r.buf.WriteString(s)
|
|
|
|
}
|
|
|
|
|
2021-05-28 21:41:42 -04:00
|
|
|
func (r *standardRenderer) repaint() {
|
|
|
|
r.lastRender = ""
|
|
|
|
}
|
|
|
|
|
2022-08-25 13:13:30 -04:00
|
|
|
func (r *standardRenderer) clearScreen() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.ClearScreen()
|
|
|
|
r.out.MoveCursor(1, 1)
|
2022-10-04 11:26:52 -04:00
|
|
|
|
|
|
|
r.repaint()
|
2022-08-25 13:13:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
func (r *standardRenderer) altScreen() bool {
|
2022-10-13 00:48:26 -04:00
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
return r.altScreenActive
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:04:05 -04:00
|
|
|
func (r *standardRenderer) enterAltScreen() {
|
2022-10-12 22:33:34 -04:00
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-10-06 06:38:15 -04:00
|
|
|
if r.altScreenActive {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:04:05 -04:00
|
|
|
r.altScreenActive = true
|
|
|
|
r.out.AltScreen()
|
2022-08-25 13:18:06 -04:00
|
|
|
|
|
|
|
// Ensure that the terminal is cleared, even when it doesn't support
|
|
|
|
// alt screen (or alt screen support is disabled, like GNU screen by
|
|
|
|
// default).
|
|
|
|
//
|
|
|
|
// Note: we can't use r.clearScreen() here because the mutex is already
|
|
|
|
// locked.
|
|
|
|
r.out.ClearScreen()
|
2022-10-03 18:04:05 -04:00
|
|
|
r.out.MoveCursor(1, 1)
|
2022-08-25 13:18:06 -04:00
|
|
|
|
2022-10-03 18:04:05 -04:00
|
|
|
r.repaint()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) exitAltScreen() {
|
2022-10-12 22:33:34 -04:00
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-10-06 06:38:15 -04:00
|
|
|
if !r.altScreenActive {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:04:05 -04:00
|
|
|
r.altScreenActive = false
|
|
|
|
r.out.ExitAltScreen()
|
2022-08-25 13:18:06 -04:00
|
|
|
|
2022-01-07 13:16:15 -05:00
|
|
|
r.repaint()
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
2022-10-03 18:04:05 -04:00
|
|
|
func (r *standardRenderer) showCursor() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.ShowCursor()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) hideCursor() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.HideCursor()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) enableMouseCellMotion() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.EnableMouseCellMotion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) disableMouseCellMotion() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.DisableMouseCellMotion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) enableMouseAllMotion() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.EnableMouseAllMotion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *standardRenderer) disableMouseAllMotion() {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.out.DisableMouseAllMotion()
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// 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{})
|
|
|
|
}
|
|
|
|
for i := from; i < to; i++ {
|
|
|
|
r.ignoreLines[i] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase ignored lines
|
|
|
|
if r.linesRendered > 0 {
|
2022-06-04 09:14:03 -04:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out := termenv.NewOutput(buf)
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
for i := r.linesRendered - 1; i >= 0; i-- {
|
|
|
|
if _, exists := r.ignoreLines[i]; exists {
|
2022-06-04 09:14:03 -04:00
|
|
|
out.ClearLine()
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
2022-06-04 09:14:03 -04:00
|
|
|
out.CursorUp(1)
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
2022-06-04 09:14:03 -04:00
|
|
|
out.MoveCursor(r.linesRendered, 0) // put cursor back
|
|
|
|
_, _ = r.out.Write(buf.Bytes())
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearIgnoredLines returns control of any ignored lines to the standard
|
|
|
|
// Bubble Tea renderer. That is, any lines previously set to be ignored can be
|
|
|
|
// rendered to again.
|
|
|
|
func (r *standardRenderer) clearIgnoredLines() {
|
|
|
|
r.ignoreLines = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// insertTop effectively scrolls up. It inserts lines at the top of a given
|
|
|
|
// area designated to be a scrollable region, pushing everything else down.
|
|
|
|
// This is roughly how ncurses does it.
|
|
|
|
//
|
|
|
|
// To call this function use command ScrollUp().
|
|
|
|
//
|
|
|
|
// For this to work renderer.ignoreLines must be set to ignore the scrollable
|
|
|
|
// region since we are bypassing the normal Bubble Tea renderer here.
|
|
|
|
//
|
|
|
|
// Because this method relies on the terminal dimensions, it's only valid for
|
|
|
|
// full-window applications (generally those that use the alternate screen
|
|
|
|
// buffer).
|
|
|
|
//
|
|
|
|
// This method bypasses the normal rendering buffer and is philosophically
|
|
|
|
// different than the normal way we approach rendering in Bubble Tea. It's for
|
|
|
|
// use in high-performance rendering, such as a pager that could potentially
|
|
|
|
// be rendering very complicated ansi. In cases where the content is simpler
|
|
|
|
// standard Bubble Tea rendering should suffice.
|
|
|
|
func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary int) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out := termenv.NewOutput(buf)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
out.ChangeScrollingRegion(topBoundary, bottomBoundary)
|
|
|
|
out.MoveCursor(topBoundary, 0)
|
|
|
|
out.InsertLines(len(lines))
|
|
|
|
_, _ = out.WriteString(strings.Join(lines, "\r\n"))
|
|
|
|
out.ChangeScrollingRegion(0, r.height)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
// Move cursor back to where the main rendering routine expects it to be
|
2022-06-04 09:14:03 -04:00
|
|
|
out.MoveCursor(r.linesRendered, 0)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
_, _ = r.out.Write(buf.Bytes())
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertBottom effectively scrolls down. It inserts lines at the bottom of
|
|
|
|
// a given area designated to be a scrollable region, pushing everything else
|
|
|
|
// up. This is roughly how ncurses does it.
|
|
|
|
//
|
|
|
|
// To call this function use the command ScrollDown().
|
|
|
|
//
|
|
|
|
// See note in insertTop() for caveats, how this function only makes sense for
|
|
|
|
// full-window applications, and how it differs from the normal way we do
|
|
|
|
// rendering in Bubble Tea.
|
|
|
|
func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBoundary int) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out := termenv.NewOutput(buf)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
out.ChangeScrollingRegion(topBoundary, bottomBoundary)
|
|
|
|
out.MoveCursor(bottomBoundary, 0)
|
|
|
|
_, _ = out.WriteString("\r\n" + strings.Join(lines, "\r\n"))
|
|
|
|
out.ChangeScrollingRegion(0, r.height)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
// Move cursor back to where the main rendering routine expects it to be
|
2022-06-04 09:14:03 -04:00
|
|
|
out.MoveCursor(r.linesRendered, 0)
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
_, _ = r.out.Write(buf.Bytes())
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleMessages handles internal messages for the renderer.
|
|
|
|
func (r *standardRenderer) handleMessages(msg Msg) {
|
|
|
|
switch msg := msg.(type) {
|
2022-04-12 10:23:10 -04:00
|
|
|
case repaintMsg:
|
|
|
|
// Force a repaint by clearing the render cache as we slide into a
|
|
|
|
// render.
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Lock()
|
2022-04-12 10:23:10 -04:00
|
|
|
r.repaint()
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Unlock()
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
case WindowSizeMsg:
|
2021-04-05 02:18:02 -04:00
|
|
|
r.mtx.Lock()
|
2021-02-09 13:33:25 -05:00
|
|
|
r.width = msg.Width
|
|
|
|
r.height = msg.Height
|
2022-10-03 18:04:05 -04:00
|
|
|
r.repaint()
|
2021-04-05 02:18:02 -04:00
|
|
|
r.mtx.Unlock()
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
case clearScrollAreaMsg:
|
|
|
|
r.clearIgnoredLines()
|
|
|
|
|
|
|
|
// Force a repaint on the area where the scrollable stuff was in this
|
|
|
|
// update cycle
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Lock()
|
2022-06-04 08:03:48 -04:00
|
|
|
r.repaint()
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Unlock()
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
case syncScrollAreaMsg:
|
|
|
|
// Re-render scrolling area
|
|
|
|
r.clearIgnoredLines()
|
|
|
|
r.setIgnoredLines(msg.topBoundary, msg.bottomBoundary)
|
|
|
|
r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)
|
|
|
|
|
|
|
|
// Force non-scrolling stuff to repaint in this update cycle
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Lock()
|
2022-06-04 08:03:48 -04:00
|
|
|
r.repaint()
|
2022-06-05 06:58:33 -04:00
|
|
|
r.mtx.Unlock()
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
case scrollUpMsg:
|
|
|
|
r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)
|
|
|
|
|
|
|
|
case scrollDownMsg:
|
|
|
|
r.insertBottom(msg.lines, msg.topBoundary, msg.bottomBoundary)
|
2022-06-22 12:53:02 -04:00
|
|
|
|
|
|
|
case printLineMessage:
|
|
|
|
if !r.altScreenActive {
|
|
|
|
lines := strings.Split(msg.messageBody, "\n")
|
|
|
|
r.mtx.Lock()
|
|
|
|
r.queuedMessageLines = append(r.queuedMessageLines, lines...)
|
|
|
|
r.repaint()
|
|
|
|
r.mtx.Unlock()
|
|
|
|
}
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HIGH-PERFORMANCE RENDERING STUFF
|
|
|
|
|
|
|
|
type syncScrollAreaMsg struct {
|
|
|
|
lines []string
|
|
|
|
topBoundary int
|
|
|
|
bottomBoundary int
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncScrollArea performs a paint of the entire region designated to be the
|
|
|
|
// scrollable area. This is required to initialize the scrollable region and
|
|
|
|
// should also be called on resize (WindowSizeMsg).
|
|
|
|
//
|
|
|
|
// For high-performance, scroll-based rendering only.
|
|
|
|
func SyncScrollArea(lines []string, topBoundary int, bottomBoundary int) Cmd {
|
|
|
|
return func() Msg {
|
|
|
|
return syncScrollAreaMsg{
|
|
|
|
lines: lines,
|
|
|
|
topBoundary: topBoundary,
|
|
|
|
bottomBoundary: bottomBoundary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type clearScrollAreaMsg struct{}
|
|
|
|
|
|
|
|
// ClearScrollArea deallocates the scrollable region and returns the control of
|
|
|
|
// those lines to the main rendering routine.
|
|
|
|
//
|
|
|
|
// For high-performance, scroll-based rendering only.
|
|
|
|
func ClearScrollArea() Msg {
|
|
|
|
return clearScrollAreaMsg{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type scrollUpMsg struct {
|
|
|
|
lines []string
|
|
|
|
topBoundary int
|
|
|
|
bottomBoundary int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollUp adds lines to the top of the scrollable region, pushing existing
|
|
|
|
// lines below down. Lines that are pushed out the scrollable region disappear
|
|
|
|
// from view.
|
|
|
|
//
|
|
|
|
// For high-performance, scroll-based rendering only.
|
|
|
|
func ScrollUp(newLines []string, topBoundary, bottomBoundary int) Cmd {
|
|
|
|
return func() Msg {
|
|
|
|
return scrollUpMsg{
|
|
|
|
lines: newLines,
|
|
|
|
topBoundary: topBoundary,
|
|
|
|
bottomBoundary: bottomBoundary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type scrollDownMsg struct {
|
|
|
|
lines []string
|
|
|
|
topBoundary int
|
|
|
|
bottomBoundary int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollDown adds lines to the bottom of the scrollable region, pushing
|
|
|
|
// existing lines above up. Lines that are pushed out of the scrollable region
|
|
|
|
// disappear from view.
|
|
|
|
//
|
|
|
|
// For high-performance, scroll-based rendering only.
|
|
|
|
func ScrollDown(newLines []string, topBoundary, bottomBoundary int) Cmd {
|
|
|
|
return func() Msg {
|
|
|
|
return scrollDownMsg{
|
|
|
|
lines: newLines,
|
|
|
|
topBoundary: topBoundary,
|
|
|
|
bottomBoundary: bottomBoundary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 12:53:02 -04:00
|
|
|
|
|
|
|
type printLineMessage struct {
|
|
|
|
messageBody string
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:22:33 -04:00
|
|
|
// Println prints above the Program. This output is unmanaged by the program and
|
2022-06-22 12:53:02 -04:00
|
|
|
// will persist across renders by the Program.
|
|
|
|
//
|
2022-07-19 19:22:33 -04:00
|
|
|
// Unlike fmt.Println (but similar to log.Println) the message will be print on
|
2022-06-22 12:53:02 -04:00
|
|
|
// its own line.
|
|
|
|
//
|
|
|
|
// If the altscreen is active no output will be printed.
|
|
|
|
func Println(args ...interface{}) Cmd {
|
|
|
|
return func() Msg {
|
|
|
|
return printLineMessage{
|
|
|
|
messageBody: fmt.Sprint(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf prints above the Program. It takes a format template followed by
|
|
|
|
// values similar to fmt.Printf. This output is unmanaged by the program and
|
|
|
|
// will persist across renders by the Program.
|
|
|
|
//
|
|
|
|
// Unlike fmt.Printf (but similar to log.Printf) the message will be print on
|
|
|
|
// its own line.
|
|
|
|
//
|
|
|
|
// If the altscreen is active no output will be printed.
|
|
|
|
func Printf(template string, args ...interface{}) Cmd {
|
|
|
|
return func() Msg {
|
|
|
|
return printLineMessage{
|
|
|
|
messageBody: fmt.Sprintf(template, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|