From 56f671f6151cae53abfc34cf3c1629784f1f7289 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Fri, 11 Nov 2022 20:28:21 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++++ src/main.rs | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..aad7987 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55edcf6c0bb319052dea84732cf99db461780fd5e8d3eb46ab6ff312ab31f197" + +[[package]] +name = "linediff" +version = "0.1.0" +dependencies = [ + "colored", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8123c8e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "linediff" +version = "0.1.0" +edition = "2021" + +[dependencies] +colored = "2.0.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..71bc3f7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,49 @@ +use colored::Colorize; +use std::{ + io::{stdin, BufRead}, + mem::swap, +}; + +fn main() { + let mut stdin = stdin().lock(); + + let mut prev_line = String::new(); + let mut next_line = String::new(); + while let Ok(n) = stdin.read_line(&mut next_line) { + if n == 0 { + break; + } + + if next_line.ends_with('\n') { + next_line.pop(); + } + + let mut chars1 = prev_line.chars(); + let mut chars2 = next_line.chars(); + + let mut buf = [0u8; 4]; + while let Some(c1) = chars1.next() { + if let Some(c2) = chars2.next() { + let s = c2.encode_utf8(&mut buf); + if c1 == c2 { + print!("{}", s); + } else { + print!("{}", s.yellow()); + } + } else { + let s = c1.encode_utf8(&mut buf); + print!("{}", s.red().strikethrough()); + } + } + + while let Some(c2) = chars2.next() { + let s = c2.encode_utf8(&mut buf); + print!("{}", s.green()); + } + + println!(); + + prev_line.clear(); + swap(&mut next_line, &mut prev_line); + } +}