Skip to content

Commit

Permalink
Merge pull request #1546 from jqnatividad/fmt-out-delimiter-tab
Browse files Browse the repository at this point in the history
`fat`: `--out-delimiter` now treats "T" as special value for tab character
  • Loading branch information
jqnatividad authored Jan 17, 2024
2 parents 61f2023 + 7d3c7e0 commit 98e48cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/cmd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Usage:
fmt options:
-t, --out-delimiter <arg> The field delimiter for writing CSV data.
Must be a single character.
If set to "T", uses tab as the delimiter.
[default: ,]
--crlf Use '\r\n' line endings in the output.
--ascii Use ASCII field and record separators. Use Substitute (U+00A1) as the
Expand All @@ -25,7 +27,7 @@ fmt options:
--escape <arg> The escape character to use. When not specified,
quotes are escaped by doubling them.
--no-final-newline Do not write a newline at the end of the output.
This makes it easier to paste the output into Excel.
This makes it easier to paste the output into Excel.
Common options:
-h, --help Display this message
Expand Down Expand Up @@ -59,6 +61,10 @@ struct Args {
pub fn run(argv: &[&str]) -> CliResult<()> {
let mut args: Args = util::get_args(USAGE, argv)?;

if args.flag_out_delimiter == Some(Delimiter(b'T')) {
args.flag_out_delimiter = Some(Delimiter(b'\t'));
}

let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.no_headers(true);
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const NO_INDEX_WARNING_FILESIZE: u64 = 100_000_000; // 100MB
// so we don't have to keep checking if the index has been created
static AUTO_INDEXED: AtomicBool = AtomicBool::new(false);

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Delimiter(pub u8);

/// Delimiter represents values that can be passed from the command line that
Expand Down
10 changes: 10 additions & 0 deletions tests/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ mnopqr,stuvwx\r
assert_eq!(got, expected.to_string());
}

#[test]
fn fmt_tab_delimiter() {
let (wrk, mut cmd) = setup("fmt_tab_delimiter");
cmd.args(["--out-delimiter", "T"]);

let got: String = wrk.stdout(&mut cmd);
let expected = "h1\th2\nabcdef\tghijkl\nmnopqr\tstuvwx\n\"ab\"\"cd\"\"ef\"\tgh,ij,kl";
assert_eq!(got, expected.to_string());
}

#[test]
fn fmt_nofinalnewline() {
let (wrk, mut cmd) = setup("fmt_nofinalnewline");
Expand Down

0 comments on commit 98e48cd

Please sign in to comment.