Skip to content

Commit

Permalink
Add "--align" flag to table command.
Browse files Browse the repository at this point in the history
This flag allows the user to specify how data should be aligned within
columns. The options are:

   * left
   * right
   * center

This alignment is applied to all columns.

Per the tabwriter crate, center alignment breaks ties in favor of being
closer to the left. Thus if "hi" appears in a column of width 5, it will
be printed as " hi  ".

resolves BurntSushi#165.
  • Loading branch information
alex-ozdemir committed Feb 21, 2020
1 parent 3de6c04 commit 98f04c6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ regex = "1"
serde = "1"
serde_derive = "1"
streaming-stats = "0.2"
tabwriter = "1"
tabwriter = "1.2"
threadpool = "1.3"

[dev-dependencies]
Expand Down
29 changes: 26 additions & 3 deletions src/cmd/table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::borrow::Cow;
use std::convert::From;

use csv;
use tabwriter::TabWriter;
use tabwriter::{Alignment, TabWriter};

use CliResult;
use config::{Config, Delimiter};
Expand All @@ -24,7 +25,10 @@ table options:
[default: 2]
-p, --pad <arg> The minimum number of spaces between each column.
[default: 2]
-c, --condense <arg> Limits the length of each field to the value
-a, --align <arg> How entries should be aligned in a column.
Options: \"left\", \"right\", \"center\".
[default: left]
-c, --condense <arg> Limits the length of each field to the value
specified. If the field is UTF-8 encoded, then
<arg> refers to the number of code points.
Otherwise, it refers to the number of bytes.
Expand All @@ -43,9 +47,27 @@ struct Args {
flag_pad: usize,
flag_output: Option<String>,
flag_delimiter: Option<Delimiter>,
flag_align: Align,
flag_condense: Option<usize>,
}

#[derive(Deserialize, Clone, Copy)]
enum Align {
Left,
Right,
Center,
}

impl From<Align> for Alignment {
fn from(align: Align) -> Self {
match align {
Align::Left => Alignment::Left,
Align::Right => Alignment::Right,
Align::Center => Alignment::Center,
}
}
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
Expand All @@ -56,7 +78,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let tw = TabWriter::new(wconfig.io_writer()?)
.minwidth(args.flag_width)
.padding(args.flag_pad);
.padding(args.flag_pad)
.alignment(args.flag_align.into());
let mut wtr = wconfig.from_writer(tw);
let mut rdr = rconfig.reader()?;

Expand Down
36 changes: 36 additions & 0 deletions tests/test_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,39 @@ abcdefg a a
a abc z\
")
}

#[test]
fn table_right_align() {
let wrk = Workdir::new("table");
wrk.create("in.csv", data());

let mut cmd = wrk.command("table");
cmd.arg("--align");
cmd.arg("right");
cmd.arg("in.csv");

let got: String = wrk.stdout(&mut cmd);
assert_eq!(&*got, concat!(
" h1 h2 h3\n",
"abcdefg a a\n",
" a abc z",
));
}

#[test]
fn table_center_align() {
let wrk = Workdir::new("table");
wrk.create("in.csv", data());

let mut cmd = wrk.command("table");
cmd.arg("-a");
cmd.arg("center");
cmd.arg("in.csv");

let got: String = wrk.stdout(&mut cmd);
assert_eq!(&*got, concat!(
" h1 h2 h3\n",
"abcdefg a a\n",
" a abc z",
));
}

0 comments on commit 98f04c6

Please sign in to comment.