Skip to main content

SelectablePanel

Struct SelectablePanel 

Source
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

Source

pub fn new() -> Self

A panel with no content and no selection.

Source

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).

Source

pub fn wrap_mode(&self) -> WrapMode

This panel’s current WrapMode.

Source

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).

Source

pub fn wrap_marker(&self) -> Option<WrapMarker>

This panel’s current end-of-row wrap marker, if any.

Source

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”.

Source

pub fn has_content(&self) -> bool

Whether any content has been set yet.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn clear_selection(&mut self)

Drop the current selection.

Source

pub fn has_selection(&self) -> bool

Whether there is a selection (even a zero-width one from a bare click).

Source

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.

Source

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.

Source

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);
Source

pub fn visible_rows(&self, scroll: u16, height: u16) -> Vec<Line<'static>>

The visible wrapped rows for a height-row window starting at wrapped-row scroll — ready to render. Only the rows actually on screen are wrapped, regardless of total content size.

Source

pub fn highlight_cells(&self, area: Rect, scroll: u16) -> Vec<(u16, u16, u16)>

The selection’s on-screen cells to highlight, as (row, col_from, col_to_exclusive) in absolute terminal coordinates, bounded to the visible window. Empty when there’s no selection or it’s off-screen.

Trait Implementations§

Source§

impl Default for SelectablePanel

Source§

fn default() -> SelectablePanel

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.