Module table

Module table 

Source
Expand description

Interactive data table component with navigation, selection, and scrolling capabilities.

This module provides a comprehensive table implementation for terminal user interfaces, designed for displaying structured data with keyboard navigation and visual selection. It features efficient viewport-based rendering, customizable styling, and integration with the Bubble Tea architecture.

§Core Components

  • Model: The main table with data, state, and navigation logic
  • Column: Column definitions with titles and width constraints
  • Row: Row data containing cell values
  • Styles: Visual styling for headers, cells, and selection states
  • TableKeyMap: Configurable keyboard bindings for navigation

§Key Features

  • Vim-Style Navigation: Familiar keyboard shortcuts for power users
  • Viewport Scrolling: Efficient rendering of large datasets
  • Selection Highlighting: Visual feedback for the current row
  • Responsive Layout: Automatic column width and table sizing
  • Help Integration: Built-in documentation of key bindings
  • Customizable Styling: Full control over appearance and colors
KeysActionDescription
, kRow UpMove selection up one row
, jRow DownMove selection down one row
PgUp, bPage UpMove up one page of rows
PgDn, fPage DownMove down one page of rows
uHalf Page UpMove up half a page
dHalf Page DownMove down half a page
Home, gGo to StartJump to first row
End, GGo to EndJump to last row

§Quick Start

use bubbletea_widgets::table::{Model, Column, Row};

// Define table structure
let columns = vec![
    Column::new("Product", 25),
    Column::new("Price", 10),
    Column::new("Stock", 8),
];

// Add data rows
let rows = vec![
    Row::new(vec!["MacBook Pro".into(), "$2399".into(), "5".into()]),
    Row::new(vec!["iPad Air".into(), "$599".into(), "12".into()]),
    Row::new(vec!["AirPods Pro".into(), "$249".into(), "23".into()]),
];

// Create and configure table
let mut table = Model::new(columns)
    .with_rows(rows);
table.set_width(50);
table.set_height(10);

§Integration with Bubble Tea

use bubbletea_widgets::table::{Model as TableModel, Column, Row};
use bubbletea_rs::{Model as BubbleTeaModel, Cmd, Msg, KeyMsg};

struct App {
    table: TableModel,
}

impl BubbleTeaModel for App {
    fn init() -> (Self, Option<Cmd>) {
        let table = TableModel::new(vec![
            Column::new("ID", 8),
            Column::new("Name", 20),
            Column::new("Status", 12),
        ]);
        (App { table }, None)
    }

    fn update(&mut self, msg: Msg) -> Option<Cmd> {
        // Forward navigation messages to table
        self.table.update(msg)
    }

    fn view(&self) -> String {
        format!("My Data Table:\n\n{}", self.table.view())
    }
}

§Styling and Customization

use bubbletea_widgets::table::{Model, Column, Row, Styles};
use lipgloss_extras::prelude::*;

let mut table = Model::new(vec![Column::new("Data", 20)]);

// Customize appearance
table.styles = Styles {
    header: Style::new()
        .bold(true)
        .background(Color::from("#1e40af"))
        .foreground(Color::from("#ffffff"))
        .padding(0, 1, 0, 1),
    cell: Style::new()
        .padding(0, 1, 0, 1),
    selected: Style::new()
        .bold(true)
        .background(Color::from("#10b981"))
        .foreground(Color::from("#ffffff")),
};

§Performance Considerations

  • Uses viewport rendering for efficient display of large datasets
  • Only visible rows are rendered, enabling smooth performance with thousands of rows
  • Column widths should be set appropriately to avoid layout recalculation
  • Selection changes trigger content rebuilding, but viewport limits render cost

Structs§

Column
Represents a table column with its title and display width.
Model
Interactive table model containing data, styling, navigation state, and a viewport for efficient rendering and scrolling.
Row
Represents a single row of data in the table.
Styles
Visual styling configuration for table rendering.
TableKeyMap
Keyboard binding configuration for table navigation.

Functions§

with_columns
Creates an option to set table columns during construction.
with_focused
Creates an option to set table focus state during construction.
with_height
Creates an option to set table height during construction.
with_key_map
Creates an option to set table key map during construction.
with_rows
Creates an option to set table rows during construction.
with_styles
Creates an option to set table styles during construction.
with_width
Creates an option to set table width during construction.

Type Aliases§

TableOption
Configuration option for table construction.