summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/settings/format
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/tabled/src/settings/format')
-rw-r--r--vendor/tabled/src/settings/format/format_config.rs14
-rw-r--r--vendor/tabled/src/settings/format/format_content.rs86
-rw-r--r--vendor/tabled/src/settings/format/format_positioned.rs51
-rw-r--r--vendor/tabled/src/settings/format/mod.rs144
4 files changed, 295 insertions, 0 deletions
diff --git a/vendor/tabled/src/settings/format/format_config.rs b/vendor/tabled/src/settings/format/format_config.rs
new file mode 100644
index 000000000..247ead344
--- /dev/null
+++ b/vendor/tabled/src/settings/format/format_config.rs
@@ -0,0 +1,14 @@
+use crate::settings::TableOption;
+
+/// This is a struct wrapper for a lambda which changes config.
+#[derive(Debug)]
+pub struct FormatConfig<F>(pub(crate) F);
+
+impl<F, R, D, C> TableOption<R, D, C> for FormatConfig<F>
+where
+ F: FnMut(&mut C),
+{
+ fn change(mut self, _: &mut R, cfg: &mut C, _: &mut D) {
+ (self.0)(cfg);
+ }
+}
diff --git a/vendor/tabled/src/settings/format/format_content.rs b/vendor/tabled/src/settings/format/format_content.rs
new file mode 100644
index 000000000..c14eee64f
--- /dev/null
+++ b/vendor/tabled/src/settings/format/format_content.rs
@@ -0,0 +1,86 @@
+use crate::{
+ grid::config::Entity,
+ grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
+ settings::{CellOption, TableOption},
+};
+
+/// A lambda which formats cell content.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+pub struct FormatContent<F> {
+ f: F,
+ multiline: bool,
+}
+
+impl<F> FormatContent<F> {
+ pub(crate) fn new(f: F) -> Self {
+ Self {
+ f,
+ multiline: false,
+ }
+ }
+}
+
+impl<F> FormatContent<F> {
+ /// Multiline a helper function for changing multiline content of cell.
+ /// Using this formatting applied for all rows not to a string as a whole.
+ ///
+ /// ```rust,no_run
+ /// use tabled::{Table, settings::{Format, object::Segment, Modify}};
+ ///
+ /// let data: Vec<&'static str> = Vec::new();
+ /// let table = Table::new(&data)
+ /// .with(Modify::new(Segment::all()).with(Format::content(|s| s.to_string()).multiline()))
+ /// .to_string();
+ /// ```
+ pub fn multiline(mut self) -> Self {
+ self.multiline = true;
+ self
+ }
+}
+
+impl<F, R, D, C> TableOption<R, D, C> for FormatContent<F>
+where
+ F: FnMut(&str) -> String + Clone,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+{
+ fn change(self, records: &mut R, cfg: &mut C, _: &mut D) {
+ CellOption::change(self, records, cfg, Entity::Global);
+ }
+}
+
+impl<F, R, C> CellOption<R, C> for FormatContent<F>
+where
+ F: FnMut(&str) -> String + Clone,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+{
+ fn change(mut self, records: &mut R, _: &mut C, entity: Entity) {
+ let count_rows = records.count_rows();
+ let count_cols = records.count_columns();
+
+ for pos in entity.iter(count_rows, count_cols) {
+ let is_valid_pos = pos.0 < count_rows && pos.1 < count_cols;
+ if !is_valid_pos {
+ continue;
+ }
+
+ let content = records.get_text(pos);
+ let content = if self.multiline {
+ multiline(self.f.clone())(content)
+ } else {
+ (self.f)(content)
+ };
+ records.set(pos, content);
+ }
+ }
+}
+
+fn multiline<F: FnMut(&str) -> String>(mut f: F) -> impl FnMut(&str) -> String {
+ move |s: &str| {
+ let mut v = Vec::new();
+ for line in s.lines() {
+ v.push(f(line));
+ }
+
+ v.join("\n")
+ }
+}
diff --git a/vendor/tabled/src/settings/format/format_positioned.rs b/vendor/tabled/src/settings/format/format_positioned.rs
new file mode 100644
index 000000000..e121f007e
--- /dev/null
+++ b/vendor/tabled/src/settings/format/format_positioned.rs
@@ -0,0 +1,51 @@
+use crate::{
+ grid::config::Entity,
+ grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
+ settings::{CellOption, TableOption},
+};
+
+/// [`FormatContentPositioned`] is like a [`FormatContent`] an abstraction over a function you can use against a cell.
+///
+/// It different from [`FormatContent`] that it provides a row and column index.
+///
+/// [`FormatContent`]: crate::settings::format::FormatContent
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+pub struct FormatContentPositioned<F>(F);
+
+impl<F> FormatContentPositioned<F> {
+ pub(crate) fn new(f: F) -> Self {
+ Self(f)
+ }
+}
+
+impl<F, R, D, C> TableOption<R, D, C> for FormatContentPositioned<F>
+where
+ F: FnMut(&str, (usize, usize)) -> String,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+{
+ fn change(self, records: &mut R, cfg: &mut C, _: &mut D) {
+ CellOption::change(self, records, cfg, Entity::Global);
+ }
+}
+
+impl<F, R, C> CellOption<R, C> for FormatContentPositioned<F>
+where
+ F: FnMut(&str, (usize, usize)) -> String,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+{
+ fn change(mut self, records: &mut R, _: &mut C, entity: Entity) {
+ let count_rows = records.count_rows();
+ let count_cols = records.count_columns();
+
+ for pos in entity.iter(count_rows, count_cols) {
+ let is_valid_pos = pos.0 < count_rows && pos.1 < count_cols;
+ if !is_valid_pos {
+ continue;
+ }
+
+ let content = records.get_text(pos);
+ let content = (self.0)(content, pos);
+ records.set(pos, content);
+ }
+ }
+}
diff --git a/vendor/tabled/src/settings/format/mod.rs b/vendor/tabled/src/settings/format/mod.rs
new file mode 100644
index 000000000..e8221ae32
--- /dev/null
+++ b/vendor/tabled/src/settings/format/mod.rs
@@ -0,0 +1,144 @@
+//! This module contains a list of primitives to help to modify a [`Table`].
+//!
+//! [`Table`]: crate::Table
+
+mod format_config;
+mod format_content;
+mod format_positioned;
+
+pub use format_config::FormatConfig;
+pub use format_content::FormatContent;
+pub use format_positioned::FormatContentPositioned;
+
+/// A formatting function of particular cells on a [`Table`].
+///
+/// [`Table`]: crate::Table
+#[derive(Debug)]
+pub struct Format;
+
+impl Format {
+ /// This function creates a new [`Format`] instance, so
+ /// it can be used as a grid setting.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use tabled::{Table, settings::{Format, object::Rows, Modify}};
+ ///
+ /// let data = vec![
+ /// (0, "Grodno", true),
+ /// (1, "Minsk", true),
+ /// (2, "Hamburg", false),
+ /// (3, "Brest", true),
+ /// ];
+ ///
+ /// let table = Table::new(&data)
+ /// .with(Modify::new(Rows::new(1..)).with(Format::content(|s| format!(": {} :", s))))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+-------+-------------+-----------+\n\
+ /// | i32 | &str | bool |\n\
+ /// +-------+-------------+-----------+\n\
+ /// | : 0 : | : Grodno : | : true : |\n\
+ /// +-------+-------------+-----------+\n\
+ /// | : 1 : | : Minsk : | : true : |\n\
+ /// +-------+-------------+-----------+\n\
+ /// | : 2 : | : Hamburg : | : false : |\n\
+ /// +-------+-------------+-----------+\n\
+ /// | : 3 : | : Brest : | : true : |\n\
+ /// +-------+-------------+-----------+"
+ /// );
+ /// ```
+ pub fn content<F>(f: F) -> FormatContent<F>
+ where
+ F: FnMut(&str) -> String,
+ {
+ FormatContent::new(f)
+ }
+
+ /// This function creates a new [`FormatContentPositioned`], so
+ /// it can be used as a grid setting.
+ ///
+ /// It's different from [`Format::content`] as it also provides a row and column index.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use tabled::{Table, settings::{Format, object::Rows, Modify}};
+ ///
+ /// let data = vec![
+ /// (0, "Grodno", true),
+ /// (1, "Minsk", true),
+ /// (2, "Hamburg", false),
+ /// (3, "Brest", true),
+ /// ];
+ ///
+ /// let table = Table::new(&data)
+ /// .with(Modify::new(Rows::single(0)).with(Format::positioned(|_, (_, col)| col.to_string())))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+---+---------+-------+\n\
+ /// | 0 | 1 | 2 |\n\
+ /// +---+---------+-------+\n\
+ /// | 0 | Grodno | true |\n\
+ /// +---+---------+-------+\n\
+ /// | 1 | Minsk | true |\n\
+ /// +---+---------+-------+\n\
+ /// | 2 | Hamburg | false |\n\
+ /// +---+---------+-------+\n\
+ /// | 3 | Brest | true |\n\
+ /// +---+---------+-------+"
+ /// );
+ /// ```
+ pub fn positioned<F>(f: F) -> FormatContentPositioned<F>
+ where
+ F: FnMut(&str, (usize, usize)) -> String,
+ {
+ FormatContentPositioned::new(f)
+ }
+
+ /// This function creates [`FormatConfig`] function to modify a table config.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use tabled::{
+ /// Table,
+ /// settings::{Format, object::Rows, Modify},
+ /// grid::config::ColoredConfig,
+ /// };
+ ///
+ /// let data = vec![
+ /// (0, "Grodno", true),
+ /// (1, "Minsk", true),
+ /// (2, "Hamburg", false),
+ /// (3, "Brest", true),
+ /// ];
+ ///
+ /// let table = Table::new(&data)
+ /// .with(Format::config(|cfg: &mut ColoredConfig| cfg.set_justification((0,1).into(), '.')))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+-----+---------+-------+\n\
+ /// | i32 | &str... | bool |\n\
+ /// +-----+---------+-------+\n\
+ /// | 0 | Grodno | true |\n\
+ /// +-----+---------+-------+\n\
+ /// | 1 | Minsk | true |\n\
+ /// +-----+---------+-------+\n\
+ /// | 2 | Hamburg | false |\n\
+ /// +-----+---------+-------+\n\
+ /// | 3 | Brest | true |\n\
+ /// +-----+---------+-------+"
+ /// );
+ /// ```
+ pub fn config<F>(f: F) -> FormatConfig<F> {
+ FormatConfig(f)
+ }
+}