github anomalyco/opentui v0.7.0
Release v0.7.0

3 hours ago

Before 0.7.0, typing one character in a 10,000-line Textarea laid out all 10,000 lines before the key call returned, and the Yoga measure of the next frame walked them again. Typing and pasting slowed down with the size of the document, and word wrap paid the most. In 0.7.0, an edit only records that the view must scroll to the cursor. The layout runs once, at the next read of the viewport or the visual cursor, or at the next frame. It compares the text with the text of the last layout and lays out only the lines around the change. An insert, a delete, an undo, or a join lays out one line. Enter lays out two lines, and a 100-line paste lays out 101.

A read that needs the layout still pays for it. visualCursor lays out the text when an edit is pending. cursorOffset reads the logical cursor and does not lay out the text. To check after each key whether the cursor is at the start of the text, compare cursorOffset:

const atStart = input.cursorOffset === 0 // not input.visualCursor.offset === 0

Before 0.7.0, Renderable found the hooks of each node by reflection. It scanned the property descriptors of every new instance two times. For 10,000 rows of a Text subclass that overrides renderSelf, this took 20,000 scans and 160,000 descriptor reads. Now a node reads its hooks from its class once, at construction. Prototype methods with super, constructor options, and assignments to renderBefore, renderAfter, onLifecyclePass, and onSizeChange work as before.

Three kinds of code must change. First, a hook that you add by reflection does nothing until you call node.refreshHooks(). Examples are an instance class field, Object.defineProperty(), or an assignment to the renderSelf or onUpdate of one node. Second, a patch or spy on a prototype method after nodes exist does not reach those nodes, and untyped JSX props such as onUpdate and onResize have no effect. Third, renderBefore, renderAfter, and onLifecyclePass are now accessors. A class field that overrides one of them is a TypeScript error, so assign the hook in the constructor. An onLifecyclePass class field still runs, because attaching the node registers it.

import { BoxRenderable, RGBA, type BoxOptions, type RenderContext } from "@opentui/core"

class MarkedBox extends BoxRenderable {
  constructor(ctx: RenderContext, options: BoxOptions) {
    super(ctx, options)
    this.renderAfter = (buffer) => {
      buffer.drawText("*", this.screenX, this.screenY, RGBA.fromInts(255, 0, 0))
    }
  }
}

A class that used defineNativeIntegration() must now declare the description directly. Without the type annotation, kind does not keep its literal type:

import { Renderable, type NativeRenderableIntegration } from "@opentui/core"

class Marker extends Renderable {
  static override readonly nativeIntegration: NativeRenderableIntegration = {
    kind: "custom",
    body: "host",
  }
}

The nativeSceneWorkBudget renderer option let a frame yield to the event loop during native preparation. OpenTUI, its SSH package, and OpenCode did not set it. On a 10,000-line log, it cut the longest native slice by about 0.15 ms but used 27% more native time. With one mutation per event-loop turn, each yield started preparation again, until the frame failed with LayoutLimit. A renderer that scrolled or streamed under a budget showed no frames. Now preparation and paint finish in one call. A C binding built against the old header passes every argument after the removed max_work_items one slot off, so the ABI version changes. To update a binding, generate it again from the new opentui.h. Then remove the max_work_items argument and any OT_SCENE_FRAME_YIELD case, and use the 224-byte ot_scene_stats record. The work_budget of ot_session_pump is a different budget and does not change.

0.7.0 also adds a native input parser. The TypeScript parser parses each framed unit again with regular expressions, and its structure caused bugs that are hard to fix in place. These bugs include lost Escape-prefixed events, empty Alt key names, wrong Alt keys for non-ASCII characters, wheel releases that ended drags, and negative X10 coordinates. The native parser is a bounded state machine that does not allocate, and it handles all of these cases. It is off by default while timing and buffering edge cases are tested. To try it, set OTUI_NATIVE_INPUT_PARSER=1 or create the renderer with the option:

const renderer = await createCliRenderer({ experimental_nativeInput: true })

With OTUI_NATIVE_INPUT_SHADOW=1, the renderer also runs the other parser. It writes a [stdin-shadow] warning for each event that the two parsers report differently. The warnings contain your input, so protect them as you would a stdin log. To report a parser bug, include a log from OTUI_STDIN_LOG. The native parser is bounded, so it ignores stdinParserMaxBufferBytes.

Known issue: when the terminal hangs up, as when an SSH connection closes, the renderer prints Error disabling raw mode and Native scene shutdown failed errors with stack traces to stderr. The process still exits with code 0.

Breaking changes

  • core: A renderer created with nativeSceneWorkBudget throws at construction, so remove the option. NativeSceneFrame.Yield and the maxWorkItems argument of FFIRenderLib.sceneFrameStep() are removed. See How Core uses native. (#1719)
  • core: NativeSceneFrameRequest.geometryRevision is removed. See How Core uses native. (#1760)
  • native: ot_scene_frame_step_with_geometry no longer has its max_work_items parameter, OT_SCENE_FRAME_YIELD is removed, and OT_CONTEXT_ABI_VERSION is 2. Build bindings again from the new opentui.h, because records with version 1 fail with OT_UNSUPPORTED_VERSION. See C. (#1719)
  • native: ot_scene_stats changes from 64 to 224 bytes, and ot_scene_get_stats returns OT_INVALID_ARGUMENT for the 64-byte record. Generate the record again from the new header. See Frames and output. (#1722)
  • native: In the Zig module, Context.sceneFrameStepWorkBudgeted is removed, sceneFrameStepWithRecording no longer takes max_work_items, and replaceOwnedStyledText no longer takes a SyntaxStyle. OwnedStyledChunk now holds a style value, and the module no longer exports 12 extra span-feed symbols. See Zig. (#1719, #1768, #1784)
  • core: Renderable.defineNativeIntegration() and NativeRenderableIntegration.construction are removed. Declare static override readonly nativeIntegration: NativeRenderableIntegration = { ... } without construction. See Custom renderables. (#1754)
  • core: A node reads its hooks from its class at construction, so a hook added by reflection runs only after refreshHooks(). A class field that overrides renderBefore, renderAfter, or onLifecyclePass is a TypeScript error (TS2610), so assign it in the constructor. See Custom renderables. (#1754)
  • core: The protected TextBufferRenderable._textBufferSyntaxStyle field is removed. Styled text keeps its styles in the text buffer, so remove code that reads the field. See Code. (#1775)
  • core: translateX and translateY throw RangeError for values outside the 32-bit float range of the layout setters. Before, such values did not paint. See Layout. (#1682)

Added

  • core: The experimental_nativeInput renderer option, or OTUI_NATIVE_INPUT_PARSER, parses stdin with a new native parser. OTUI_NATIVE_INPUT_SHADOW runs both parsers and logs each event that they report differently. See Environment variables. (#1592, #1787)
  • native: ot_session_input_feed() parses terminal input into caller-owned ot_input_event records, a payload buffer, and an ot_input_drain summary, and it does not allocate. ot_session_input_expect() sets the replies that the parser expects, and ot_session_input_reset() clears the parser state. FFIRenderLib.sessionInputFeed(), sessionInputExpect(), and sessionInputReset() wrap them. See Host I/O and time. (#1592)
  • core: getNativeStats().frameWork, also in getStats(), counts the work of the last presented scene frame, such as Yoga solves, view preparations, and painted cells. The test renderer option nativeSceneFullFrame makes each frame rebuild its work lists, so tests can compare the counts. See Rendering diagnostics. (#1722)
  • native: ot_scene_stats reports the work of the last presented scene frame. ot_scene_set_debug_flags() with OT_SCENE_DEBUG_FULL_FRAME forces the full frame path, and full_path_reasons uses the OT_SCENE_FULL_PATH_* bits. See Frames and output. (#1722)
  • core: The protected _paintWidth and _paintHeight of Renderable give the size that a renderSelf() paints, clamped to the cell range that drawing accepts. See Custom renderables. (#1682)

Changed

  • core: An edit in a text view or editor lays out only the lines that it changed, once, at the next read or frame. Typing one character in a 10,000-line Textarea lays out 1 line, not 10,000. Typing or pasting over a selection scrolls once instead of twice. See Editing buffers and views. (#1758, #1751, #1747)
  • core: A Yoga measure of text at the render width reads the render layout, and at a different width it measures again only the edited lines. One edit in a 10,000-line word-wrapped editor measures 0 lines, not 10,000. See Layout. (#1772)
  • core: Text writes that cannot change the size no longer cause a Yoga solve. Examples are the same bytes, colors, styles, viewport changes, scrolls, textAlign, truncate, tabIndicator, tabIndicatorColor, and the editor scroll margin. See Rendering pipeline. (#1755, #1767, #1774)
  • core: A ScrollBox with viewport culling refreshes its rows only after a layout change or a row change, and an idle ScrollBox requests no update hook. A 10,000-row list does 0 row refreshes on idle and scroll frames, not 10,000. See ScrollBox. (#1756, #1761)
  • core: A frame records one hit rectangle per member and no longer writes a screen-sized hit grid. A hit test walks the members of the presented frame from the top. See Interaction, focus, and selection. (#1771)
  • core: With wrapMode: "none", a line that is scrolled right skips the text to the left of the viewport by chunk and cluster. An idle editor with one 256 KiB line no longer walks the line on each frame. See Textarea. (#1777)
  • core: After style and paint writes, reads of x, y, width, and height use the cached layout without a native call or a flush. getLayout() and getComputedLayout() call native code on every call, and a rejected staged write shows at the next flushing call. See Layout. (#1760)
  • core: Only native code checks staged style writes. On Node, a box.width write with a flush takes 111 ns instead of 149 ns. A paint value that only native code rejects, such as an opacity above 1 passed to setNativeScenePaint, fails at the next flush. See Rendering pipeline. (#1770)
  • core: A text buffer keeps its styled-text styles by value, and a Code creates two native objects instead of three. A highlight update no longer copies the style table or scans the object table. See Code. (#1768, #1775)
  • core: A wheel step scrolls only the innermost scroller under the pointer that can still move in its direction. A scroller at its edge, or one that cannot scroll that axis, gives the step to the next scroller out, as browsers do. Before, one step moved every scroller on its path, so the text of a Textarea inside a ScrollBox moved twice as far, and the Textarea moved away from the pointer. The event still bubbles, so the split-pane sync of a Diff and the onMouseScroll handler of an ancestor still receive it. Text, Code, and Textarea treat Shift+wheel as sideways, and an editor's sideways step stops at its widest line. See ScrollBox. (#1761)
  • core: 896 symbols that terminals draw in one cell take one cell in every width method, also with VS15. Examples are ‼, ⁉, ☢, ⚛, 🖥, 🗑, and the Mahjong, domino, and playing-card tiles. Every measured terminal advances one cell for at least 892 of the 896. Before, they took two cells, so text after them moved one column right, the cursor landed past the text, and old cells stayed on screen. With VS16, as in ❤️, they take two cells in the grapheme methods and one cell in wcwidth, so under tmux and screen ❤️ is still one cell too narrow, until OpenTUI selects the width method correctly. In classic Windows conhost, which gives VS15 its own cell, 53 of these symbols followed by VS15 are now one cell off. Some terminals draw a glyph wider than the one cell that they advance, so it covers the next character, extends into a blank cell, or is cut off; this comes from the terminal, not from the layout. See Text and terminal cells. (#1762)
  • core: detectLinks() styles bare URLs with markup.link.label from the new syntaxStyle context field and keeps their other attributes. Bare URLs in Markdown prose now look like link labels. See Markdown. (#1765)
  • core: An audio stream whose decoder reads past maxProbeBytes rejects with the decoder error. Before, it could play a truncated prefix and end without an error. See Streaming audio. (#1759)
  • core: The embedded terminal uses a newer Ghostty and no longer answers Kitty graphics or glyph protocol queries, and it ignores OSC 72 drag-and-drop commands. DECSTR, CSI g, DECRQM, and other controls now behave as in xterm. XTGETTCAP answers from Ghostty's terminfo, which claims some features that the embedded terminal does not draw. See Embedded terminal. (#1714)
  • native: Highlight reads mark styled-text styles with the new OT_STYLE_RUN_FLAG bit, and user highlights reject such style ids. Styled text no longer registers chunkN styles in the attached SyntaxStyle, and ot_text_buffer_clear without reset keeps styled highlights but drops their links. The styled-text batch accepts buffers that have an attached style. See Editing buffers and views. (#1768)
  • native: ot_editor_view_get_viewport, ot_editor_view_set_scroll_margin, and a local ot_editor_view_select can lay out text and return OT_OUT_OF_MEMORY. This occurs only while a scroll to the cursor is pending. See C. (#1751)
  • native: ot_scene_set_paint rejects a translation that is not finite or exceeds FLT_MAX, ot_scene_set_style stores finite margin, padding, position, and gap values beyond ±1e30 as ±1e30, and OT_LAYOUT_PAINT clamps an axis outside the 32-bit cell range. See C. (#1682)

Fixed

  • core: Text with a fractional flex size rounds like a box and stays inside the rounded box of its parent. Before, Yoga rounded the right edge of text up but rounded containers to the nearest cell, so the last glyph of each wrapped line could draw under the next column or under a ScrollBox scrollbar. Some layouts move by one cell. Text at a half-cell offset, such as centered text with an odd leftover, moves right or down to the position of a box of the same size. A box centered inside another centered box, with odd leftovers at both levels, can move left or up. Update snapshots of such layouts. The public yoga.ts API keeps the upstream rounding. See Layout. (#1728)
  • core: A node with layout outside 32-bit cell coordinates, such as left = 2 ** 31 - 1 or margin = 1e15, no longer makes every later frame fail with InvalidArgument. Native code clamps only the geometry that it paints, clips, and hit-tests. See Layout. (#1682)
  • core: With the wcwidth width method, drawText and encodeUnicode give a multi-code-point cluster, such as 👩‍💻, the same width as the layout. Before, text after the cluster landed two cells to the left. Clusters wider than four cells, such as 👨‍👩‍👧, are still drawn too narrow. See Text and terminal cells. (#1773)
  • core: A ScrollBar position is never negative. Before, a ScrollBox with content that fit the viewport could report a scrollTop or scrollLeft such as -7 while its content did not move. A ScrollBar's onChange value and scrollPosition now agree, and with sticky bottom over short content, a sideways scroll no longer stops the box from following new content. See ScrollBar. (#1769)
  • core: destroy() leaves raw mode after the Session restores the terminal, and resume() enters raw mode before mouse reporting starts again. Before, mouse reports could show as text such as ^[[<35;30;5M at the shell prompt. suspend() still leaves raw mode at once, so mouse reports can still echo there. See Lifecycle and cleanup. (#1720)
  • core: Audio.playStream() with an MP3 whose first chunk is larger than the decoder probe limit always rejects with { action: "decoder", errorCode: -3 }. Before, it sometimes rejected with { action: "write", status: -1 }, depending on thread timing. See Streaming audio. (#1759)
  • core: An Input with wrap off limits its horizontal scroll by the widest line, not the widest text piece, so it no longer jumps sideways on resize. A typed character lands on the same side of zero-width text as in the same text loaded in one piece. See Input. (#1747)
  • core: Undo and redo in a Textarea scroll to the cursor in the restored text. Before, they used the layout from before the undo and could leave blank rows below the text. See Textarea. (#1751)
  • core: Undo and redo in a Textarea or Input emit content-changed, so onContentChange and line-info-change run. Before, an app that synced on content changes kept the text from before the undo, and the line-number gutter kept its old width. See Textarea. (#1790)
  • core: A node that is resized and then given an onLifecyclePass before its first frame gets its first onResize. Before, a buffered node kept its old buffer size and clipped its drawing. See Custom renderables. (#1754)
  • core: Two text buffers that share a SyntaxStyle keep their own styled-text colors. Before, each update of one buffer changed the colors of the other. See Editing buffers and views. (#1768)
  • core: In an EmbeddedTerminalRenderable, RIS (ESC c) and DECSTR (CSI ! p) reset the palette that a program set with OSC 4, and keep the OSC 10, 11, and 12 colors. Tools such as kitten icat now use their text fallback and do not show blank cells. See Embedded terminal. (#1714)

API changes and documentation: https://opentui.com/docs/releases/0.7.0

What's Changed

  • native: add native input parser by @simonklee in #1592
  • ci(release): format release notes with bun run fmt by @simonklee in #1748
  • native: update vendored Ghostty so RIS and DECSTR reset the palette by @simonklee in #1714
  • native: count styled-text, highlight, and streaming work in tests by @simonklee in #1744
  • core: leave raw mode after the terminal is restored on destroy and resume by @simonklee in #1720
  • native: delete the scene work budget, asynchronous paint, and YIELD by @simonklee in #1719
  • native: count scene frame work and add a full-frame debug flag by @simonklee in #1722
  • native: keep measured text inside its parent's rounded box by @simonklee in #1728
  • native: keep line facts in rope metrics and delete the marker cache by @simonklee in #1747
  • web: refresh two documentation visuals for #1728's rounding by @simonklee in #1752
  • native: clamp out-of-range layout instead of failing every frame by @simonklee in #1682
  • native: add a twin-Session scene parity harness and a scene-frame bench by @simonklee in #1753
  • native: lay out the editor at the next read or frame, not on each edit by @simonklee in #1751
  • core: declare paint and lifecycle hooks per class by @simonklee in #1754
  • native: keep the Yoga measure on text writes that cannot change its size by @simonklee in #1755
  • native: refresh viewport children only after a layout change by @simonklee in #1756
  • core: report the decoder error when a write follows decoder failure by @simonklee in #1759
  • core: idle ScrollBox updates and give each wheel step one scroller by @simonklee in #1761
  • core: stop discarding cached layout on style and paint writes by @simonklee in #1760
  • core: style bare URLs like link labels by @Tarquinen in #1765
  • native: re-lay out only the lines an edit changed by @simonklee in #1758
  • native: keep the Yoga measure when a text viewport changes by @simonklee in #1767
  • native: measure neutral symbols in one cell, as terminals draw them by @simonklee in #1762
  • native: hit-test the presented member list by @simonklee in #1771
  • core: keep a ScrollBar position at 0 when the content fits by @simonklee in #1769
  • native: intern styled-text styles by value in the buffer by @simonklee in #1768
  • core: drop the copied scene style validator by @simonklee in #1770
  • native: draw wcwidth clusters at their summed width by @simonklee in #1773
  • native: keep the Yoga measure on text options it does not read by @simonklee in #1774
  • native: measure from the per-line layout by @simonklee in #1772
  • core: drop Code's private SyntaxStyle by @simonklee in #1775
  • native: maintain the editor cursor once per frame by @simonklee in #1776
  • native: skip text left of the viewport by chunk and cluster by @simonklee in #1777
  • core: keep modifyOtherKeys letters as sent in native input by @simonklee in #1787
  • native: compile the placement-sensitive benches as their own unit by @simonklee in #1784
  • native: keep bench results alive by @simonklee in #1780
  • native: emit content-changed on undo and redo by @simonklee in #1790

New Contributors

Full Changelog: v0.6.8...v0.7.0

Don't miss a new opentui release

NewReleases is sending notifications on new releases.