standard_renderer.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. package tea
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/charmbracelet/x/ansi"
  10. "github.com/muesli/ansi/compressor"
  11. )
  12. const (
  13. // defaultFramerate specifies the maximum interval at which we should
  14. // update the view.
  15. defaultFPS = 60
  16. maxFPS = 120
  17. )
  18. // standardRenderer is a framerate-based terminal renderer, updating the view
  19. // at a given framerate to avoid overloading the terminal emulator.
  20. //
  21. // In cases where very high performance is needed the renderer can be told
  22. // to exclude ranges of lines, allowing them to be written to directly.
  23. type standardRenderer struct {
  24. mtx *sync.Mutex
  25. out io.Writer
  26. buf bytes.Buffer
  27. queuedMessageLines []string
  28. framerate time.Duration
  29. ticker *time.Ticker
  30. done chan struct{}
  31. lastRender string
  32. lastRenderedLines []string
  33. linesRendered int
  34. altLinesRendered int
  35. useANSICompressor bool
  36. once sync.Once
  37. // cursor visibility state
  38. cursorHidden bool
  39. // essentially whether or not we're using the full size of the terminal
  40. altScreenActive bool
  41. // whether or not we're currently using bracketed paste
  42. bpActive bool
  43. // reportingFocus whether reporting focus events is enabled
  44. reportingFocus bool
  45. // renderer dimensions; usually the size of the window
  46. width int
  47. height int
  48. // lines explicitly set not to render
  49. ignoreLines map[int]struct{}
  50. }
  51. // newRenderer creates a new renderer. Normally you'll want to initialize it
  52. // with os.Stdout as the first argument.
  53. func newRenderer(out io.Writer, useANSICompressor bool, fps int) renderer {
  54. if fps < 1 {
  55. fps = defaultFPS
  56. } else if fps > maxFPS {
  57. fps = maxFPS
  58. }
  59. r := &standardRenderer{
  60. out: out,
  61. mtx: &sync.Mutex{},
  62. done: make(chan struct{}),
  63. framerate: time.Second / time.Duration(fps),
  64. useANSICompressor: useANSICompressor,
  65. queuedMessageLines: []string{},
  66. }
  67. if r.useANSICompressor {
  68. r.out = &compressor.Writer{Forward: out}
  69. }
  70. return r
  71. }
  72. // start starts the renderer.
  73. func (r *standardRenderer) start() {
  74. if r.ticker == nil {
  75. r.ticker = time.NewTicker(r.framerate)
  76. } else {
  77. // If the ticker already exists, it has been stopped and we need to
  78. // reset it.
  79. r.ticker.Reset(r.framerate)
  80. }
  81. // Since the renderer can be restarted after a stop, we need to reset
  82. // the done channel and its corresponding sync.Once.
  83. r.once = sync.Once{}
  84. go r.listen()
  85. }
  86. // stop permanently halts the renderer, rendering the final frame.
  87. func (r *standardRenderer) stop() {
  88. // Stop the renderer before acquiring the mutex to avoid a deadlock.
  89. r.once.Do(func() {
  90. r.done <- struct{}{}
  91. })
  92. // flush locks the mutex
  93. r.flush()
  94. r.mtx.Lock()
  95. defer r.mtx.Unlock()
  96. r.execute(ansi.EraseEntireLine)
  97. // Move the cursor back to the beginning of the line
  98. r.execute("\r")
  99. if r.useANSICompressor {
  100. if w, ok := r.out.(io.WriteCloser); ok {
  101. _ = w.Close()
  102. }
  103. }
  104. }
  105. // execute writes a sequence to the terminal.
  106. func (r *standardRenderer) execute(seq string) {
  107. _, _ = io.WriteString(r.out, seq)
  108. }
  109. // kill halts the renderer. The final frame will not be rendered.
  110. func (r *standardRenderer) kill() {
  111. // Stop the renderer before acquiring the mutex to avoid a deadlock.
  112. r.once.Do(func() {
  113. r.done <- struct{}{}
  114. })
  115. r.mtx.Lock()
  116. defer r.mtx.Unlock()
  117. r.execute(ansi.EraseEntireLine)
  118. // Move the cursor back to the beginning of the line
  119. r.execute("\r")
  120. }
  121. // listen waits for ticks on the ticker, or a signal to stop the renderer.
  122. func (r *standardRenderer) listen() {
  123. for {
  124. select {
  125. case <-r.done:
  126. r.ticker.Stop()
  127. return
  128. case <-r.ticker.C:
  129. r.flush()
  130. }
  131. }
  132. }
  133. // flush renders the buffer.
  134. func (r *standardRenderer) flush() {
  135. r.mtx.Lock()
  136. defer r.mtx.Unlock()
  137. if r.buf.Len() == 0 || r.buf.String() == r.lastRender {
  138. // Nothing to do.
  139. return
  140. }
  141. // Output buffer.
  142. buf := &bytes.Buffer{}
  143. // Moving to the beginning of the section, that we rendered.
  144. if r.altScreenActive {
  145. buf.WriteString(ansi.HomeCursorPosition)
  146. } else if r.linesRendered > 1 {
  147. buf.WriteString(ansi.CursorUp(r.linesRendered - 1))
  148. }
  149. newLines := strings.Split(r.buf.String(), "\n")
  150. // If we know the output's height, we can use it to determine how many
  151. // lines we can render. We drop lines from the top of the render buffer if
  152. // necessary, as we can't navigate the cursor into the terminal's scrollback
  153. // buffer.
  154. if r.height > 0 && len(newLines) > r.height {
  155. newLines = newLines[len(newLines)-r.height:]
  156. }
  157. flushQueuedMessages := len(r.queuedMessageLines) > 0 && !r.altScreenActive
  158. if flushQueuedMessages {
  159. // Dump the lines we've queued up for printing.
  160. for _, line := range r.queuedMessageLines {
  161. if ansi.StringWidth(line) < r.width {
  162. // We only erase the rest of the line when the line is shorter than
  163. // the width of the terminal. When the cursor reaches the end of
  164. // the line, any escape sequences that follow will only affect the
  165. // last cell of the line.
  166. // Removing previously rendered content at the end of line.
  167. line = line + ansi.EraseLineRight
  168. }
  169. _, _ = buf.WriteString(line)
  170. _, _ = buf.WriteString("\r\n")
  171. }
  172. // Clear the queued message lines.
  173. r.queuedMessageLines = []string{}
  174. }
  175. // Paint new lines.
  176. for i := 0; i < len(newLines); i++ {
  177. canSkip := !flushQueuedMessages && // Queuing messages triggers repaint -> we don't have access to previous frame content.
  178. len(r.lastRenderedLines) > i && r.lastRenderedLines[i] == newLines[i] // Previously rendered line is the same.
  179. if _, ignore := r.ignoreLines[i]; ignore || canSkip {
  180. // Unless this is the last line, move the cursor down.
  181. if i < len(newLines)-1 {
  182. buf.WriteString(ansi.CursorDown1)
  183. }
  184. continue
  185. }
  186. if i == 0 && r.lastRender == "" {
  187. // On first render, reset the cursor to the start of the line
  188. // before writing anything.
  189. buf.WriteByte('\r')
  190. }
  191. line := newLines[i]
  192. // Truncate lines wider than the width of the window to avoid
  193. // wrapping, which will mess up rendering. If we don't have the
  194. // width of the window this will be ignored.
  195. //
  196. // Note that on Windows we only get the width of the window on
  197. // program initialization, so after a resize this won't perform
  198. // correctly (signal SIGWINCH is not supported on Windows).
  199. if r.width > 0 {
  200. line = ansi.Truncate(line, r.width, "")
  201. }
  202. if ansi.StringWidth(line) < r.width {
  203. // We only erase the rest of the line when the line is shorter than
  204. // the width of the terminal. When the cursor reaches the end of
  205. // the line, any escape sequences that follow will only affect the
  206. // last cell of the line.
  207. // Removing previously rendered content at the end of line.
  208. line = line + ansi.EraseLineRight
  209. }
  210. _, _ = buf.WriteString(line)
  211. if i < len(newLines)-1 {
  212. _, _ = buf.WriteString("\r\n")
  213. }
  214. }
  215. lastLinesRendered := r.linesRendered
  216. if r.altScreenActive {
  217. lastLinesRendered = r.altLinesRendered
  218. }
  219. // Clearing left over content from last render.
  220. if lastLinesRendered > len(newLines) {
  221. buf.WriteString(ansi.EraseScreenBelow)
  222. }
  223. if r.altScreenActive {
  224. r.altLinesRendered = len(newLines)
  225. } else {
  226. r.linesRendered = len(newLines)
  227. }
  228. // Make sure the cursor is at the start of the last line to keep rendering
  229. // behavior consistent.
  230. if r.altScreenActive {
  231. // This case fixes a bug in macOS terminal. In other terminals the
  232. // other case seems to do the job regardless of whether or not we're
  233. // using the full terminal window.
  234. buf.WriteString(ansi.SetCursorPosition(0, len(newLines)))
  235. } else {
  236. buf.WriteString(ansi.CursorLeft(r.width))
  237. }
  238. _, _ = r.out.Write(buf.Bytes())
  239. r.lastRender = r.buf.String()
  240. // Save previously rendered lines for comparison in the next render. If we
  241. // don't do this, we can't skip rendering lines that haven't changed.
  242. // See https://github.com/charmbracelet/bubbletea/pull/1233
  243. r.lastRenderedLines = newLines
  244. r.buf.Reset()
  245. }
  246. // write writes to the internal buffer. The buffer will be outputted via the
  247. // ticker which calls flush().
  248. func (r *standardRenderer) write(s string) {
  249. r.mtx.Lock()
  250. defer r.mtx.Unlock()
  251. r.buf.Reset()
  252. // If an empty string was passed we should clear existing output and
  253. // rendering nothing. Rather than introduce additional state to manage
  254. // this, we render a single space as a simple (albeit less correct)
  255. // solution.
  256. if s == "" {
  257. s = " "
  258. }
  259. _, _ = r.buf.WriteString(s)
  260. }
  261. func (r *standardRenderer) repaint() {
  262. r.lastRender = ""
  263. r.lastRenderedLines = nil
  264. }
  265. func (r *standardRenderer) clearScreen() {
  266. r.mtx.Lock()
  267. defer r.mtx.Unlock()
  268. r.execute(ansi.EraseEntireScreen)
  269. r.execute(ansi.HomeCursorPosition)
  270. r.repaint()
  271. }
  272. func (r *standardRenderer) altScreen() bool {
  273. r.mtx.Lock()
  274. defer r.mtx.Unlock()
  275. return r.altScreenActive
  276. }
  277. func (r *standardRenderer) enterAltScreen() {
  278. r.mtx.Lock()
  279. defer r.mtx.Unlock()
  280. if r.altScreenActive {
  281. return
  282. }
  283. r.altScreenActive = true
  284. r.execute(ansi.EnableAltScreenBuffer)
  285. // Ensure that the terminal is cleared, even when it doesn't support
  286. // alt screen (or alt screen support is disabled, like GNU screen by
  287. // default).
  288. //
  289. // Note: we can't use r.clearScreen() here because the mutex is already
  290. // locked.
  291. r.execute(ansi.EraseEntireScreen)
  292. r.execute(ansi.HomeCursorPosition)
  293. // cmd.exe and other terminals keep separate cursor states for the AltScreen
  294. // and the main buffer. We have to explicitly reset the cursor visibility
  295. // whenever we enter AltScreen.
  296. if r.cursorHidden {
  297. r.execute(ansi.HideCursor)
  298. } else {
  299. r.execute(ansi.ShowCursor)
  300. }
  301. // Entering the alt screen resets the lines rendered count.
  302. r.altLinesRendered = 0
  303. r.repaint()
  304. }
  305. func (r *standardRenderer) exitAltScreen() {
  306. r.mtx.Lock()
  307. defer r.mtx.Unlock()
  308. if !r.altScreenActive {
  309. return
  310. }
  311. r.altScreenActive = false
  312. r.execute(ansi.DisableAltScreenBuffer)
  313. // cmd.exe and other terminals keep separate cursor states for the AltScreen
  314. // and the main buffer. We have to explicitly reset the cursor visibility
  315. // whenever we exit AltScreen.
  316. if r.cursorHidden {
  317. r.execute(ansi.HideCursor)
  318. } else {
  319. r.execute(ansi.ShowCursor)
  320. }
  321. r.repaint()
  322. }
  323. func (r *standardRenderer) showCursor() {
  324. r.mtx.Lock()
  325. defer r.mtx.Unlock()
  326. r.cursorHidden = false
  327. r.execute(ansi.ShowCursor)
  328. }
  329. func (r *standardRenderer) hideCursor() {
  330. r.mtx.Lock()
  331. defer r.mtx.Unlock()
  332. r.cursorHidden = true
  333. r.execute(ansi.HideCursor)
  334. }
  335. func (r *standardRenderer) enableMouseCellMotion() {
  336. r.mtx.Lock()
  337. defer r.mtx.Unlock()
  338. r.execute(ansi.EnableMouseCellMotion)
  339. }
  340. func (r *standardRenderer) disableMouseCellMotion() {
  341. r.mtx.Lock()
  342. defer r.mtx.Unlock()
  343. r.execute(ansi.DisableMouseCellMotion)
  344. }
  345. func (r *standardRenderer) enableMouseAllMotion() {
  346. r.mtx.Lock()
  347. defer r.mtx.Unlock()
  348. r.execute(ansi.EnableMouseAllMotion)
  349. }
  350. func (r *standardRenderer) disableMouseAllMotion() {
  351. r.mtx.Lock()
  352. defer r.mtx.Unlock()
  353. r.execute(ansi.DisableMouseAllMotion)
  354. }
  355. func (r *standardRenderer) enableMouseSGRMode() {
  356. r.mtx.Lock()
  357. defer r.mtx.Unlock()
  358. r.execute(ansi.EnableMouseSgrExt)
  359. }
  360. func (r *standardRenderer) disableMouseSGRMode() {
  361. r.mtx.Lock()
  362. defer r.mtx.Unlock()
  363. r.execute(ansi.DisableMouseSgrExt)
  364. }
  365. func (r *standardRenderer) enableBracketedPaste() {
  366. r.mtx.Lock()
  367. defer r.mtx.Unlock()
  368. r.execute(ansi.EnableBracketedPaste)
  369. r.bpActive = true
  370. }
  371. func (r *standardRenderer) disableBracketedPaste() {
  372. r.mtx.Lock()
  373. defer r.mtx.Unlock()
  374. r.execute(ansi.DisableBracketedPaste)
  375. r.bpActive = false
  376. }
  377. func (r *standardRenderer) bracketedPasteActive() bool {
  378. r.mtx.Lock()
  379. defer r.mtx.Unlock()
  380. return r.bpActive
  381. }
  382. func (r *standardRenderer) enableReportFocus() {
  383. r.mtx.Lock()
  384. defer r.mtx.Unlock()
  385. r.execute(ansi.EnableReportFocus)
  386. r.reportingFocus = true
  387. }
  388. func (r *standardRenderer) disableReportFocus() {
  389. r.mtx.Lock()
  390. defer r.mtx.Unlock()
  391. r.execute(ansi.DisableReportFocus)
  392. r.reportingFocus = false
  393. }
  394. func (r *standardRenderer) reportFocus() bool {
  395. r.mtx.Lock()
  396. defer r.mtx.Unlock()
  397. return r.reportingFocus
  398. }
  399. // setWindowTitle sets the terminal window title.
  400. func (r *standardRenderer) setWindowTitle(title string) {
  401. r.execute(ansi.SetWindowTitle(title))
  402. }
  403. // setIgnoredLines specifies lines not to be touched by the standard Bubble Tea
  404. // renderer.
  405. func (r *standardRenderer) setIgnoredLines(from int, to int) {
  406. // Lock if we're going to be clearing some lines since we don't want
  407. // anything jacking our cursor.
  408. if r.linesRendered > 0 {
  409. r.mtx.Lock()
  410. defer r.mtx.Unlock()
  411. }
  412. if r.ignoreLines == nil {
  413. r.ignoreLines = make(map[int]struct{})
  414. }
  415. for i := from; i < to; i++ {
  416. r.ignoreLines[i] = struct{}{}
  417. }
  418. // Erase ignored lines
  419. if r.linesRendered > 0 {
  420. buf := &bytes.Buffer{}
  421. for i := r.linesRendered - 1; i >= 0; i-- {
  422. if _, exists := r.ignoreLines[i]; exists {
  423. buf.WriteString(ansi.EraseEntireLine)
  424. }
  425. buf.WriteString(ansi.CursorUp1)
  426. }
  427. buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered)) // put cursor back
  428. _, _ = r.out.Write(buf.Bytes())
  429. }
  430. }
  431. // clearIgnoredLines returns control of any ignored lines to the standard
  432. // Bubble Tea renderer. That is, any lines previously set to be ignored can be
  433. // rendered to again.
  434. func (r *standardRenderer) clearIgnoredLines() {
  435. r.ignoreLines = nil
  436. }
  437. // insertTop effectively scrolls up. It inserts lines at the top of a given
  438. // area designated to be a scrollable region, pushing everything else down.
  439. // This is roughly how ncurses does it.
  440. //
  441. // To call this function use command ScrollUp().
  442. //
  443. // For this to work renderer.ignoreLines must be set to ignore the scrollable
  444. // region since we are bypassing the normal Bubble Tea renderer here.
  445. //
  446. // Because this method relies on the terminal dimensions, it's only valid for
  447. // full-window applications (generally those that use the alternate screen
  448. // buffer).
  449. //
  450. // This method bypasses the normal rendering buffer and is philosophically
  451. // different than the normal way we approach rendering in Bubble Tea. It's for
  452. // use in high-performance rendering, such as a pager that could potentially
  453. // be rendering very complicated ansi. In cases where the content is simpler
  454. // standard Bubble Tea rendering should suffice.
  455. //
  456. // Deprecated: This option is deprecated and will be removed in a future
  457. // version of this package.
  458. func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary int) {
  459. r.mtx.Lock()
  460. defer r.mtx.Unlock()
  461. buf := &bytes.Buffer{}
  462. buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))
  463. buf.WriteString(ansi.SetCursorPosition(0, topBoundary))
  464. buf.WriteString(ansi.InsertLine(len(lines)))
  465. _, _ = buf.WriteString(strings.Join(lines, "\r\n"))
  466. buf.WriteString(ansi.SetScrollingRegion(0, r.height))
  467. // Move cursor back to where the main rendering routine expects it to be
  468. buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered))
  469. _, _ = r.out.Write(buf.Bytes())
  470. }
  471. // insertBottom effectively scrolls down. It inserts lines at the bottom of
  472. // a given area designated to be a scrollable region, pushing everything else
  473. // up. This is roughly how ncurses does it.
  474. //
  475. // To call this function use the command ScrollDown().
  476. //
  477. // See note in insertTop() for caveats, how this function only makes sense for
  478. // full-window applications, and how it differs from the normal way we do
  479. // rendering in Bubble Tea.
  480. //
  481. // Deprecated: This option is deprecated and will be removed in a future
  482. // version of this package.
  483. func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBoundary int) {
  484. r.mtx.Lock()
  485. defer r.mtx.Unlock()
  486. buf := &bytes.Buffer{}
  487. buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))
  488. buf.WriteString(ansi.SetCursorPosition(0, bottomBoundary))
  489. _, _ = buf.WriteString("\r\n" + strings.Join(lines, "\r\n"))
  490. buf.WriteString(ansi.SetScrollingRegion(0, r.height))
  491. // Move cursor back to where the main rendering routine expects it to be
  492. buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered))
  493. _, _ = r.out.Write(buf.Bytes())
  494. }
  495. // handleMessages handles internal messages for the renderer.
  496. func (r *standardRenderer) handleMessages(msg Msg) {
  497. switch msg := msg.(type) {
  498. case repaintMsg:
  499. // Force a repaint by clearing the render cache as we slide into a
  500. // render.
  501. r.mtx.Lock()
  502. r.repaint()
  503. r.mtx.Unlock()
  504. case WindowSizeMsg:
  505. r.mtx.Lock()
  506. r.width = msg.Width
  507. r.height = msg.Height
  508. r.repaint()
  509. r.mtx.Unlock()
  510. case clearScrollAreaMsg:
  511. r.clearIgnoredLines()
  512. // Force a repaint on the area where the scrollable stuff was in this
  513. // update cycle
  514. r.mtx.Lock()
  515. r.repaint()
  516. r.mtx.Unlock()
  517. case syncScrollAreaMsg:
  518. // Re-render scrolling area
  519. r.clearIgnoredLines()
  520. r.setIgnoredLines(msg.topBoundary, msg.bottomBoundary)
  521. r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)
  522. // Force non-scrolling stuff to repaint in this update cycle
  523. r.mtx.Lock()
  524. r.repaint()
  525. r.mtx.Unlock()
  526. case scrollUpMsg:
  527. r.insertTop(msg.lines, msg.topBoundary, msg.bottomBoundary)
  528. case scrollDownMsg:
  529. r.insertBottom(msg.lines, msg.topBoundary, msg.bottomBoundary)
  530. case printLineMessage:
  531. if !r.altScreenActive {
  532. lines := strings.Split(msg.messageBody, "\n")
  533. r.mtx.Lock()
  534. r.queuedMessageLines = append(r.queuedMessageLines, lines...)
  535. r.repaint()
  536. r.mtx.Unlock()
  537. }
  538. }
  539. }
  540. // HIGH-PERFORMANCE RENDERING STUFF
  541. type syncScrollAreaMsg struct {
  542. lines []string
  543. topBoundary int
  544. bottomBoundary int
  545. }
  546. // SyncScrollArea performs a paint of the entire region designated to be the
  547. // scrollable area. This is required to initialize the scrollable region and
  548. // should also be called on resize (WindowSizeMsg).
  549. //
  550. // For high-performance, scroll-based rendering only.
  551. //
  552. // Deprecated: This option will be removed in a future version of this package.
  553. func SyncScrollArea(lines []string, topBoundary int, bottomBoundary int) Cmd {
  554. return func() Msg {
  555. return syncScrollAreaMsg{
  556. lines: lines,
  557. topBoundary: topBoundary,
  558. bottomBoundary: bottomBoundary,
  559. }
  560. }
  561. }
  562. type clearScrollAreaMsg struct{}
  563. // ClearScrollArea deallocates the scrollable region and returns the control of
  564. // those lines to the main rendering routine.
  565. //
  566. // For high-performance, scroll-based rendering only.
  567. //
  568. // Deprecated: This option will be removed in a future version of this package.
  569. func ClearScrollArea() Msg {
  570. return clearScrollAreaMsg{}
  571. }
  572. type scrollUpMsg struct {
  573. lines []string
  574. topBoundary int
  575. bottomBoundary int
  576. }
  577. // ScrollUp adds lines to the top of the scrollable region, pushing existing
  578. // lines below down. Lines that are pushed out the scrollable region disappear
  579. // from view.
  580. //
  581. // For high-performance, scroll-based rendering only.
  582. //
  583. // Deprecated: This option will be removed in a future version of this package.
  584. func ScrollUp(newLines []string, topBoundary, bottomBoundary int) Cmd {
  585. return func() Msg {
  586. return scrollUpMsg{
  587. lines: newLines,
  588. topBoundary: topBoundary,
  589. bottomBoundary: bottomBoundary,
  590. }
  591. }
  592. }
  593. type scrollDownMsg struct {
  594. lines []string
  595. topBoundary int
  596. bottomBoundary int
  597. }
  598. // ScrollDown adds lines to the bottom of the scrollable region, pushing
  599. // existing lines above up. Lines that are pushed out of the scrollable region
  600. // disappear from view.
  601. //
  602. // For high-performance, scroll-based rendering only.
  603. //
  604. // Deprecated: This option will be removed in a future version of this package.
  605. func ScrollDown(newLines []string, topBoundary, bottomBoundary int) Cmd {
  606. return func() Msg {
  607. return scrollDownMsg{
  608. lines: newLines,
  609. topBoundary: topBoundary,
  610. bottomBoundary: bottomBoundary,
  611. }
  612. }
  613. }
  614. type printLineMessage struct {
  615. messageBody string
  616. }
  617. // Println prints above the Program. This output is unmanaged by the program and
  618. // will persist across renders by the Program.
  619. //
  620. // Unlike fmt.Println (but similar to log.Println) the message will be print on
  621. // its own line.
  622. //
  623. // If the altscreen is active no output will be printed.
  624. func Println(args ...interface{}) Cmd {
  625. return func() Msg {
  626. return printLineMessage{
  627. messageBody: fmt.Sprint(args...),
  628. }
  629. }
  630. }
  631. // Printf prints above the Program. It takes a format template followed by
  632. // values similar to fmt.Printf. This output is unmanaged by the program and
  633. // will persist across renders by the Program.
  634. //
  635. // Unlike fmt.Printf (but similar to log.Printf) the message will be print on
  636. // its own line.
  637. //
  638. // If the altscreen is active no output will be printed.
  639. func Printf(template string, args ...interface{}) Cmd {
  640. return func() Msg {
  641. return printLineMessage{
  642. messageBody: fmt.Sprintf(template, args...),
  643. }
  644. }
  645. }