Skip to main content

rat_ftable2/edit/
mod.rs

1//!
2//! Table-widget and row-editor.
3//!
4
5use ratatui_core::buffer::Buffer;
6use ratatui_core::layout::Rect;
7
8mod state;
9mod widget;
10
11pub use state::*;
12pub use widget::*;
13
14/// This trait renders the row-editor widgets.
15///
16/// It gets a slice of areas for all the cells in the table,
17/// and renders all input widgets as it needs.
18pub trait TableEditor<T> {
19    /// State associated with the stateful widget.
20    type State: TableEditorState<T>;
21
22    /// Standard render call, but with added areas for each cell.
23    /// The area is the area of the full row.
24    fn render(
25        &self, //
26        area: Rect,
27        cell_areas: &[Rect],
28        buf: &mut Buffer,
29        state: &mut Self::State,
30    );
31}
32
33///
34/// Trait for the editor widget state.
35///
36#[allow(unused_variables)]
37pub trait TableEditorState<T> {
38    /// Some external context.
39    type Context<'a>;
40    /// Error type.
41    type Err;
42
43    /// Create a fresh row value with all the defaults.
44    fn create_value<'a>(&self, ctx: &'a Self::Context<'a>) -> Result<T, Self::Err>;
45
46    /// Set the current row value for the editor.
47    fn set_value<'a>(&mut self, value: T, ctx: &'a Self::Context<'a>) -> Result<(), Self::Err>;
48
49    /// Return the current row value from the editor.
50    /// None results will remove the table row.
51    fn value<'a>(&mut self, ctx: &'a Self::Context<'a>) -> Result<Option<T>, Self::Err>;
52
53    /// Returns the currently focused column.
54    /// Used to scroll the column to fully visible.
55    fn focused_col(&self) -> Option<usize> {
56        None
57    }
58
59    /// Start editing the current value.
60    /// If a specific column should be focused initially it is given as
61    /// the column index.
62    fn start<'a>(
63        &mut self,
64        col: Option<usize>,
65        ctx: &'a Self::Context<'a>,
66    ) -> Result<(), Self::Err> {
67        Ok(())
68    }
69
70    /// Stop editing the current value.
71    /// If necessary the editor can do some cleanup here.
72    fn stop<'a>(&mut self, ctx: &'a Self::Context<'a>) -> Result<(), Self::Err> {
73        Ok(())
74    }
75}
76
77/// Editing mode.
78#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
79pub enum Mode {
80    #[default]
81    View,
82    Edit,
83    Insert,
84}