add pretty line print with colors

This commit is contained in:
Denis-Cosmin Nutiu 2024-10-24 23:03:29 +03:00
parent 283efb0b1f
commit 14ae7c6bf0
4 changed files with 121 additions and 4 deletions

92
Cargo.lock generated
View file

@ -22,4 +22,96 @@ name = "ndiff"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"itertools", "itertools",
"termcolor",
] ]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View file

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
itertools = "0.13.0" itertools = {version = "0.13.0"}
termcolor = {version = "1.4.1" }

View file

@ -1,5 +1,6 @@
use std::fmt; use std::{fmt};
use std::fmt::Formatter; use std::fmt::{Debug, Formatter};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct MissingLineIndicator { pub(crate) struct MissingLineIndicator {
@ -26,6 +27,29 @@ pub(crate) enum Line {
DifferingLine(i32, String, String), DifferingLine(i32, String, String),
} }
impl Line {
/// Prints the line to stdout
pub fn print(&self) {
match self {
Line::MatchedLine(line_number, line) => {
println!(r#"{}. {}"#, line_number, line)
}
Line::DifferingLine(line_number, left_line, right_line) => {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
let _ = stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)));
println!(" - {}", left_line);
let _ = stdout.reset();
println!("{line_number}. ----");
let _ = stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red)));
println!(" + {}", right_line);
let _ = stdout.reset();
let _ = stdout.reset();
}
}
}
}
impl fmt::Display for Line { impl fmt::Display for Line {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self { match self {

View file

@ -35,7 +35,7 @@ fn main() {
let mut differences_counter: i32 = 0; let mut differences_counter: i32 = 0;
for line in lines.iter() { for line in lines.iter() {
print!("{}", line); line.print();
match line { match line {
Line::MatchedLine(_, _) => {} Line::MatchedLine(_, _) => {}
Line::DifferingLine(_, _, _) => { Line::DifferingLine(_, _, _) => {