From 14ae7c6bf0399617a1d9a512856e4ed888af43b9 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Thu, 24 Oct 2024 23:03:29 +0300 Subject: [PATCH] add pretty line print with colors --- Cargo.lock | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/line.rs | 28 ++++++++++++++-- src/main.rs | 2 +- 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b27437d..40c63b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,4 +22,96 @@ name = "ndiff" version = "0.1.0" dependencies = [ "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" diff --git a/Cargo.toml b/Cargo.toml index c0750fc..d57458f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -itertools = "0.13.0" \ No newline at end of file +itertools = {version = "0.13.0"} +termcolor = {version = "1.4.1" } \ No newline at end of file diff --git a/src/line.rs b/src/line.rs index 9a1fc06..771da6f 100644 --- a/src/line.rs +++ b/src/line.rs @@ -1,5 +1,6 @@ -use std::fmt; -use std::fmt::Formatter; +use std::{fmt}; +use std::fmt::{Debug, Formatter}; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; #[derive(Debug)] pub(crate) struct MissingLineIndicator { @@ -26,6 +27,29 @@ pub(crate) enum Line { 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 { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { diff --git a/src/main.rs b/src/main.rs index 190421f..fb2a3a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ fn main() { let mut differences_counter: i32 = 0; for line in lines.iter() { - print!("{}", line); + line.print(); match line { Line::MatchedLine(_, _) => {} Line::DifferingLine(_, _, _) => {