papergrid/records/
mod.rs

1//! The module contains a [Records] abstraction of a [`Grid`] trait and its implementators.
2//!
3//! [`Grid`]: crate::Grid
4
5use crate::{width::WidthFunc, Position};
6
7pub mod cell_info;
8pub mod empty;
9pub mod vec_records;
10
11#[cfg(feature = "color")]
12pub mod tcell;
13
14/// The representaion of data, rows and columns of a [`Grid`].
15///
16/// [`Grid`]: crate::Grid
17pub trait Records {
18    /// Returns amount of rows on a grid.
19    fn count_rows(&self) -> usize;
20
21    /// Returns amount of columns on a grid.
22    fn count_columns(&self) -> usize;
23
24    /// Returns a text of a cell by an index.
25    fn get_text(&self, pos: Position) -> &str;
26
27    /// Returns a line of a text of a cell by an index.
28    fn get_line(&self, pos: Position, i: usize) -> &str;
29
30    /// Returns an amount of lines of a text of a cell by an index.
31    fn count_lines(&self, pos: Position) -> usize;
32
33    /// Returns a width of a text of a cell by an index.
34    fn get_width<W>(&self, pos: Position, width_ctrl: W) -> usize
35    where
36        W: WidthFunc;
37
38    /// Returns a width of line of a text of a cell by an index.
39    fn get_line_width<W>(&self, pos: Position, i: usize, width_ctrl: W) -> usize
40    where
41        W: WidthFunc;
42
43    /// Prints a prefix of a text of a cell by an index.
44    ///
45    /// Maybe be usefull in order to emit ANSI sequences.
46    fn fmt_text_prefix(&self, f: &mut std::fmt::Formatter<'_>, pos: Position) -> std::fmt::Result;
47
48    /// Prints a suffix of a text of a cell by an index.
49    ///
50    /// Maybe be usefull in order to emit ANSI sequences.
51    fn fmt_text_suffix(&self, f: &mut std::fmt::Formatter<'_>, pos: Position) -> std::fmt::Result;
52}
53
54impl<R> Records for &R
55where
56    R: Records,
57{
58    fn count_rows(&self) -> usize {
59        R::count_rows(self)
60    }
61
62    fn count_columns(&self) -> usize {
63        R::count_columns(self)
64    }
65
66    fn get_text(&self, pos: Position) -> &str {
67        R::get_text(self, pos)
68    }
69
70    fn get_line(&self, pos: Position, i: usize) -> &str {
71        R::get_line(self, pos, i)
72    }
73
74    fn count_lines(&self, pos: Position) -> usize {
75        R::count_lines(self, pos)
76    }
77
78    fn get_width<W>(&self, pos: Position, width_ctrl: W) -> usize
79    where
80        W: WidthFunc,
81    {
82        R::get_width(self, pos, width_ctrl)
83    }
84
85    fn get_line_width<W>(&self, pos: Position, i: usize, width_ctrl: W) -> usize
86    where
87        W: WidthFunc,
88    {
89        R::get_line_width(self, pos, i, width_ctrl)
90    }
91
92    fn fmt_text_prefix(&self, f: &mut std::fmt::Formatter<'_>, pos: Position) -> std::fmt::Result {
93        R::fmt_text_prefix(self, f, pos)
94    }
95
96    fn fmt_text_suffix(&self, f: &mut std::fmt::Formatter<'_>, pos: Position) -> std::fmt::Result {
97        R::fmt_text_suffix(self, f, pos)
98    }
99}
100
101/// A [`Grid`] representation of a data set which can be modified.
102///
103/// [`Grid`]: crate::Grid
104pub trait RecordsMut<T> {
105    /// Sets a text to a given cell by index.
106    fn set<W>(&mut self, pos: Position, text: T, width_ctrl: W)
107    where
108        W: WidthFunc;
109
110    /// Updates a given cell by index.
111    ///
112    /// Maybe used if width function was changed.
113    fn update<W>(&mut self, pos: Position, width_ctrl: W)
114    where
115        W: WidthFunc;
116}
117
118/// A [`Grid`] representation of a data set which can be modified by moving rows/columns around.
119///
120/// [`Grid`]: crate::Grid
121pub trait Resizable {
122    /// Swap cells with one another.
123    fn swap(&mut self, lhs: Position, rhs: Position);
124    /// Swap rows with one another.
125    fn swap_row(&mut self, lhs: usize, rhs: usize);
126    /// Swap columns with one another.
127    fn swap_column(&mut self, lhs: usize, rhs: usize);
128    /// Adds a new row to a data set.
129    fn push_row(&mut self);
130    /// Adds a new column to a data set.
131    fn push_column(&mut self);
132    /// Removes a row from a data set by index.
133    fn remove_row(&mut self, row: usize);
134    /// Removes a column from a data set by index.
135    fn remove_column(&mut self, column: usize);
136    /// Inserts a row to specific by row index.
137    fn insert_row(&mut self, row: usize);
138}