tui-panel-select
Panel-scoped mouse text selection and clipboard copy for ratatui apps.
A terminal's own click-drag selection can't be confined to one panel — it spans the full terminal row, sweeping up borders and neighbouring panels. This crate lets your app capture the mouse itself and implement a selection that is:
- Confined to a single panel's rectangle — never spills into other panels or the border.
- Natural "stream" selection — first line from the click column to its end, full lines in between, last line up to the release column (never a rectangular block).
- Stable across resize / rewrap / scroll — selections are stored as logical
(line, column)positions, not stale screen cells, so the same characters stay selected when the panel is resized or rewrapped. - Cheap on huge content — only the rows actually on screen are ever wrapped or painted, so a multi-megabyte body (or one enormous unbroken line) stays responsive.
- Copy that works locally and remotely — on mouse-up the text is copied via
a local clipboard tool (
xclip/xsel/wl-copy/pbcopy/clip.exe) when available, falling back to an OSC 52 escape sequence for SSH/tmux sessions. Pin or disable the mechanism withset_clipboard_modeor theTUI_PANEL_SELECT_CLIPBOARDenvironment variable — see Choosing the clipboard backend.
Quick start — the batteries-included API
use Arc;
use Rect;
use SelectablePanel;
let mut panel = new;
// Each frame, before drawing, give the panel its text and inner width.
panel.set_content;
// The panel's inner text area on screen, and its scroll offset (wrapped rows).
let area = new;
let scroll = 0;
// Mouse down starts a selection; drag extends it; up copies it.
panel.begin_selection; // click at 'h'
panel.extend_selection; // drag to 'o'
assert_eq!;
panel.copy_selection; // -> system clipboard
Rendering each frame:
# use SelectablePanel;
# use Rect;
# let panel = new;
# let area = new;
# let scroll = 0u16;
// 1. Draw the visible wrapped rows into your panel:
let rows = panel.visible_rows;
// 2. Paint the highlight over the selected cells:
for in panel.highlight_cells
Wire these into your event loop: capture the mouse (EnableMouseCapture), then
call begin_selection on MouseEventKind::Down, extend_selection on Drag,
and copy_selection on Up. Call set_content every frame — it only rebuilds
its cache when the text (by Arc identity) or width actually changed.
Opt-in mouse handler
If you'd rather not wire the three events up yourself, handle_mouse does the
common "drag to select, release to copy" flow in one call. Behaviour is
configured per-application via MouseConfig (e.g. copy_on_release), and the
low-level methods above stay available:
# use ;
# use Rect;
# use MouseEvent;
#
Panic-safe terminal guard (feature terminal-guard, on by default)
Enabling panel selection means turning on the terminal's mouse-tracking mode,
which must be undone on exit and on any panic — otherwise the user's shell is
left spewing tracking escape sequences. TerminalGuard centralises that: it
enables mouse capture (and, optionally, the keyboard-enhancement protocol),
wraps the panic hook, and restores everything on drop:
#
Disable the terminal-guard feature (default-features = false) if you only
want the pure selection/wrapping logic without the process-global panic hook.
Line layout: wrap or clip (WrapMode)
By default each raw line wider than the panel is wrapped onto multiple rows.
For panels that show pre-formatted, column-aligned output (e.g. program output
echoed verbatim), call set_wrap_mode(WrapMode::Clip) instead: every raw line
then occupies exactly one screen row and anything past the right edge is
clipped. Selection, copy and scrolling all follow suit (scrolling moves by whole
lines, one row per line).
# use ;
let mut panel = new;
panel.set_wrap_mode;
End of line wrap marker (`WrapMarker)
There is an optional WrapMarker that can be added to a SelectablePanel. The
WrapMarker can be built with the WrapMarkerBuilder like so:
# use ;
let mut panel = new;
panel.set_wrap_mode;
let wrap_marker = builder.glyph.build;
panel.set_wrap_marker;
Vertical scrollbar (feature scrollbar, on by default)
Two panel-agnostic helpers wire up a native-feeling vertical scrollbar for any scrollable content:
scroll_for_track_row(track, row, max_scroll)maps a click or drag anywhere in the scrollbar track to a scroll offset (proportional, clamped to the track), so dragging the thumb — or clicking anywhere along it — jumps there.render_scrollbar(area, buf, total, capacity, start, &style)draws a ratatui scrollbar into a track column, sizing the thumb from the content totals and no-op-ing when everything already fits.
A MultiSelectPanel (which owns its scroll offset) has convenience wrappers
that plumb their own geometry in:
# use ;
# use ;
#
Disable the feature (default-features = false) if you draw your own indicator.
ANSI-coloured content (feature ansi, off by default)
If your panel text contains ANSI escape sequences (e.g. coloured program
output), enable the ansi feature and feed it via set_ansi_content instead of
set_content. Rendered rows (visible_rows) keep their colour, while
selection, copy and all geometry operate on the plain, stripped text — so
you don't have to maintain a second, un-coloured copy yourself:
= { = "0.1", = ["ansi"] }
# use Arc;
# use ;
#
Low-level primitives
If your app already owns its selection state (e.g. multiple simultaneous
selections, keyboard-extended selection, excluding decorative characters from
the copied text), skip SelectablePanel and use the stateless building blocks
directly:
- [
wrapcache::PanelWrap] / [wrapcache::TextPos] — the line/wrap cache and logical positions, with conversions between screen and logical space. - [
selection] — pure functions:point_to_textpos,extract_text,highlight_cells,strip_positions. - [
clipboard::copy_to_clipboard] — local tool + OSC 52 fallback. - [
wrap] — the underlying character-exact line-wrapping helpers.
Choosing the clipboard backend
copy_to_clipboard tries a local clipboard tool first and only falls back to
OSC 52. That order is deliberate: writing an OSC 52 sequence to stdout always
"succeeds" whether or not the terminal acts on it, so there is no failure to
detect and nothing to fall back from — preferring it would silently drop the
copy on the many terminals that ignore it (GNOME Terminal/VTE among them).
When both an X11 display and a Wayland display are reachable, the X11 tools are
tried first. Setting the Wayland selection needs a serial from an input event,
so it needs keyboard focus and therefore a mapped surface, unless the
compositor implements wlr-data-control — which GNOME does not. Under GNOME,
wl-copy consequently maps a real window and the desktop lists it as a running
application for as long as it owns the selection, flashing an entry into the
app bar on every copy. An X11 selection owner needs no mapped window, and
XWayland's clipboard bridge still propagates the selection to Wayland clients.
Override the choice with TUI_PANEL_SELECT_CLIPBOARD (auto, x11,
wayland, osc52, none) or, from code, set_clipboard_mode:
use ;
// Compositor with XWayland but no clipboard bridging? Force the native path.
set_clipboard_mode;
Test suites should disable it. Copying reaches the real system clipboard, so any test that exercises a copy path will overwrite whatever the developer had copied — and each spawned helper must be reaped or it lingers as an unkillable zombie. Disable it once during test setup:
use ;
set_clipboard_mode;
License
MIT