From 96f5213264e805b3f1cbbd72c3b93f957f2b95dd Mon Sep 17 00:00:00 2001 From: unixtensor Date: Wed, 9 Apr 2025 19:43:23 -0300 Subject: [PATCH] diff view --- src/diff.rs | 40 ++++++++++++++++++---------------------- src/eval.rs | 3 ++- src/input.rs | 28 ++++++++++++++++++---------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 2ee3f4e..2acf67a 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,31 +1,33 @@ +use color_print::cformat; + pub struct Compare { pub right: String, pub left: String, - pub eval: String } -type Compared = Compare; +pub struct Compared { + pub right: String, + pub left: String, +} pub struct Comparer { - formatted_right: Vec, - formatted_left: Vec, - formatted_eval: Vec + formatted_right: String, + formatted_left: String } impl Comparer { pub const fn new() -> Self { Self { - formatted_right: Vec::new(), - formatted_left: Vec::new(), - formatted_eval: Vec::new(), + formatted_right: String::new(), + formatted_left: String::new(), } } fn format_compare(&mut self, left_char: char, right_char: char) { if left_char == right_char { - self.formatted_left.push(left_char.to_string()); - self.formatted_right.push(right_char.to_string()); + self.formatted_left.push(left_char); + self.formatted_right.push(right_char); } else { - self.formatted_left.push(color_print::cformat!("{left_char}")); - self.formatted_right.push(color_print::cformat!("{right_char}")); + self.formatted_left.push_str(&cformat!("{left_char}")); + self.formatted_right.push_str(&cformat!("{right_char}")); }; } @@ -35,18 +37,12 @@ impl Comparer { for (i, left_char) in compare.left.chars().enumerate() { match right_chars.get(i) { Some(right_char) => self.format_compare(left_char, *right_char), - None => { - compare.eval.chars().for_each(|char| { - // self.formatted_eval - }); - break - }, + None => break, } } Compared { - right: self.formatted_right.concat(), - left: self.formatted_left.concat(), - eval: self.formatted_eval.concat() + right: self.formatted_right.clone(), + left: self.formatted_left.clone() } } -} \ No newline at end of file +} diff --git a/src/eval.rs b/src/eval.rs index 0cde1c9..1f446ad 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,5 +1,6 @@ use std::num::ParseIntError; +#[allow(dead_code)] pub enum EvalError { LeftMissing, RightMissing, @@ -44,4 +45,4 @@ pub fn eval(expression: Vec<&str>) -> Result { "^" | "xor" => Ok(wrap_result(left ^ right)), str => Err(EvalError::InvalidOperator(str.to_owned())) } -} \ No newline at end of file +} diff --git a/src/input.rs b/src/input.rs index 282a029..81e8d5e 100644 --- a/src/input.rs +++ b/src/input.rs @@ -29,26 +29,34 @@ impl Out { match eval_error { EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"), EvalError::InvalidOperator(invalid_op) => println!("The operator {:?} is invalid.", invalid_op), + EvalError::LeftMissing => println!("Left side expression missing."), + EvalError::RightMissing => println!("Right side expression missing."), } } + fn padded(&self, diffed_left: &str, diffed_right: &str, diffed_eval: String) -> String { + let diffed_eval_chars = diffed_eval.chars().count(); + let mut char_counts = [diffed_left.chars().count(), diffed_right.chars().count(), diffed_eval_chars]; + char_counts.sort(); + let subtracted_count = char_counts.last().map_or(0, |biggest_len| { + *biggest_len-diffed_eval_chars + }); + format!("{}{diffed_eval}", "0".repeat(subtracted_count)) + } + pub fn evaled(&self, eval_out: Result) { eval_out.map_or_else(|e| self.error(e), |result| { - let eval_base2 = base::binary(result.eval); - let left = base::binary(result.input.left); - let right = base::binary(result.input.right); - let mut differ = diff::Comparer::new(); let diffed = differ.compare(Compare { - right: right.clone(), - left: left.clone(), - eval: eval_base2.clone(), + right: base::binary(result.input.right), + left: base::binary(result.input.left), }); + let padded_eval = self.padded(&diffed.left, &diffed.right, base::binary(result.eval)); println!("{}", diffed.left); println!("{}", diffed.right); - println!("{}", "-".repeat(eval_base2.chars().count())); - println!("{}", diffed.eval) + println!("{}", "-".repeat(padded_eval.chars().count())); + println!("{}", padded_eval) }) } } @@ -88,4 +96,4 @@ impl Bitfyre { } } } -} \ No newline at end of file +}