diff --git a/CHANGELOG.md b/CHANGELOG.md index 659b694..6eebad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +- fix: field formatting is now applied to field 1 even if it's the only + one present and with no delimiters around + ## [1.1.0] - 2023-12-02 - feat: no more need to pass --join when using --replace, it's implied diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index 0b87edf..6458227 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -109,6 +109,13 @@ fn parse_args() -> Result { BoundsType::Fields }; + if bounds_type == BoundsType::Fields + && (maybe_fields.is_none() || maybe_fields.as_ref().unwrap().0.is_empty()) + { + eprintln!("tuc: invariant error. At least 1 field bound is expected with --fields"); + std::process::exit(1); + } + let delimiter = match bounds_type { BoundsType::Fields => pargs .opt_value_from_str(["-d", "--delimiter"])? diff --git a/src/bounds.rs b/src/bounds.rs index c5c3640..512d697 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -334,7 +334,7 @@ impl PartialEq for UserBounds { impl Default for UserBounds { fn default() -> Self { - UserBounds::new(Side::Some(1), Side::Some(1)) + UserBounds::new(Side::Some(1), Side::Continue) } } @@ -504,6 +504,11 @@ mod tests { ))], ); + assert_eq!( + parse_bounds_list("1:").unwrap(), + vec![BoundOrFiller::Bound(UserBounds::default())], + ); + assert_eq!( parse_bounds_list("1,2").unwrap(), vec![ diff --git a/src/cut_str.rs b/src/cut_str.rs index bb3b33d..38d4f09 100644 --- a/src/cut_str.rs +++ b/src/cut_str.rs @@ -263,7 +263,7 @@ pub fn cut_str( match bounds_as_ranges.len() { 1 if opt.only_delimited => (), - 1 => { + 1 if opt.bounds.0.len() == 1 => { stdout.write_all(line.as_bytes())?; stdout.write_all(eol)?; } diff --git a/src/options.rs b/src/options.rs index f073601..ebceca5 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,4 +1,4 @@ -use crate::bounds::{BoundsType, UserBoundsList}; +use crate::bounds::{BoundOrFiller, BoundsType, UserBounds, UserBoundsList}; use anyhow::Result; use std::str::FromStr; @@ -44,7 +44,7 @@ impl Default for Opt { Opt { delimiter: String::from("-"), eol: EOL::Newline, - bounds: UserBoundsList(Vec::new()), + bounds: UserBoundsList(vec![BoundOrFiller::Bound(UserBounds::default())]), bounds_type: BoundsType::Fields, only_delimited: false, greedy_delimiter: false, diff --git a/tests/cli.rs b/tests/cli.rs index 6c340dc..6c42516 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -247,6 +247,15 @@ fn it_format_fields() { .stdout("Say hello to our world.\nJust {saying}\n"); } +#[test] +fn it_format_field_1_even_with_no_matching_parameters() { + let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + + let assert = cmd.args(["-f", "Say {1}"]).write_stdin("hello").assert(); + + assert.success().stdout("Say hello\n"); +} + #[test] fn it_cuts_using_a_greedy_delimiter() { let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();