pub struct Model {
pub columns: Vec<Column>,
pub rows: Vec<Row>,
pub selected: usize,
pub width: i32,
pub height: i32,
pub keymap: TableKeyMap,
pub styles: Styles,
pub focus: bool,
pub help: Model,
/* private fields */
}Expand description
Interactive table model containing data, styling, navigation state, and a viewport for efficient rendering and scrolling.
Fields§
§columns: Vec<Column>Column definitions controlling headers and widths.
rows: Vec<Row>Row data; each row contains one string per column.
selected: usizeIndex of the currently selected row (0-based).
width: i32Rendered width of the table in characters.
height: i32Rendered height of the table in lines.
keymap: TableKeyMapKey bindings for navigation and movement.
styles: StylesStyles used when rendering the table.
focus: boolWhether this table currently has keyboard focus.
help: ModelHelp model used to render key binding help.
Implementations§
Source§impl Model
impl Model
Sourcepub fn new(columns: Vec<Column>) -> Self
pub fn new(columns: Vec<Column>) -> Self
Creates a new table with the given columns and sensible defaults.
Defaults: height 20, focused, empty rows, and default styles/keymap.
Sourcepub fn with_rows(self, rows: Vec<Row>) -> Self
pub fn with_rows(self, rows: Vec<Row>) -> Self
Sets the table rows on construction and returns self for chaining.
Sourcepub fn set_width(&mut self, w: i32)
pub fn set_width(&mut self, w: i32)
Sets the table width in characters and rebuilds the viewport content.
Sourcepub fn set_height(&mut self, h: i32)
pub fn set_height(&mut self, h: i32)
Sets the table height in lines and rebuilds the viewport content.
Sourcepub fn add_row(&mut self, row: Row)
pub fn add_row(&mut self, row: Row)
Appends a row to the table and refreshes the rendered content.
Sourcepub fn selected_row(&self) -> Option<&Row>
pub fn selected_row(&self) -> Option<&Row>
Returns a reference to the currently selected row, if any.
Sourcepub fn select_next(&mut self)
pub fn select_next(&mut self)
Moves the selection down by one row.
Sourcepub fn select_prev(&mut self)
pub fn select_prev(&mut self)
Moves the selection up by one row.
Trait Implementations§
Source§impl KeyMap for Model
Help system integration for displaying table navigation keys.
impl KeyMap for Model
Help system integration for displaying table navigation keys.
This implementation provides the help system with information about the table’s key bindings, enabling automatic generation of help text that documents the available navigation commands.
Source§fn short_help(&self) -> Vec<&Binding>
fn short_help(&self) -> Vec<&Binding>
Returns the most commonly used key bindings for short help display.
This method provides a concise list of the most essential navigation keys that users need to know for basic table operation. It’s used when displaying compact help information.
§Returns
A vector of key binding references for row and page navigation
§Examples
use bubbletea_widgets::table::{Model, Column};
use bubbletea_widgets::help::KeyMap;
let table = Model::new(vec![Column::new("Data", 20)]);
let short_bindings = table.short_help();
// Returns bindings for: up, down, page up, page down
assert_eq!(short_bindings.len(), 4);§Help Content
The short help includes:
- Row Up: Move selection up one row
- Row Down: Move selection down one row
- Page Up: Move up one page of rows
- Page Down: Move down one page of rows
Source§fn full_help(&self) -> Vec<Vec<&Binding>>
fn full_help(&self) -> Vec<Vec<&Binding>>
Returns all key bindings organized by category for full help display.
This method provides a comprehensive list of all available navigation keys, organized into logical groups for clear presentation in detailed help displays. Each group contains related navigation commands.
§Returns
A vector of groups, where each group is a vector of related key bindings
§Examples
use bubbletea_widgets::table::{Model, Column};
use bubbletea_widgets::help::KeyMap;
let table = Model::new(vec![Column::new("Data", 20)]);
let full_bindings = table.full_help();
// Returns 4 groups of key bindings
assert_eq!(full_bindings.len(), 4);
// First group: row navigation (up/down)
assert_eq!(full_bindings[0].len(), 2);§Help Organization
The full help is organized into these groups:
- Row Navigation: Single row up/down movement
- Page Navigation: Full page up/down scrolling
- Half Page Navigation: Half page up/down movement
- Jump Navigation: Go to start/end positions
§Display Integration
This grouped format allows help displays to show related commands together with appropriate spacing and categorization for better user comprehension.
Source§impl Model for Model
impl Model for Model
Source§fn init() -> (Self, Option<Cmd>)
fn init() -> (Self, Option<Cmd>)
Creates a new empty table model for Bubble Tea applications.
This initialization method creates a table with no columns or data, suitable for applications that will configure the table structure after initialization. The table starts focused and ready to receive keyboard input.
§Returns
A tuple containing the new table model and no initial command
§Examples
use bubbletea_widgets::table::Model;
use bubbletea_rs::Model as BubbleTeaModel;
// This is typically called by the Bubble Tea framework
let (mut table, cmd) = Model::init();
assert_eq!(table.columns.len(), 0);
assert_eq!(table.rows.len(), 0);
assert!(cmd.is_none());§Note
Most applications will want to use Model::new(columns) directly
instead of this init method, as it allows specifying the table
structure immediately.
Source§fn update(&mut self, msg: Msg) -> Option<Cmd>
fn update(&mut self, msg: Msg) -> Option<Cmd>
Processes messages and updates table state with keyboard navigation.
This method handles all keyboard navigation for the table, including row selection, page scrolling, and jumping to start/end positions. It only processes messages when the table is focused, ensuring proper behavior in multi-component applications.
§Arguments
msg- The message to process, typically aKeyMsgfor keyboard input
§Returns
An optional Cmd that may need to be executed (currently always None)
§Key Handling
The following keys are processed based on the table’s key map configuration:
- Row Navigation: Up/Down arrows,
k/jkeys - Page Navigation: Page Up/Down,
b/fkeys - Half Page:
u/dkeys for half-page scrolling - Jump Navigation: Home/End,
g/Gkeys for start/end
§Examples
use bubbletea_widgets::table::{Model, Column};
use bubbletea_rs::{KeyMsg, Model as BubbleTeaModel};
use crossterm::event::{KeyCode, KeyModifiers};
let mut table = Model::new(vec![Column::new("Data", 20)]);
// Simulate down arrow key press
let key_msg = Box::new(KeyMsg {
key: KeyCode::Down,
modifiers: KeyModifiers::NONE,
});
let cmd = table.update(key_msg);
// Table selection moves down (if there are rows)§Focus Handling
If the table is not focused (self.focus == false), this method
returns immediately without processing the message. This allows
multiple components to coexist without interference.
§Performance Optimization
After any navigation that changes the selection, the viewport content is automatically rebuilt to ensure the selected row remains visible and the display is updated correctly.
Source§fn view(&self) -> String
fn view(&self) -> String
Renders the table for display in a Bubble Tea application.
This method delegates to the table’s own view() method to generate
the formatted string representation. It’s called by the Bubble Tea
framework during the render cycle.
§Returns
A multi-line string containing the formatted table with headers, data rows, selection highlighting, and applied styling
§Examples
use bubbletea_widgets::table::{Model, Column, Row};
use bubbletea_rs::Model as BubbleTeaModel;
let mut table = Model::new(vec![Column::new("Name", 15)]);
table.add_row(Row::new(vec!["Alice".into()]));
let output = table.view();
// Contains formatted table ready for terminal display§Integration Pattern
This method is typically called from your application’s main view() method:
use bubbletea_widgets::table::Model as TableModel;
use bubbletea_rs::Model as BubbleTeaModel;
struct App {
table: TableModel,
}
impl BubbleTeaModel for App {
fn view(&self) -> String {
format!("My Application\n\n{}", self.table.view())
}
}Auto Trait Implementations§
impl Freeze for Model
impl !RefUnwindSafe for Model
impl Send for Model
impl Sync for Model
impl Unpin for Model
impl !UnwindSafe for Model
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more