diff view

This commit is contained in:
rhpidfyre 2025-04-09 19:43:23 -03:00
parent 1049f9bcf6
commit 96f5213264
3 changed files with 38 additions and 33 deletions

View File

@ -1,31 +1,33 @@
use color_print::cformat;
pub struct Compare { pub struct Compare {
pub right: String, pub right: String,
pub left: String, pub left: String,
pub eval: String
} }
type Compared = Compare; pub struct Compared {
pub right: String,
pub left: String,
}
pub struct Comparer { pub struct Comparer {
formatted_right: Vec<String>, formatted_right: String,
formatted_left: Vec<String>, formatted_left: String
formatted_eval: Vec<String>
} }
impl Comparer { impl Comparer {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
formatted_right: Vec::new(), formatted_right: String::new(),
formatted_left: Vec::new(), formatted_left: String::new(),
formatted_eval: Vec::new(),
} }
} }
fn format_compare(&mut self, left_char: char, right_char: char) { fn format_compare(&mut self, left_char: char, right_char: char) {
if left_char == right_char { if left_char == right_char {
self.formatted_left.push(left_char.to_string()); self.formatted_left.push(left_char);
self.formatted_right.push(right_char.to_string()); self.formatted_right.push(right_char);
} else { } else {
self.formatted_left.push(color_print::cformat!("<r>{left_char}</>")); self.formatted_left.push_str(&cformat!("<r>{left_char}</>"));
self.formatted_right.push(color_print::cformat!("<r>{right_char}</>")); self.formatted_right.push_str(&cformat!("<r>{right_char}</>"));
}; };
} }
@ -35,18 +37,12 @@ impl Comparer {
for (i, left_char) in compare.left.chars().enumerate() { for (i, left_char) in compare.left.chars().enumerate() {
match right_chars.get(i) { match right_chars.get(i) {
Some(right_char) => self.format_compare(left_char, *right_char), Some(right_char) => self.format_compare(left_char, *right_char),
None => { None => break,
compare.eval.chars().for_each(|char| {
// self.formatted_eval
});
break
},
} }
} }
Compared { Compared {
right: self.formatted_right.concat(), right: self.formatted_right.clone(),
left: self.formatted_left.concat(), left: self.formatted_left.clone()
eval: self.formatted_eval.concat()
} }
} }
} }

View File

@ -1,5 +1,6 @@
use std::num::ParseIntError; use std::num::ParseIntError;
#[allow(dead_code)]
pub enum EvalError { pub enum EvalError {
LeftMissing, LeftMissing,
RightMissing, RightMissing,
@ -44,4 +45,4 @@ pub fn eval(expression: Vec<&str>) -> Result<EvalResult, EvalError> {
"^" | "xor" => Ok(wrap_result(left ^ right)), "^" | "xor" => Ok(wrap_result(left ^ right)),
str => Err(EvalError::InvalidOperator(str.to_owned())) str => Err(EvalError::InvalidOperator(str.to_owned()))
} }
} }

View File

@ -29,26 +29,34 @@ impl Out {
match eval_error { match eval_error {
EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"), EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"),
EvalError::InvalidOperator(invalid_op) => println!("The operator {:?} is invalid.", invalid_op), 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<EvalResult, EvalError>) { pub fn evaled(&self, eval_out: Result<EvalResult, EvalError>) {
eval_out.map_or_else(|e| self.error(e), |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 mut differ = diff::Comparer::new();
let diffed = differ.compare(Compare { let diffed = differ.compare(Compare {
right: right.clone(), right: base::binary(result.input.right),
left: left.clone(), left: base::binary(result.input.left),
eval: eval_base2.clone(),
}); });
let padded_eval = self.padded(&diffed.left, &diffed.right, base::binary(result.eval));
println!("{}", diffed.left); println!("{}", diffed.left);
println!("{}", diffed.right); println!("{}", diffed.right);
println!("{}", "-".repeat(eval_base2.chars().count())); println!("{}", "-".repeat(padded_eval.chars().count()));
println!("{}", diffed.eval) println!("{}", padded_eval)
}) })
} }
} }
@ -88,4 +96,4 @@ impl Bitfyre {
} }
} }
} }
} }