Skip to content

Commit

Permalink
Use atty to omit text coloring and decorations (#95)
Browse files Browse the repository at this point in the history
* Use atty to omit text coloring and decorations when tidy-viewer is not outputting directly to tty

* Fix formatting issue in main.rs file
  • Loading branch information
rlewicki authored Oct 10, 2021
1 parent 418a6ed commit 1206937
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 54 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ priority = "optional"
section = "utility"

[dependencies]
atty = "0.2.14"
console = "0.14.1"
crossterm = "0.20.0"
csv = "1.1.6"
Expand Down
152 changes: 98 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn main() {
let is_title_defined = opt.title.chars().count() > 0;
let is_footer_defined = opt.title.chars().count() > 0;
let is_row_display_defined = !(opt.row_display == 25);
let is_tty = atty::is(atty::Stream::Stdout);

let title_option = match (&config, is_title_defined) {
(Some(x), false) => &x.title,
Expand Down Expand Up @@ -376,35 +377,47 @@ fn main() {
let meta_text = "tv dim:";
let div = "x";
print!("{: <6}", "");
println!(
"{} {} {} {}",
meta_text.truecolor(meta_color[0], meta_color[1], meta_color[2]),
(rows_in_file - 1).truecolor(meta_color[0], meta_color[1], meta_color[2]),
div.truecolor(meta_color[0], meta_color[1], meta_color[2]),
(cols).truecolor(meta_color[0], meta_color[1], meta_color[2]),
);
if is_tty {
println!(
"{} {} {} {}",
meta_text.truecolor(meta_color[0], meta_color[1], meta_color[2]),
(rows_in_file - 1).truecolor(meta_color[0], meta_color[1], meta_color[2]),
div.truecolor(meta_color[0], meta_color[1], meta_color[2]),
(cols).truecolor(meta_color[0], meta_color[1], meta_color[2]),
);
} else {
println!("{} {} {} {}", meta_text, rows_in_file - 1, div, cols);
}
// title
if !datatype::is_na(&title_option.clone()) {
print!("{: <6}", "");
println!(
"{}",
title_option
.truecolor(meta_color[0], meta_color[1], meta_color[2])
.underline()
.bold()
);
if is_tty {
println!(
"{}",
title_option
.truecolor(meta_color[0], meta_color[1], meta_color[2])
.underline()
.bold()
);
} else {
println!("{}", title_option);
}
}

// header
print!("{: <6}", "");
//for col in 0..cols {
for col in 0..num_cols_to_print {
let text = vp[0].get(col).unwrap().to_string();
print!(
"{}",
text.truecolor(header_color[0], header_color[1], header_color[2])
.bold()
);
if is_tty {
print!(
"{}",
text.truecolor(header_color[0], header_color[1], header_color[2])
.bold()
);
} else {
print!("{}", text);
}
}
//println!();
// datatypes
Expand All @@ -422,20 +435,28 @@ fn main() {
.take(rows)
.skip(1)
.for_each(|(i, row)| {
print!(
"{: <6}",
i.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
//for col in 0..cols {
row.iter().take(num_cols_to_print).for_each(|col| {
if is_tty {
print!(
"{}",
if datatype::is_na_string_padded(col) {
col.truecolor(na_color[0], na_color[1], na_color[2])
} else {
col.truecolor(std_color[0], std_color[1], std_color[2])
}
"{: <6}",
i.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
} else {
print!("{: <6}", i);
}
//for col in 0..cols {
row.iter().take(num_cols_to_print).for_each(|col| {
if is_tty {
print!(
"{}",
if datatype::is_na_string_padded(col) {
col.truecolor(na_color[0], na_color[1], na_color[2])
} else {
col.truecolor(std_color[0], std_color[1], std_color[2])
}
);
} else {
print!("{}", col);
}
});
println!();
});
Expand All @@ -444,10 +465,14 @@ fn main() {

if rows_remaining > 0 {
print!("{: <6}", "");
print!(
"{}",
row_remaining_text.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
if is_tty {
print!(
"{}",
row_remaining_text.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
} else {
print!("{}", row_remaining_text);
}
//println!("num_cols_to_print {:?} cols {:?}", num_cols_to_print, cols);
let extra_cols_to_mention = num_cols_to_print;
let remainder_cols = cols - extra_cols_to_mention;
Expand All @@ -456,26 +481,41 @@ fn main() {
let meta_text_var = "more variables";
let meta_text_comma = ",";
let meta_text_colon = ":";
print!(
" {} {} {}{}",
meta_text_and.truecolor(meta_color[0], meta_color[1], meta_color[2]),
remainder_cols.truecolor(meta_color[0], meta_color[1], meta_color[2]),
meta_text_var.truecolor(meta_color[0], meta_color[1], meta_color[2]),
meta_text_colon.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
for col in extra_cols_to_mention..cols {
let text = rdr[0].get(col).unwrap();
if is_tty {
print!(
" {} {} {}{}",
meta_text_and.truecolor(meta_color[0], meta_color[1], meta_color[2]),
remainder_cols.truecolor(meta_color[0], meta_color[1], meta_color[2]),
meta_text_var.truecolor(meta_color[0], meta_color[1], meta_color[2]),
meta_text_colon.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
} else {
print!(
" {}",
text.truecolor(meta_color[0], meta_color[1], meta_color[2])
" {} {} {}{}",
meta_text_and, remainder_cols, meta_text_var, meta_text_colon
);
}
for col in extra_cols_to_mention..cols {
let text = rdr[0].get(col).unwrap();
if is_tty {
print!(
" {}",
text.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
} else {
print!(" {}", text);
}

// The last column mentioned in foot should not be followed by a comma
if col + 1 < cols {
print!(
"{}",
meta_text_comma.truecolor(meta_color[0], meta_color[1], meta_color[2])
)
if is_tty {
print!(
"{}",
meta_text_comma.truecolor(meta_color[0], meta_color[1], meta_color[2])
)
} else {
print!("{}", meta_text_comma)
}
}
}
}
Expand All @@ -484,10 +524,14 @@ fn main() {
// footer
if !datatype::is_na(&footer_option.clone()) {
println!("{: <6}", "");
println!(
"{}",
footer_option.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
if is_tty {
println!(
"{}",
footer_option.truecolor(meta_color[0], meta_color[1], meta_color[2])
);
} else {
println!("{}", footer_option);
}
}

println!();
Expand Down

0 comments on commit 1206937

Please sign in to comment.