Skip to content

Commit cf51eb7

Browse files
committed
Allow printing a prettytable to anything that implements Write
For example, stderr instead of stdout. Depends on phsym/prettytable-rs#156
1 parent 2d52d1f commit cf51eb7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ license = "MIT"
1414
readme = "README.md"
1515
repository = "https://github.com/romankoblov/prettydiff"
1616
homepage = "https://github.com/romankoblov/prettydiff"
17+
rust-version = "1.70"
1718

1819
[dependencies]
1920
ansi_term.version = "0.12"
@@ -27,3 +28,6 @@ structopt.optional = true
2728
[features]
2829
cli = ["prettytable-rs", "structopt"]
2930
default = ["cli"]
31+
32+
[patch.crates-io]
33+
prettytable-rs = { git = "https://github.com/asomers/prettytable-rs.git", rev = "1e2c2d5c3" }

src/text.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ impl<'a> LineChangeset<'a> {
338338
}
339339

340340
#[cfg(feature = "prettytable-rs")]
341-
/// Prints side-by-side diff in table
342-
pub fn prettytable(&self) {
341+
fn prettytable_mktable(&self) -> prettytable::Table {
343342
let mut table = format_table::new();
344343
if let Some((old, new)) = &self.names {
345344
let mut header = vec![];
@@ -395,9 +394,25 @@ impl<'a> LineChangeset<'a> {
395394
table.add_row(row![old, new]);
396395
}
397396
}
397+
table
398+
}
399+
400+
#[cfg(feature = "prettytable-rs")]
401+
/// Prints side-by-side diff in table
402+
pub fn prettytable(&self) {
403+
let table = self.prettytable_mktable();
398404
table.printstd();
399405
}
400406

407+
#[cfg(feature = "prettytable-rs")]
408+
/// Write side-by-side diff in table to any Writer.
409+
pub fn write_prettytable<W>(&self, f: &mut W) -> std::io::Result<usize>
410+
where W: std::io::Write + std::io::IsTerminal
411+
{
412+
let table = self.prettytable_mktable();
413+
table.print_term(f)
414+
}
415+
401416
fn remove_color(&self, a: &str) -> String {
402417
Colour::Red.strikethrough().paint(a).to_string()
403418
}

0 commit comments

Comments
 (0)