Skip to content

Commit

Permalink
Allow printing a prettytable to anything that implements Write
Browse files Browse the repository at this point in the history
For example, stderr instead of stdout.

Depends on phsym/prettytable-rs#156
  • Loading branch information
asomers committed Sep 10, 2023
1 parent 2d52d1f commit cf51eb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license = "MIT"
readme = "README.md"
repository = "https://github.com/romankoblov/prettydiff"
homepage = "https://github.com/romankoblov/prettydiff"
rust-version = "1.70"

[dependencies]
ansi_term.version = "0.12"
Expand All @@ -27,3 +28,6 @@ structopt.optional = true
[features]
cli = ["prettytable-rs", "structopt"]
default = ["cli"]

[patch.crates-io]
prettytable-rs = { git = "https://github.com/asomers/prettytable-rs.git", rev = "1e2c2d5c3" }
19 changes: 17 additions & 2 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ impl<'a> LineChangeset<'a> {
}

#[cfg(feature = "prettytable-rs")]
/// Prints side-by-side diff in table
pub fn prettytable(&self) {
fn prettytable_mktable(&self) -> prettytable::Table {
let mut table = format_table::new();
if let Some((old, new)) = &self.names {
let mut header = vec![];
Expand Down Expand Up @@ -395,9 +394,25 @@ impl<'a> LineChangeset<'a> {
table.add_row(row![old, new]);
}
}
table
}

#[cfg(feature = "prettytable-rs")]
/// Prints side-by-side diff in table
pub fn prettytable(&self) {
let table = self.prettytable_mktable();
table.printstd();
}

#[cfg(feature = "prettytable-rs")]
/// Write side-by-side diff in table to any Writer.
pub fn write_prettytable<W>(&self, f: &mut W) -> std::io::Result<usize>
where W: std::io::Write + std::io::IsTerminal
{
let table = self.prettytable_mktable();
table.print_term(f)
}

fn remove_color(&self, a: &str) -> String {
Colour::Red.strikethrough().paint(a).to_string()
}
Expand Down

0 comments on commit cf51eb7

Please sign in to comment.