Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow selecting specific columns #22

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Allow selecting specific columns from input

## [v1.0.0] - Mar 07, 2025

### Fixed
Expand Down
89 changes: 66 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tabled = "0.18.0"

[dev-dependencies]
assert_cmd = "2.0.16"
pretty_assertions = "1.4.1"
predicates = "3.1.3"

[[bin]]
name = "tbll"
Expand All @@ -39,7 +39,10 @@ doc = false
unwrap_used = "deny"
expect_used = "deny"

[profile.release]
lto = "fat"
codegen-units = 1

# The profile that 'cargo dist' will build with
[profile.dist]
inherits = "release"
lto = "thin"
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ struct Args {
/// Command separated list of headers
#[arg(long = "headers", value_name = "STRING,STRING...")]
headers: Option<String>,
/// Indices of columns (starting from zero) to display
#[arg(
short = 'c',
long = "cols",
value_name = "NUMBER,NUMBER...",
value_delimiter = ','
)]
cols: Option<Vec<usize>>,
/// Border Style
#[arg(short = 's', long = "style", value_name = "STRING")]
#[clap(value_enum, default_value = "sharp", value_name = "STRING")]
Expand Down Expand Up @@ -92,9 +100,9 @@ fn main() -> anyhow::Result<()> {
padding,
};

let output = get_output(&data, config);

println!("{output}");
if let Some(output) = get_output(&data, config, args.cols.as_deref()) {
println!("{output}");
}

Ok(())
}
81 changes: 73 additions & 8 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,38 @@ use tabled::{
settings::{Alignment, Padding, Style},
};

pub fn get_output(data: &[StringRecord], config: RenderConfig) -> String {
pub fn get_output(
data: &[StringRecord],
config: RenderConfig,
cols: Option<&[usize]>,
) -> Option<String> {
let mut builder = Builder::default();

data.iter().for_each(|d| {
builder.push_record(d);
});
match cols {
Some(index) => {
data.iter().for_each(|record| {
let vals = record
.iter()
.enumerate()
.filter(|(i, _)| index.contains(i))
.map(|(_, s)| s)
.collect::<Vec<_>>();
if !vals.is_empty() {
builder.push_record(vals);
}
});
}
None => {
data.iter().for_each(|record| {
builder.push_record(record);
});
}
}

let mut b = builder.build();
if b.count_rows() == 0 {
return None;
}

b.with(Alignment::left());
b.with(Style::sharp());
Expand All @@ -25,7 +49,7 @@ pub fn get_output(data: &[StringRecord], config: RenderConfig) -> String {

config.style.apply_to(&mut b);

b.to_string()
Some(b.to_string())
}

#[cfg(test)]
Expand Down Expand Up @@ -60,7 +84,7 @@ mod tests {
};

// WHEN
let got = get_output(&data, config);
let got = get_output(&data, config, None).expect("a string should've been returned");

// THEN
let expected = "
Expand All @@ -84,7 +108,7 @@ mod tests {
};

// WHEN
let got = get_output(&data, config);
let got = get_output(&data, config, None).expect("a string should've been returned");

// THEN
let expected = "
Expand All @@ -109,7 +133,7 @@ mod tests {
};

// WHEN
let got = get_output(&data, config);
let got = get_output(&data, config, None).expect("a string should've been returned");

// THEN
let expected = "
Expand All @@ -122,4 +146,45 @@ mod tests {
";
assert_eq!(got, expected.trim());
}

#[test]
fn selects_indices_correctly() {
// GIVEN
let data = generate_data();
let config = RenderConfig {
style: TableStyle::Sharp,
padding: TablePadding { left: 1, right: 1 },
};

// WHEN
let got = get_output(&data, config, Some(vec![0, 2]).as_deref())
.expect("a string should've been returned");

// THEN
let expected = "
┌──────────┬──────────┐
│ row1col1 │ row1col3 │
├──────────┼──────────┤
│ row2col1 │ row2col3 │
│ row3col1 │ row3col3 │
└──────────┴──────────┘
";
assert_eq!(got, expected.trim());
}

#[test]
fn returns_nothing_if_indices_are_incorrect() {
// GIVEN
let data = generate_data();
let config = RenderConfig {
style: TableStyle::Sharp,
padding: TablePadding { left: 1, right: 1 },
};

// WHEN
let got = get_output(&data, config, Some(vec![5, 8]).as_deref());

// THEN
assert!(got.is_none());
}
}
Loading