Toc

pub struct Toc<'a> { /* private fields */ }
Expand description

Table of Contents widget for markdown navigation.

Shows document headings in either compact (lines) or expanded (text) mode. Supports hover interactions and click-to-scroll navigation.

This is a UI-only widget that receives &TocState for state access. State mutations happen through TocState methods, not here.

§Example

use ratatui_toolkit::markdown_widget::extensions::toc::{Toc, TocConfig};
use ratatui_toolkit::markdown_widget::state::toc_state::TocState;

let mut toc_state = TocState::new();
let toc = Toc::new(&toc_state)
    .config(TocConfig::default())
    .expanded(true);

Implementations§

Source§

impl<'a> Toc<'a>

Source

pub fn hovered(self, _index: Option<usize>) -> Self

Set the hovered item index.

§Arguments
  • index - The index of the hovered heading, or None.
§Returns

Self for method chaining.

Source§

impl<'a> Toc<'a>

Source

pub fn new(toc_state: &'a TocState) -> Self

Create a new TOC widget from TocState.

The Toc widget is a UI-only component that receives state via reference. State mutations happen through TocState methods, not the Toc widget.

§Arguments
  • toc_state - Reference to the TocState containing entries, scroll, and hover state.
§Returns

A new Toc instance ready for rendering.

§Example
use ratatui_toolkit::markdown_widget::extensions::toc::Toc;
use ratatui_toolkit::markdown_widget::state::toc_state::TocState;

let toc_state = TocState::new();
let toc = Toc::new(&toc_state);
Source

pub fn expanded(self, expanded: bool) -> Self

Set whether the TOC is expanded.

§Arguments
  • expanded - True for expanded mode (full text), false for compact mode (lines).
§Returns

Self for method chaining.

Source

pub fn config(self, config: TocConfig) -> Self

Set the TOC configuration.

§Arguments
  • config - The TOC configuration.
§Returns

Self for method chaining.

Source§

impl<'a> Toc<'a>

Source

pub fn viewport( self, _scroll_offset: usize, _viewport_height: usize, _total_lines: usize, ) -> Self

Set the current viewport information.

§Arguments
  • scroll_offset - Current scroll offset.
  • viewport_height - Height of the visible viewport.
  • total_lines - Total number of lines in the document.
§Returns

Self for method chaining.

Source

pub fn toc_scroll(self, _offset: usize) -> Self

Set the TOC scroll offset (for scrolling within the TOC list).

§Arguments
  • offset - The scroll offset for the TOC list.
§Returns

Self for method chaining.

Source§

impl<'a> Toc<'a>

Source

pub fn count_headings(content: &str) -> usize

Count the number of headings in markdown content.

This is a static method useful for calculating dynamic TOC height without constructing a full Toc widget.

§Arguments
  • content - The markdown content to scan.
§Returns

The number of headings found.

Source§

impl<'a> Toc<'a>

Source

pub fn entry_count(&self) -> usize

Get the number of entries in the TOC.

Delegates to the underlying TocState.

Source

pub fn entries(&self) -> &[TocEntry]

Get all entries.

Delegates to the underlying TocState.

Source

pub fn click_to_line(&self, entry_index: usize) -> Option<usize>

Get the target line number for a clicked entry.

§Arguments
  • entry_index - The index of the clicked entry.
§Returns

The line number to scroll to, or None if the index is invalid.

Source

pub fn get_entry(&self, index: usize) -> Option<&TocEntry>

Get the entry at a given index.

§Arguments
  • index - The entry index.
§Returns

The entry, or None if the index is invalid.

Source§

impl<'a> Toc<'a>

Source

pub fn entry_at_position(&self, x: u16, y: u16, area: Rect) -> Option<usize>

Find the entry index at a given screen position.

§Arguments
  • x - Screen X coordinate.
  • y - Screen Y coordinate.
  • area - The area the TOC is rendered in.
§Returns

The entry index at that position, or None if no entry is there.

Source§

impl<'a> Toc<'a>

Source

pub fn required_compact_height( content: &str, line_spacing: u8, show_border: bool, ) -> u16

Calculate the required height for compact mode.

With Braille markers, we have 4 vertical dots per cell. Height = ceil(entries * spacing / 4) + border_height.

§Arguments
  • content - The markdown content to scan.
  • line_spacing - Spacing between lines in dot units.
  • show_border - Whether the border is shown.
§Returns

The required height in rows.

Source§

impl<'a> Toc<'a>

Source

pub fn required_expanded_width(content: &str, show_border: bool) -> u16

Calculate the required width to display all headings without truncation.

Takes into account:

  • Indentation based on heading level (2 chars per level)
  • Actual text width using Unicode width
  • Padding (left and right)
  • Border if enabled
§Arguments
  • content - The markdown content to scan.
  • show_border - Whether the border is shown.
§Returns

The required width in columns.

Source§

impl<'a> Toc<'a>

Source

pub fn required_height(content: &str, show_border: bool) -> u16

Calculate the required height for expanded mode.

Accounts for border (2 rows) and one row per entry.

§Arguments
  • content - The markdown content to scan.
  • show_border - Whether the border is shown.
§Returns

The required height in rows.

Trait Implementations§

Source§

impl<'a> Debug for Toc<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Widget for Toc<'a>

Source§

fn render(self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.

Auto Trait Implementations§

§

impl<'a> Freeze for Toc<'a>

§

impl<'a> RefUnwindSafe for Toc<'a>

§

impl<'a> Send for Toc<'a>

§

impl<'a> Sync for Toc<'a>

§

impl<'a> Unpin for Toc<'a>

§

impl<'a> UnwindSafe for Toc<'a>

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more