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

Fix format no delimiter field #116

Merged
merged 4 commits into from
Dec 6, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/bin/tuc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ fn parse_args() -> Result<Opt, pico_args::Error> {
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"])?
Expand Down
7 changes: 6 additions & 1 deletion src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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![
Expand Down
2 changes: 1 addition & 1 deletion src/cut_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub fn cut_str<W: Write>(

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)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bounds::{BoundsType, UserBoundsList};
use crate::bounds::{BoundOrFiller, BoundsType, UserBounds, UserBoundsList};
use anyhow::Result;
use std::str::FromStr;

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading