Skip to main content

Paginator

Struct Paginator 

Source
pub struct Paginator { /* private fields */ }
Expand description

Which page of several is showing.

use rich_interact::headless::{self, Script};
use rich_interact::{Paginator, PaginatorStyle};

// Embedded: state and an indicator line.
let mut pages = Paginator::new(3);
assert!(pages.next_page());
assert_eq!(pages.page(), 1);
assert_eq!(pages.indicator_text(80), "○ ● ○");
assert_eq!(pages.style(PaginatorStyle::Fraction).indicator_text(80), "2/3");

// Standalone: a list shown a page at a time; Enter returns the page.
let letters = ["a", "b", "c", "d", "e"];
let paginator = Paginator::over("Letters", letters, 2);
let (outcome, record) = headless::run(paginator, Script::new().keys("right enter"), 40, 8);
assert_eq!(outcome.unwrap().value(), Some(1));
assert_eq!(record.last_frame(), "? Letters › page 2 of 3");

Implementations§

Source§

impl Paginator

Source

pub fn new(pages: usize) -> Paginator

A paginator over pages pages (at least one), on the first.

Source

pub fn over<I, S>( title: impl Into<String>, items: I, per_page: usize, ) -> Paginator
where I: IntoIterator<Item = S>, S: Into<String>,

A paginator over items, per_page at a time, titled title: the standalone component shows the current page’s items above the indicator.

Source

pub fn style(self, style: PaginatorStyle) -> Self

Dots or a fraction (default: dots).

Source

pub fn dots( self, active: impl Into<String>, inactive: impl Into<String>, ) -> Self

The dots for the current page and the others (default ● and ○).

Source

pub fn start_at(self, page: usize) -> Self

Start on page (0-based; past the last means the last).

Source

pub fn theme(self, theme: Theme) -> Self

The current dot is drawn in the theme’s checked_style, the others and the fraction’s count in hint.

Source

pub fn with_mouse(self, on: bool) -> Self

Report the mouse: a click on a dot goes to its page, and the wheel moves a page.

Source

pub fn rebind(self, action: &str, keys: impl IntoIterator<Item = Key>) -> Self

Make keys do action on this paginator only (see paginator_keymap); no keys unbinds it.

Source

pub fn pages(&self) -> usize

The number of pages.

Source

pub fn page(&self) -> usize

The current page, from 0.

Source

pub fn set_pages(&mut self, pages: usize)

Change the number of pages (at least one), keeping the current page if it is still there.

Source

pub fn set_page(&mut self, page: usize)

Go to page; past the last means the last.

Source

pub fn next_page(&mut self) -> bool

Move to the next page; false on the last.

Source

pub fn previous_page(&mut self) -> bool

Move to the previous page; false on the first.

Source

pub fn on_first(&self) -> bool

Source

pub fn on_last(&self) -> bool

Source

pub fn bounds(&self, len: usize, per_page: usize) -> Range<usize> ⓘ

The range of a list of len items on the current page, at per_page a page: what to slice for it.

Source

pub fn keys(&self) -> Keymap

The keys, with this paginator’s rebinding.

Source

pub fn handle_key(&mut self, key: Key) -> bool

Move for key if it is one of the moving keys (next, previous, first, last page): true when it was, whether or not the page changed. For a container that embeds a paginator.

Source

pub fn indicator(&self, width: usize) -> Vec<Segment>

The indicator line, at most width columns.

Source

pub fn indicator_text(&self, width: usize) -> String

The indicator as plain text: what a screen reader or a log reads.

Source

pub fn page_at(&self, width: usize, column: usize) -> Option<usize>

The page a click at column of the indicator (drawn at width) lands on: a dot’s page, or None off the dots.

Trait Implementations§

Source§

impl Component for Paginator

Source§

fn prompt( &mut self, io: &mut dyn LineIo, ) -> Result<Option<usize>, NotInteractive>

Without a terminal: every item, then the page asked for by number.

Source§

type Output = usize

Source§

fn handle(&mut self, event: &Event, context: &Context<'_>) -> Flow<usize>

Handle one event. Ctrl+C never arrives here: the event loop ends the component with Outcome::Interrupted.
Source§

fn render(&self, context: &Context<'_>) -> View

Render the current state.
Source§

fn keymap(&self) -> Keymap

The bindings this component has right now, for a help overlay, a shortcut list or status-bar hints to read (see keymap). A container’s include its focused child’s, before its own. The default declares none.
Source§

fn mouse(&self) -> bool

Whether the component wants mouse events (#476): clicks, drags and the wheel. Off by default, since reporting the mouse takes text selection away from the terminal; a component turns it on when its caller opts in, and run then enables mouse reporting for the session.
Source§

fn default_value(&self) -> Option<usize>

The value to return when the terminal is not interactive and the policy asks for defaults. None: there is none.
Source§

fn start(&mut self, context: &Context<'_>) -> Flow<Self::Output>

Called once, before the component is first painted, with the terminal’s size: for work that needs it, such as rendering content at the width. Returning Done or Cancel finishes the component without waiting for an event. The default does nothing.
Source§

fn tick(&self) -> Option<Duration>

How often the component wants Event::Tick, for a spinner or a countdown. None (the default): never.
Source§

fn focusable(&self) -> bool

Whether the component takes focus in a container: Tab stops on it and keys go to it. The default: yes. A label, or a component that has finished, says no.
Source§

fn focus_step(&mut self, forward: bool) -> bool

Move focus to the next (forward) or previous focus stop inside this component, for Tab and Shift+Tab. false when there is none after the current one, so the container moves on to its next child. The default, for a component that is one stop: false.
Source§

fn focus_enter(&mut self, forward: bool) -> bool

Take focus from outside: at the first stop inside when forward, at the last otherwise. false when nothing in it can take focus. The default: whether the component is focusable.

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<C> ComponentExt for C
where C: Component,

Source§

fn map<'a, M>( self, done: impl FnMut(Self::Output) -> Flow<M> + 'a, ) -> Map<'a, Self, M>
where Self: 'a,

Turn this component’s answer into M’s flow: Flow::Done(m) to finish the whole composition with m, Flow::Continue to carry on (the component stays, showing its answer, and focus moves past it). Cancelling cancels the composition unless on_cancel says otherwise.
Source§

fn with_overlays<'a>(self) -> Overlays<'a, Self::Output>
where Self: 'a, Self::Output: 'a,

Wrap this component in Overlays: a command palette, help and a shortcut sheet read from its keymap, and room for a status bar and breadcrumbs.
Source§

fn boxed<'a>(self) -> Child<'a, Self::Output>
where Self: 'a,

Box this component as a Child.
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.