Struct rat_ftable::FTable

source ·
pub struct FTable<'a, Selection> { /* private fields */ }
Expand description

FTable widget.

Can be used like a ratatui::Table, but the benefits only show if you use FTable::data to set the table data.

See FTable::data for a sample.

Implementations§

source§

impl<'a, Selection> FTable<'a, Selection>

source

pub fn new<R, C>(rows: R, widths: C) -> Self
where R: IntoIterator, R::Item: Into<Row<'a>>, C: IntoIterator, C::Item: Into<Constraint>, Selection: Default,

Create a new FTable with preformatted data. For compatibility with ratatui.

Use of FTable::data is preferred.

source

pub fn rows<T>(self, rows: T) -> Self
where T: IntoIterator<Item = Row<'a>>,

Set preformatted row-data. For compatibility with ratatui.

Use of FTable::data is preferred.

source

pub fn data(self, data: &'a dyn TableData<'a>) -> Self

Set a reference to the TableData facade to your data.

The way to go is to define a small struct that contains just a reference to your data. Then implement TableData for this struct.

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::Style;
use ratatui::text::Span;
use ratatui::widgets::Widget;
use rat_ftable::{FTable, FTableState, TableData};


struct Data1<'a>(&'a [SampleRow]);

impl<'a> TableData<'a> for Data1<'a> {
    // returns (cols, rows)
    fn size(&self) -> (usize, usize) {
        (5, self.0.len())
    }

    fn row_height(&self, row: usize) -> u16 {
        // to some calculations ...
        1
    }

    fn row_style(&self, row: usize) -> Style {
        // to some calculations ...
        Style::default()
    }

    fn render_cell(&self, column: usize, row: usize, area: Rect, buf: &mut Buffer) {
        if let Some(data) = self.0.get(row) {
            let rend = match column {
                0 => Span::from("column1"),
                1 => Span::from("column2"),
                2 => Span::from("column3"),
                _ => return
            };
            rend.render(area, buf);
        }
    }
}

// When you are creating the table widget you hand over a reference
// to the facade struct.

let my_data_somewhere_else = vec![SampleRow;999999];
let mut table_state_somewhere_else = FTableState::default();

// ...

let tabledata1 = Data1(&my_data_somewhere_else);
let table1 = FTable::default().data(&tabledata1);
table1.render(area, buf, &mut table_state_somewhere_else);
source

pub fn iter(self, data: &'a mut dyn TableDataIter<'a>) -> Self

Alternative representation for the data as a kind of Iterator. It uses interior iteration, which fits quite nice for this and avoids handing out lifetime bound results of the actual iterator. Which is a bit nightmarish to get right.

Caution: If you can’t give the number of rows, the table will iterate over all the data.

use std::iter::{Enumerate};
use std::slice::Iter;
use format_num_pattern::NumberFormat;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Rect};
use ratatui::prelude::Color;
use ratatui::style::{Style, Stylize};
use ratatui::text::Span;
use ratatui::widgets::Widget;
use rat_ftable::{FTable, TableDataIter};

struct Data {
    table_data: Vec<Sample>
}

struct Sample {
    pub text: String
}

let data = Data {
    table_data: vec![],
};

struct RowIter1<'a> {
    iter: Enumerate<Iter<'a, Sample>>,
    item: Option<(usize, &'a Sample)>,
}

impl<'a> TableDataIter<'a> for RowIter1<'a> {
    fn rows(&self) -> Option<usize> {
        // If you can, give the length. Otherwise,
        // the table will iterate all to find out a length.
        None
        // Some(100_000)
    }

    /// Select the nth element from the current position.
    fn nth(&mut self, n: usize) -> bool {
        self.item = self.iter.nth(n);
        self.item.is_some()
    }

    /// Select the next element.
    fn next(&mut self) -> bool {
        self.item = self.iter.next();
        self.item.is_some()
    }

    /// Row height.
    fn row_height(&self) -> u16 {
        1
    }

    /// Row style.
    fn row_style(&self) -> Style {
        Style::default()
    }

    /// Render one cell.
    fn render_cell(&self, column: usize, area: Rect, buf: &mut Buffer) {
        let row = self.item.expect("data");
        match column {
            0 => {
                let row_fmt = NumberFormat::new("000000").expect("fmt");
                let span = Span::from(row_fmt.fmt_u(row.0));
                buf.set_style(area, Style::new().black().bg(Color::from_u32(0xe7c787)));
                span.render(area, buf);
            }
            1 => {
                let span = Span::from(&row.1.text);
                span.render(area, buf);
            }
            _ => {}
        }
    }
}

let mut rit = RowIter1 {
    iter: data.table_data.iter().enumerate(),
    item: None,
};

let table1 = FTable::default()
    .iter(&mut rit)
    .widths([
        Constraint::Length(6),
        Constraint::Length(20)
    ]);

// table1.render(area, buf);
source

pub fn header(self, header: Row<'a>) -> Self

Set the table-header.

source

pub fn footer(self, footer: Row<'a>) -> Self

Set the table-footer.

source

pub fn widths<I>(self, widths: I) -> Self

Column widths as Constraints.

source

pub fn flex(self, flex: Flex) -> Self

Flex for layout.

source

pub fn column_spacing(self, spacing: u16) -> Self

Spacing between columns.

source

pub fn layout_width(self, width: u16) -> Self

Overrides the width of the rendering area for layout purposes. Layout uses this width, even if it means that some columns are not visible.

source

pub fn block(self, block: Block<'a>) -> Self

Draws a block around the table widget.

source

pub fn styles(self, styles: FTableStyle) -> Self

Set all styles as a bundle.

source

pub fn style<S: Into<Style>>(self, style: S) -> Self

Base style for the table.

source

pub fn select_row_style<S: Into<Style>>(self, select_style: S) -> Self

Style for a selected row. The chosen selection must support row-selection for this to take effect.

source

pub fn select_column_style<S: Into<Style>>(self, select_style: S) -> Self

Style for a selected column. The chosen selection must support column-selection for this to take effect.

source

pub fn select_cell_style<S: Into<Style>>(self, select_style: S) -> Self

Style for a selected cell. The chosen selection must support cell-selection for this to take effect.

source

pub fn select_header_style<S: Into<Style>>(self, select_style: S) -> Self

Style for a selected header cell. The chosen selection must support column-selection for this to take effect.

Style for a selected footer cell. The chosen selection must support column-selection for this to take effect.

source

pub fn focus_style<S: Into<Style>>(self, focus_style: S) -> Self

This style will be patched onto the selection to indicate that the widget has the input focus.

The selection must support some kind of selection for this to be effective.

source

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

Indicates that this widget has the input focus.

source§

impl<'a, Selection> FTable<'a, Selection>

source

pub fn need_scroll(&self, area: Rect) -> (bool, bool)

Does this table need scrollbars? Returns (horizontal, vertical)

Trait Implementations§

source§

impl<'a, Selection: Debug> Debug for FTable<'a, Selection>

source§

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

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

impl<'a, Selection: Default> Default for FTable<'a, Selection>

source§

fn default() -> FTable<'a, Selection>

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

impl<'a, Selection> StatefulWidget for FTable<'a, Selection>
where Selection: TableSelection + Debug,

§

type State = FTableState<Selection>

State associated with the stateful widget. Read more
source§

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

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

impl<'a, Selection> Styled for FTable<'a, Selection>

§

type Item = FTable<'a, Selection>

source§

fn style(&self) -> Style

Returns the style of the object.
source§

fn set_style<S: Into<Style>>(self, style: S) -> Self::Item

Sets the style of the object. Read more

Auto Trait Implementations§

§

impl<'a, Selection> Freeze for FTable<'a, Selection>

§

impl<'a, Selection> !RefUnwindSafe for FTable<'a, Selection>

§

impl<'a, Selection> !Send for FTable<'a, Selection>

§

impl<'a, Selection> !Sync for FTable<'a, Selection>

§

impl<'a, Selection> Unpin for FTable<'a, Selection>
where Selection: Unpin,

§

impl<'a, Selection> !UnwindSafe for FTable<'a, Selection>

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<'a, T, U> Stylize<'a, T> for U
where U: Styled<Item = T>,

source§

fn bg(self, color: Color) -> T

source§

fn fg<S>(self, color: S) -> T
where S: Into<Color>,

source§

fn add_modifier(self, modifier: Modifier) -> T

source§

fn remove_modifier(self, modifier: Modifier) -> T

source§

fn reset(self) -> T

source§

fn black(self) -> T

Sets the foreground color to black.
source§

fn on_black(self) -> T

Sets the background color to black.
source§

fn red(self) -> T

Sets the foreground color to red.
source§

fn on_red(self) -> T

Sets the background color to red.
source§

fn green(self) -> T

Sets the foreground color to green.
source§

fn on_green(self) -> T

Sets the background color to green.
source§

fn yellow(self) -> T

Sets the foreground color to yellow.
source§

fn on_yellow(self) -> T

Sets the background color to yellow.
source§

fn blue(self) -> T

Sets the foreground color to blue.
source§

fn on_blue(self) -> T

Sets the background color to blue.
source§

fn magenta(self) -> T

Sets the foreground color to magenta.
source§

fn on_magenta(self) -> T

Sets the background color to magenta.
source§

fn cyan(self) -> T

Sets the foreground color to cyan.
source§

fn on_cyan(self) -> T

Sets the background color to cyan.
source§

fn gray(self) -> T

Sets the foreground color to gray.
source§

fn on_gray(self) -> T

Sets the background color to gray.
source§

fn dark_gray(self) -> T

Sets the foreground color to dark_gray.
source§

fn on_dark_gray(self) -> T

Sets the background color to dark_gray.
source§

fn light_red(self) -> T

Sets the foreground color to light_red.
source§

fn on_light_red(self) -> T

Sets the background color to light_red.
source§

fn light_green(self) -> T

Sets the foreground color to light_green.
source§

fn on_light_green(self) -> T

Sets the background color to light_green.
source§

fn light_yellow(self) -> T

Sets the foreground color to light_yellow.
source§

fn on_light_yellow(self) -> T

Sets the background color to light_yellow.
source§

fn light_blue(self) -> T

Sets the foreground color to light_blue.
source§

fn on_light_blue(self) -> T

Sets the background color to light_blue.
source§

fn light_magenta(self) -> T

Sets the foreground color to light_magenta.
source§

fn on_light_magenta(self) -> T

Sets the background color to light_magenta.
source§

fn light_cyan(self) -> T

Sets the foreground color to light_cyan.
source§

fn on_light_cyan(self) -> T

Sets the background color to light_cyan.
source§

fn white(self) -> T

Sets the foreground color to white.
source§

fn on_white(self) -> T

Sets the background color to white.
source§

fn bold(self) -> T

Adds the BOLD modifier.
source§

fn not_bold(self) -> T

Removes the BOLD modifier.
source§

fn dim(self) -> T

Adds the DIM modifier.
source§

fn not_dim(self) -> T

Removes the DIM modifier.
source§

fn italic(self) -> T

Adds the ITALIC modifier.
source§

fn not_italic(self) -> T

Removes the ITALIC modifier.
source§

fn underlined(self) -> T

Adds the UNDERLINED modifier.
source§

fn not_underlined(self) -> T

Removes the UNDERLINED modifier.
Adds the SLOW_BLINK modifier.
Removes the SLOW_BLINK modifier.
Adds the RAPID_BLINK modifier.
Removes the RAPID_BLINK modifier.
source§

fn reversed(self) -> T

Adds the REVERSED modifier.
source§

fn not_reversed(self) -> T

Removes the REVERSED modifier.
source§

fn hidden(self) -> T

Adds the HIDDEN modifier.
source§

fn not_hidden(self) -> T

Removes the HIDDEN modifier.
source§

fn crossed_out(self) -> T

Adds the CROSSED_OUT modifier.
source§

fn not_crossed_out(self) -> T

Removes the CROSSED_OUT modifier.
source§

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

§

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>,

§

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.