Skip to content

Commit

Permalink
Merge pull request #119 from Techassi/feature/row-col-count
Browse files Browse the repository at this point in the history
feat: Add col_count, row_count and is_empty methods
  • Loading branch information
Nukesor committed Aug 21, 2023
2 parents 9f45a5e + c4bf3ad commit aebf4ef
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// ```
Expand Down Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions tests/all/counts.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions tests/all/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit aebf4ef

Please sign in to comment.