pub struct SelectablePanel { /* private fields */ }Expand description
One scrollable, mouse-selectable text panel.
Holds the panel’s wrapped-line cache and its current selection. Cheap to
keep around across frames: set_content only
rebuilds the cache when the text or width actually changed, so calling it
unconditionally every frame is fine.
Implementations§
Source§impl SelectablePanel
impl SelectablePanel
Sourcepub fn set_wrap_mode(&mut self, mode: WrapMode)
pub fn set_wrap_mode(&mut self, mode: WrapMode)
Choose how raw lines wider than the panel are laid out — WrapMode::Wrap
(the default) breaks them onto multiple rows, WrapMode::Clip renders
each raw line on exactly one row and clips the overflow. Takes effect on
the next set_content call (which apps make every
frame).
Sourcepub fn set_wrap_marker(&mut self, marker: Option<WrapMarker>)
pub fn set_wrap_marker(&mut self, marker: Option<WrapMarker>)
Enable or disable the end-of-row wrap marker — a dim glyph (a chevron
›, a return arrow ↵, …) drawn in a reserved rightmost column on
every continued wrapped row, so a soft wrap reads differently from a
real line break. Pass Some(WrapMarker { .. }) to enable it (start
from WrapMarker::default and override the glyph/style), or None
to disable it (the default).
Only meaningful in WrapMode::Wrap. When enabled, lines wrap to one
column narrower than the panel to make room for the glyph; because all
selection and copy geometry keys off that reduced wrap width, the
marker column is automatically excluded from highlighting and from
copied text. Takes effect on the next set_content
call (which apps make every frame).
Sourcepub fn wrap_marker(&self) -> Option<WrapMarker>
pub fn wrap_marker(&self) -> Option<WrapMarker>
This panel’s current end-of-row wrap marker, if any.
Sourcepub fn set_content(&mut self, text: Arc<str>, width: usize)
pub fn set_content(&mut self, text: Arc<str>, width: usize)
Set (or update) the panel’s text and the inner width it wraps to, in
columns. A no-op when neither the text (by Arc identity) nor the
width (nor the WrapMode) changed, so it’s safe — and intended — to
call every frame.
Pass a fresh Arc<str> whenever the underlying text changes; identity
(not byte comparison) is what signals “content changed”.
Sourcepub fn has_content(&self) -> bool
pub fn has_content(&self) -> bool
Whether any content has been set yet.
Sourcepub fn total_rows(&self) -> u32
pub fn total_rows(&self) -> u32
The total number of wrapped rows the current content occupies — the scrollable extent, for sizing a scrollbar or clamping a scroll offset.
Sourcepub fn whole_text(&self) -> Option<&str>
pub fn whole_text(&self) -> Option<&str>
The exact, unmodified text the panel was built from (every line, not just what’s scrolled into view) — for a “copy the whole panel” action that needs no selection.
Sourcepub fn begin_selection(&mut self, area: Rect, scroll: u16, point: (u16, u16))
pub fn begin_selection(&mut self, area: Rect, scroll: u16, point: (u16, u16))
Start a selection at terminal point (col, row), given the panel’s
inner area and current scroll (in wrapped rows). Points outside
area clamp to its nearest edge. No-op if there’s no content.
Sourcepub fn extend_selection(&mut self, area: Rect, scroll: u16, point: (u16, u16))
pub fn extend_selection(&mut self, area: Rect, scroll: u16, point: (u16, u16))
Extend the in-progress selection’s live end to terminal point
(col, row). No-op if no selection was started or there’s no content.
Sourcepub fn clear_selection(&mut self)
pub fn clear_selection(&mut self)
Drop the current selection.
Sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Whether there is a selection (even a zero-width one from a bare click).
Sourcepub fn selected_text(&self) -> Option<String>
pub fn selected_text(&self) -> Option<String>
The currently selected text (lines joined with \n), or None when
there’s no selection or it covers nothing but whitespace.
Sourcepub fn copy_selection(&self) -> bool
pub fn copy_selection(&self) -> bool
Copy the current selection to the system clipboard (best-effort:
local clipboard tool, else an OSC 52 escape sequence). Returns true
if there was text to copy.
Sourcepub fn handle_mouse(
&mut self,
event: MouseEvent,
area: Rect,
scroll: u16,
config: &MouseConfig,
) -> MouseAction
pub fn handle_mouse( &mut self, event: MouseEvent, area: Rect, scroll: u16, config: &MouseConfig, ) -> MouseAction
Batteries-included mouse handling for the common “drag to select, release
to copy” workflow. This is entirely opt-in — the lower-level
begin_selection /
extend_selection /
copy_selection methods stay available if you
want to wire events up yourself.
Pass the panel’s inner area, its current scroll (in wrapped rows),
and a MouseConfig describing the behaviour you want. The returned
MouseAction tells you whether anything changed so you can redraw.
Only the left button is handled. A left press inside area starts a
selection; a drag extends it; a release copies it (when
MouseConfig::copy_on_release).
use ratatui::layout::Rect;
use ratatui::crossterm::event::MouseEvent;
use tui_panel_select::{MouseConfig, SelectablePanel};
let cfg = MouseConfig::default();
let _action = panel.handle_mouse(ev, area, scroll, &cfg);Trait Implementations§
Source§impl Default for SelectablePanel
impl Default for SelectablePanel
Source§fn default() -> SelectablePanel
fn default() -> SelectablePanel
Auto Trait Implementations§
impl !Freeze for SelectablePanel
impl !RefUnwindSafe for SelectablePanel
impl !Sync for SelectablePanel
impl Send for SelectablePanel
impl Unpin for SelectablePanel
impl UnsafeUnpin for SelectablePanel
impl UnwindSafe for SelectablePanel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more