diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d09e67..b641354 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Add helper methods `(col,row)_count` and `is_empty`. The first set of methods return the number of columns and rows + respectively. The method `is_empty` returns if the table is empty (contains no data rows). Implemented by + [Techassi](https://github.com/Techassi) in [#119](https://github.com/Nukesor/comfy-table/pull/119). + ## [7.0.1] - 2023-06-16 ## Fix diff --git a/src/table.rs b/src/table.rs index 0ccc815..613075c 100644 --- a/src/table.rs +++ b/src/table.rs @@ -118,6 +118,21 @@ impl Table { self.header.as_ref() } + /// Returns the number of currently present columns. + /// + /// ``` + /// use comfy_table::Table; + /// + /// let mut table = Table::new(); + /// table.set_header(vec!["Col 1", "Col 2", "Col 3"]); + /// + /// assert_eq!(table.column_count(), 3); + /// ``` + pub fn column_count(&mut self) -> usize { + self.discover_columns(); + self.columns.len() + } + /// Add a new row to the table. /// /// ``` @@ -209,6 +224,35 @@ impl Table { self } + /// Returns the number of currently present rows. + /// + /// ``` + /// use comfy_table::Table; + /// + /// let mut table = Table::new(); + /// table.add_row(vec!["One", "Two"]); + /// + /// assert_eq!(table.row_count(), 1); + /// ``` + pub fn row_count(&self) -> usize { + self.rows.len() + } + + /// Returns if the table is empty (contains no data rows). + /// + /// ``` + /// use comfy_table::Table; + /// + /// let mut table = Table::new(); + /// assert!(table.is_empty()); + /// + /// table.add_row(vec!["One", "Two"]); + /// assert!(!table.is_empty()); + /// ``` + pub fn is_empty(&self) -> bool { + self.rows.is_empty() + } + /// Enforce a max width that should be used in combination with [dynamic content arrangement](ContentArrangement::Dynamic).\ /// This is usually not necessary, if you plan to output your table to a tty, /// since the terminal width can be automatically determined. diff --git a/tests/all/counts.rs b/tests/all/counts.rs new file mode 100644 index 0000000..8083505 --- /dev/null +++ b/tests/all/counts.rs @@ -0,0 +1,55 @@ +use pretty_assertions::assert_eq; + +use comfy_table::*; + +#[test] +fn test_col_count_header() { + let mut table = Table::new(); + + table.set_header(vec!["Col 1", "Col 2", "Col 3"]); + assert_eq!(table.column_count(), 3); + + table.set_header(vec!["Col 1", "Col 2", "Col 3", "Col 4"]); + assert_eq!(table.column_count(), 4); + + table.set_header(vec!["Col I", "Col II"]); + assert_eq!(table.column_count(), 4); +} + +#[test] +fn test_col_count_row() { + let mut table = Table::new(); + + table.add_row(vec!["Foo", "Bar"]); + assert_eq!(table.column_count(), 2); + + table.add_row(vec!["Bar", "Foo", "Baz"]); + assert_eq!(table.column_count(), 3); +} + +#[test] +fn test_row_count() { + let mut table = Table::new(); + assert_eq!(table.row_count(), 0); + + table.add_row(vec!["Foo", "Bar"]); + assert_eq!(table.row_count(), 1); + + table.add_row(vec!["Bar", "Foo", "Baz"]); + assert_eq!(table.row_count(), 2); + + table.add_row_if(|_, _| false, vec!["Baz", "Bar", "Foo"]); + assert_eq!(table.row_count(), 2); + + table.add_row_if(|_, _| true, vec!["Foo", "Baz", "Bar"]); + assert_eq!(table.row_count(), 3); +} + +#[test] +fn test_is_empty() { + let mut table = Table::new(); + assert_eq!(table.is_empty(), true); + + table.add_row(vec!["Foo", "Bar"]); + assert_eq!(table.is_empty(), false); +} diff --git a/tests/all/mod.rs b/tests/all/mod.rs index 29f4d2d..04a4f49 100644 --- a/tests/all/mod.rs +++ b/tests/all/mod.rs @@ -7,6 +7,7 @@ mod alignment_test; mod combined_test; mod constraints_test; mod content_arrangement_test; +mod counts; mod custom_delimiter_test; mod edge_cases; mod hidden_test;