diff --git a/src/lib.rs b/src/lib.rs index 7635949..9e301bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,5 +45,4 @@ pub mod output; /// This module takes care for sending your mails! pub mod smtp; -/// The TUI for listing the mails for example. -pub mod table; +pub mod ui; diff --git a/src/mbox/model.rs b/src/mbox/model.rs index bee203c..748c369 100644 --- a/src/mbox/model.rs +++ b/src/mbox/model.rs @@ -7,7 +7,7 @@ use std::borrow::Cow; use std::collections::HashSet; use std::fmt; -use crate::table::{Cell, Row, Table}; +use crate::ui::table::{Cell, Row, Table}; // Attribute diff --git a/src/msg/model.rs b/src/msg/model.rs index 179f631..f1a9d42 100644 --- a/src/msg/model.rs +++ b/src/msg/model.rs @@ -11,7 +11,7 @@ use mailparse; use crate::{ ctx::Ctx, flag::model::Flags, - table::{Cell, Row, Table}, + ui::table::{Cell, Row, Table}, }; #[cfg(not(test))] diff --git a/src/ui/mod.rs b/src/ui/mod.rs new file mode 100644 index 0000000..020ecce --- /dev/null +++ b/src/ui/mod.rs @@ -0,0 +1,3 @@ +//! Modules related to User Interface. + +pub mod table; diff --git a/src/table.rs b/src/ui/table.rs similarity index 80% rename from src/table.rs rename to src/ui/table.rs index b1cc152..944d65a 100644 --- a/src/table.rs +++ b/src/ui/table.rs @@ -1,40 +1,67 @@ +//! Toolbox for building responsive tables. +//! A table is composed of rows, a row is composed of cells. +//! The toolbox uses the [builder design pattern]. +//! +//! [builder design pattern]: https://refactoring.guru/design-patterns/builder + use log::{debug, trace}; use std::fmt; +use terminal_size; use unicode_width::UnicodeWidthStr; +/// Define the default terminal size. +/// It is used when the size cannot be determined by the `terminal_size` crate. const DEFAULT_TERM_WIDTH: usize = 80; + +/// Define the minimum size of a shrinked cell. +/// TODO: make this customizable. const MAX_SHRINK_WIDTH: usize = 5; +/// Wrapper around [ANSI escape codes] for styling cells. +/// +/// [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code #[derive(Debug)] -pub struct Style(u8, u8, u8); +pub struct Style( + /// The style/color code. + u8, + /// The brightness code. + u8, + /// The shade code. + u8, +); impl fmt::Display for Style { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Style(color, bright, shade) = self; - let mut style = String::from("\x1b["); + let mut style = String::new(); + // Push first the style/color code. style.push_str(&color.to_string()); + // Then push the brightness code if exist. if *bright > 0 { style.push_str(";"); style.push_str(&bright.to_string()); }; + // Then push the shade code if exist. if *shade > 0 { style.push_str(";"); style.push_str(&shade.to_string()); }; - style.push_str("m"); - - write!(f, "{}", style) + write!(f, "\x1b[{}m", style) } } +/// Representation of a table cell. #[derive(Debug)] pub struct Cell { + /// The list of style applied to the cell. styles: Vec