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

Add "--align" flag to table command. #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 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",
));
}