From cf51eb7261935e375fd273c31a6ca65844a9f013 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 9 Sep 2023 20:46:43 -0600 Subject: [PATCH] Allow printing a prettytable to anything that implements Write For example, stderr instead of stdout. Depends on https://github.com/phsym/prettytable-rs/pull/156 --- Cargo.toml | 4 ++++ src/text.rs | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e047a0a..7da2fc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" } diff --git a/src/text.rs b/src/text.rs index 0d5fc03..45cddcc 100644 --- a/src/text.rs +++ b/src/text.rs @@ -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![]; @@ -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(&self, f: &mut W) -> std::io::Result + 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() }