check out the different versions of formatter that makes COLOR omg so good
This commit is contained in:
1 parent
985970453c
commit
bcea06ddc2
6 files changed
+419
-165
No files matched your search
+1
-1
Submodule lda_ansi updated: de4c23020d...f59285d3c9.
@@ -0,0 +1,157 @@
|
||||
//! use copilot;
|
||||
|
||||
use lda_ansi::{ansi_backbone::Style as StyleConstruct, ansi_colors_2::{ansi_color_interface::ColorContext as _, Color}, ansi_styles::Style, AnsiStyles};
|
||||
use lda_strdist::{levenstein_distance, EditCost, EditOperation};
|
||||
|
||||
// you can tell right away what code this is
|
||||
struct FilterConfig<F, G>
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
should_be_colored: F,
|
||||
extract_char: G,
|
||||
}
|
||||
|
||||
impl<F, G> FilterConfig<F, G>
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
fn new(should_be_colored: F, extract_char: G) -> Self {
|
||||
Self {
|
||||
should_be_colored,
|
||||
extract_char,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn split_operations<F, G>(
|
||||
f_ops: &Vec<EditOperation>,
|
||||
config: &FilterConfig<F, G>,
|
||||
color: StyleConstruct,
|
||||
reset: StyleConstruct,
|
||||
) -> String
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
let mut result = Vec::new();
|
||||
let mut current_group = AnsiStyles::new();
|
||||
let mut last_group_was_colored: bool;
|
||||
if let Some(first_op) = f_ops.first() {
|
||||
last_group_was_colored = (config.should_be_colored)(first_op);
|
||||
if last_group_was_colored {
|
||||
current_group.add_style(&color);
|
||||
};
|
||||
} else {
|
||||
return String::new();
|
||||
}
|
||||
for op in f_ops {
|
||||
let this_should_be_colored = (config.should_be_colored)(op);
|
||||
if this_should_be_colored {
|
||||
if !current_group.text.is_empty() && !last_group_was_colored {
|
||||
result.push(current_group);
|
||||
current_group = AnsiStyles::new();
|
||||
current_group.add_style(&color);
|
||||
}
|
||||
if let Some(c) = (config.extract_char)(op) {
|
||||
current_group.text.push(c);
|
||||
}
|
||||
last_group_was_colored = this_should_be_colored;
|
||||
} else {
|
||||
if last_group_was_colored {
|
||||
result.push(current_group);
|
||||
current_group = AnsiStyles::new();
|
||||
current_group.add_style(&reset);
|
||||
}
|
||||
if let EditOperation::Match { char: c } = op {
|
||||
current_group.text.push(*c);
|
||||
}
|
||||
last_group_was_colored = this_should_be_colored;
|
||||
}
|
||||
}
|
||||
|
||||
if !current_group.text.is_empty() {
|
||||
result.push(current_group);
|
||||
}
|
||||
|
||||
result.into_iter().map(String::from).collect()
|
||||
}
|
||||
|
||||
fn filter_from(f_ops: &Vec<EditOperation>) -> String {
|
||||
let red = StyleConstruct::new().color(Color::Red.fg()).to_owned();
|
||||
let reset = StyleConstruct::new().style(Style::Reset).to_owned();
|
||||
|
||||
let config = FilterConfig::new(
|
||||
|op| {
|
||||
matches!(
|
||||
op,
|
||||
EditOperation::Delete { .. } | EditOperation::Substitute { .. }
|
||||
)
|
||||
},
|
||||
|op| match op {
|
||||
EditOperation::Delete { char } => Some(*char),
|
||||
EditOperation::Substitute { from, .. } => Some(*from),
|
||||
_ => None,
|
||||
},
|
||||
);
|
||||
|
||||
split_operations(f_ops, &config, red, reset)
|
||||
}
|
||||
|
||||
fn filter_to(f_ops: &Vec<EditOperation>) -> String {
|
||||
let blue = StyleConstruct::new().color(Color::Blue.fg()).to_owned();
|
||||
let reset = StyleConstruct::new().style(Style::Reset).to_owned();
|
||||
|
||||
let config = FilterConfig::new(
|
||||
|op| {
|
||||
matches!(
|
||||
op,
|
||||
EditOperation::Insert { .. } | EditOperation::Substitute { .. }
|
||||
)
|
||||
},
|
||||
|op| match op {
|
||||
EditOperation::Insert { char } => Some(*char),
|
||||
EditOperation::Substitute { to, .. } => Some(*to),
|
||||
_ => None,
|
||||
},
|
||||
);
|
||||
|
||||
split_operations(f_ops, &config, blue, reset)
|
||||
}
|
||||
|
||||
/// another way you do it, with chatgpt.
|
||||
pub fn formatted_comparison(str1: &str, str2: &str) -> (String, String) {
|
||||
let (_, ops) = levenstein_distance(str1, str2, &EditCost::default());
|
||||
let to = filter_to(&ops);
|
||||
let from = filter_from(&ops);
|
||||
(from, to)
|
||||
}
|
||||
|
||||
// struct FilterConfig {
|
||||
// should_be_colored: Box<dyn Fn(&EditOperation) -> bool>,
|
||||
// extract_char: Box<dyn Fn(&EditOperation) -> Option<char>>,
|
||||
// }
|
||||
|
||||
// impl FilterConfig {
|
||||
// fn new<F, G>(should_be_colored: F, extract_char: G) -> Self
|
||||
// where
|
||||
// F: 'static + Fn(&EditOperation) -> bool,
|
||||
// G: 'static + Fn(&EditOperation) -> Option<char>,
|
||||
// {
|
||||
// Self {
|
||||
// should_be_colored: Box::new(should_be_colored),
|
||||
// extract_char: Box::new(extract_char),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn split_operations(
|
||||
// f_ops: &Vec<EditOperation>,
|
||||
// config: &FilterConfig,
|
||||
// color: StyleConstruct,
|
||||
// reset: StyleConstruct,
|
||||
// ) -> String {
|
||||
// ...
|
||||
// }
|
||||
@@ -0,0 +1,224 @@
|
||||
// i love ais idea
|
||||
|
||||
use lda_ansi::{
|
||||
ansi_backbone::Style as StyleConstruct,
|
||||
ansi_colors_2::{ansi_color_interface::ColorContext as _, Color},
|
||||
ansi_styles::Style,
|
||||
AnsiStyles,
|
||||
};
|
||||
use lda_strdist::{levenstein_distance, EditCost, EditOperation};
|
||||
|
||||
pub enum IsColored {
|
||||
True(char),
|
||||
False(char),
|
||||
None,
|
||||
}
|
||||
|
||||
fn is_from_colored(op: &EditOperation) -> IsColored {
|
||||
use EditOperation::*;
|
||||
use IsColored::*;
|
||||
match op {
|
||||
Delete { char } | Substitute { from: char, to: _ } => True(*char),
|
||||
Insert { char: _ } => None,
|
||||
Match { char } => False(*char),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_to_colored(op: &EditOperation) -> IsColored {
|
||||
use EditOperation::*;
|
||||
use IsColored::*;
|
||||
match op {
|
||||
Insert { char } | Substitute { from: _, to: char } => True(*char),
|
||||
Delete { char: _ } => None,
|
||||
Match { char } => False(*char),
|
||||
}
|
||||
}
|
||||
|
||||
// and we use ai to refactor code again
|
||||
fn update_colored_state<'a>(
|
||||
is_current_colored: IsColored,
|
||||
is_last_colored: &mut IsColored,
|
||||
current_color: &mut AnsiStyles,
|
||||
text: &mut Vec<AnsiStyles>,
|
||||
color: &'a StyleConstruct,
|
||||
reset: &'a StyleConstruct,
|
||||
) {
|
||||
match (&is_current_colored, &is_last_colored) {
|
||||
(IsColored::False(c), IsColored::True(_)) | (IsColored::True(c), IsColored::False(_)) => {
|
||||
match &is_last_colored {
|
||||
IsColored::False(_) => {
|
||||
current_color.add_style(reset);
|
||||
}
|
||||
IsColored::True(_) => {
|
||||
current_color.add_style(color);
|
||||
}
|
||||
IsColored::None => {}
|
||||
};
|
||||
text.push(std::mem::replace(
|
||||
current_color,
|
||||
AnsiStyles::init(&c.to_string()),
|
||||
)); // Move current state to `text` and initialize new state
|
||||
*is_last_colored = is_current_colored;
|
||||
}
|
||||
(IsColored::True(c) | IsColored::False(c), _) => {
|
||||
current_color.text.push(*c); // Append character to the current style
|
||||
*is_last_colored = is_current_colored;
|
||||
}
|
||||
(IsColored::None, _) => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// compare2_format
|
||||
pub fn compare2_format(str1: &str, str2: &str) -> (String, String) {
|
||||
let (_, ops) = levenstein_distance(str1, str2, &EditCost::default());
|
||||
let blue_text = StyleConstruct::new().color(Color::Blue.fg()).to_owned();
|
||||
let red_text = StyleConstruct::new().color(Color::Red.fg()).to_owned();
|
||||
let reset = StyleConstruct::new().style(Style::Reset).to_owned();
|
||||
|
||||
let mut froms: Vec<AnsiStyles> = Vec::new();
|
||||
let mut tos: Vec<AnsiStyles> = Vec::new();
|
||||
let mut current_from = AnsiStyles::new();
|
||||
let mut current_to = AnsiStyles::new();
|
||||
let mut last_from_also_colored = IsColored::None;
|
||||
let mut last_to_also_colored = IsColored::None;
|
||||
|
||||
for op in ops {
|
||||
let is_current_from_colored = is_from_colored(&op);
|
||||
update_colored_state(
|
||||
is_current_from_colored,
|
||||
&mut last_from_also_colored,
|
||||
&mut current_from,
|
||||
&mut froms,
|
||||
&red_text,
|
||||
&reset,
|
||||
);
|
||||
|
||||
let is_current_to_colored = is_to_colored(&op);
|
||||
update_colored_state(
|
||||
is_current_to_colored,
|
||||
&mut last_to_also_colored,
|
||||
&mut current_to,
|
||||
&mut tos,
|
||||
&blue_text,
|
||||
&reset,
|
||||
);
|
||||
}
|
||||
|
||||
match &last_from_also_colored {
|
||||
&IsColored::False(_) => {
|
||||
current_from.add_style(&reset);
|
||||
}
|
||||
&IsColored::True(_) => {
|
||||
current_from.add_style(&red_text);
|
||||
}
|
||||
&IsColored::None => (),
|
||||
}
|
||||
froms.push(current_from);
|
||||
match &last_to_also_colored {
|
||||
&IsColored::False(_) => {
|
||||
current_to.add_style(&reset);
|
||||
}
|
||||
&IsColored::True(_) => {
|
||||
current_to.add_style(&blue_text);
|
||||
}
|
||||
&IsColored::None => (),
|
||||
}
|
||||
tos.push(current_to);
|
||||
let first_str: String = froms.into_iter().map(String::from).collect();
|
||||
let second_str: String = tos.into_iter().map(String::from).collect();
|
||||
(first_str, second_str)
|
||||
}
|
||||
|
||||
pub fn compare_mini_format<T>(
|
||||
// str: &str,
|
||||
operations: &Vec<EditOperation>,
|
||||
operation_is_colored: T,
|
||||
color: &StyleConstruct,
|
||||
color_false: &StyleConstruct,
|
||||
) -> String where
|
||||
T: Fn(&EditOperation) -> IsColored,
|
||||
{
|
||||
let mut text: Vec<AnsiStyles> = Vec::new();
|
||||
let mut current_color = AnsiStyles::new();
|
||||
let mut last_operation_is_colored = IsColored::None;
|
||||
for op in operations {
|
||||
update_colored_state(
|
||||
operation_is_colored(op),
|
||||
&mut last_operation_is_colored,
|
||||
&mut current_color,
|
||||
&mut text,
|
||||
color,
|
||||
color_false,
|
||||
);
|
||||
};
|
||||
match &last_operation_is_colored {
|
||||
&IsColored::False(_) => {
|
||||
current_color.add_style(color_false);
|
||||
}
|
||||
&IsColored::True(_) => {
|
||||
current_color.add_style(color);
|
||||
}
|
||||
&IsColored::None => (),
|
||||
}
|
||||
text.push(current_color);
|
||||
text.into_iter().map(String::from).collect()
|
||||
}
|
||||
|
||||
// for op in ops {
|
||||
// use IsColored::*;
|
||||
// let is_current_from_colored = is_from_colored(&op);
|
||||
// match (&is_current_from_colored, &last_from_also_colored) {
|
||||
// (False(c), True(_)) | (True(c), False(_)) => {
|
||||
// match &last_from_also_colored {
|
||||
// False(_) => {
|
||||
// current_from.add_style(&reset);
|
||||
// }
|
||||
// True(_) => {
|
||||
// current_from.add_style(&red_text);
|
||||
// }
|
||||
// None => (),
|
||||
// }
|
||||
// froms.push(current_from);
|
||||
// current_from = AnsiStyles::init(&c.to_string());
|
||||
// last_from_also_colored = is_current_from_colored;
|
||||
// }
|
||||
// (True(c) | False(c), _) => {
|
||||
// current_from.text.push(*c);
|
||||
// last_from_also_colored = is_current_from_colored;
|
||||
// }
|
||||
// (None, _) => {}
|
||||
// };
|
||||
|
||||
// let is_current_to_colored = is_to_colored(&op);
|
||||
// match (&is_current_to_colored, &last_to_also_colored) {
|
||||
// (False(c), True(_)) | (True(c), False(_)) => {
|
||||
// match &last_to_also_colored {
|
||||
// False(_) => {
|
||||
// current_to.add_style(&reset);
|
||||
// }
|
||||
// True(_) => {
|
||||
// current_to.add_style(&blue_text);
|
||||
// }
|
||||
// None => (),
|
||||
// }
|
||||
// tos.push(current_to);
|
||||
// current_to = AnsiStyles::init(&c.to_string());
|
||||
// last_to_also_colored = is_current_to_colored;
|
||||
// }
|
||||
// (True(c) | False(c), _) => {
|
||||
// current_to.text.push(*c);
|
||||
// last_to_also_colored = is_current_to_colored;
|
||||
// }
|
||||
// (None, _) => {}
|
||||
// };
|
||||
|
||||
// // last_to_also_colored = is_current_to_colored;
|
||||
// // CHOOSE_YOUR_XAS(
|
||||
// // &is_current_to_colored,
|
||||
// // &mut last_to_also_colored,
|
||||
// // &mut current_to,
|
||||
// // &mut tos,
|
||||
// // &blue_text,
|
||||
// // &reset,
|
||||
// // );
|
||||
// }
|
||||
@@ -1,6 +1,7 @@
|
||||
#![allow(unused_variables)]
|
||||
// #![allow(unused_variables)]
|
||||
|
||||
mod test;
|
||||
|
||||
use lda_strdist::{levenstein_distance, EditCost, EditOperation};
|
||||
|
||||
use lda_ansi::{
|
||||
@@ -9,7 +10,8 @@ use lda_ansi::{
|
||||
ansi_styles::Style,
|
||||
AnsiStyles,
|
||||
};
|
||||
|
||||
pub mod formatter;
|
||||
pub mod formatter2;
|
||||
/// produce new thing with lint, inefficient, ass
|
||||
///
|
||||
/// not by chatgpt
|
||||
@@ -98,156 +100,3 @@ pub fn compare_format(str1: &str, str2: &str) -> (String, String) {
|
||||
let second_str: String = second_vec.into_iter().map(String::from).collect();
|
||||
(first_str, second_str)
|
||||
}
|
||||
|
||||
// you can tell right away what code this is
|
||||
struct FilterConfig<F, G>
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
should_be_colored: F,
|
||||
extract_char: G,
|
||||
}
|
||||
|
||||
impl<F, G> FilterConfig<F, G>
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
fn new(should_be_colored: F, extract_char: G) -> Self {
|
||||
Self {
|
||||
should_be_colored,
|
||||
extract_char,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn split_operations<F, G>(
|
||||
f_ops: &Vec<EditOperation>,
|
||||
config: &FilterConfig<F, G>,
|
||||
color: StyleConstruct,
|
||||
reset: StyleConstruct,
|
||||
) -> String
|
||||
where
|
||||
F: Fn(&EditOperation) -> bool,
|
||||
G: Fn(&EditOperation) -> Option<char>,
|
||||
{
|
||||
let mut result = Vec::new();
|
||||
let mut current_group = AnsiStyles::new();
|
||||
let mut last_group_was_colored: bool;
|
||||
if let Some(first_op) = f_ops.first() {
|
||||
last_group_was_colored = (config.should_be_colored)(first_op);
|
||||
if last_group_was_colored {
|
||||
current_group.add_style(&color);
|
||||
};
|
||||
} else {
|
||||
return String::new();
|
||||
}
|
||||
for op in f_ops {
|
||||
let this_should_be_colored = (config.should_be_colored)(op);
|
||||
if this_should_be_colored {
|
||||
if !current_group.text.is_empty() && !last_group_was_colored {
|
||||
result.push(current_group);
|
||||
current_group = AnsiStyles::new();
|
||||
current_group.add_style(&color);
|
||||
}
|
||||
if let Some(c) = (config.extract_char)(op) {
|
||||
current_group.text.push(c);
|
||||
}
|
||||
last_group_was_colored = this_should_be_colored;
|
||||
} else {
|
||||
if last_group_was_colored {
|
||||
result.push(current_group);
|
||||
current_group = AnsiStyles::new();
|
||||
current_group.add_style(&reset);
|
||||
}
|
||||
if let EditOperation::Match { char: c } = op {
|
||||
current_group.text.push(*c);
|
||||
}
|
||||
last_group_was_colored = this_should_be_colored;
|
||||
}
|
||||
}
|
||||
|
||||
if !current_group.text.is_empty() {
|
||||
result.push(current_group);
|
||||
}
|
||||
|
||||
result.into_iter().map(String::from).collect()
|
||||
}
|
||||
|
||||
fn filter_from(f_ops: &Vec<EditOperation>) -> String {
|
||||
let red = StyleConstruct::new().color(Color::Red.fg()).to_owned();
|
||||
let reset = StyleConstruct::new().style(Style::Reset).to_owned();
|
||||
|
||||
let config = FilterConfig::new(
|
||||
|op| {
|
||||
matches!(
|
||||
op,
|
||||
EditOperation::Delete { .. } | EditOperation::Substitute { .. }
|
||||
)
|
||||
},
|
||||
|op| match op {
|
||||
EditOperation::Delete { char } => Some(*char),
|
||||
EditOperation::Substitute { from, .. } => Some(*from),
|
||||
_ => None,
|
||||
},
|
||||
);
|
||||
|
||||
split_operations(f_ops, &config, red, reset)
|
||||
}
|
||||
|
||||
fn filter_to(f_ops: &Vec<EditOperation>) -> String {
|
||||
let blue = StyleConstruct::new().color(Color::Blue.fg()).to_owned();
|
||||
let reset = StyleConstruct::new().style(Style::Reset).to_owned();
|
||||
|
||||
let config = FilterConfig::new(
|
||||
|op| {
|
||||
matches!(
|
||||
op,
|
||||
EditOperation::Insert { .. } | EditOperation::Substitute { .. }
|
||||
)
|
||||
},
|
||||
|op| match op {
|
||||
EditOperation::Insert { char } => Some(*char),
|
||||
EditOperation::Substitute { to, .. } => Some(*to),
|
||||
_ => None,
|
||||
},
|
||||
);
|
||||
|
||||
split_operations(f_ops, &config, blue, reset)
|
||||
}
|
||||
|
||||
/// another way you do it, with chatgpt.
|
||||
pub fn compare2_format(str1: &str, str2: &str) -> (String, String) {
|
||||
let (_, ops) = levenstein_distance(str1, str2, &EditCost::default());
|
||||
let to = filter_to(&ops);
|
||||
let from = filter_from(&ops);
|
||||
(from, to)
|
||||
}
|
||||
|
||||
// struct FilterConfig {
|
||||
// should_be_colored: Box<dyn Fn(&EditOperation) -> bool>,
|
||||
// extract_char: Box<dyn Fn(&EditOperation) -> Option<char>>,
|
||||
// }
|
||||
|
||||
// impl FilterConfig {
|
||||
// fn new<F, G>(should_be_colored: F, extract_char: G) -> Self
|
||||
// where
|
||||
// F: 'static + Fn(&EditOperation) -> bool,
|
||||
// G: 'static + Fn(&EditOperation) -> Option<char>,
|
||||
// {
|
||||
// Self {
|
||||
// should_be_colored: Box::new(should_be_colored),
|
||||
// extract_char: Box::new(extract_char),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn split_operations(
|
||||
// f_ops: &Vec<EditOperation>,
|
||||
// config: &FilterConfig,
|
||||
// color: StyleConstruct,
|
||||
// reset: StyleConstruct,
|
||||
// ) -> String {
|
||||
// ...
|
||||
// }
|
||||
@@ -1,17 +1,41 @@
|
||||
#![allow(unused)] // SHUT UP
|
||||
// #![allow(unused)] // SHUT UP
|
||||
|
||||
use lda_ansi::{ansi_backbone::Style as StyleConstruct, ansi_styles::Style};
|
||||
use lda_sd_visualizer::{compare2_format, compare_format};
|
||||
use lda_sd_visualizer::{
|
||||
compare_format, formatter::formatted_comparison, formatter2::compare2_format,
|
||||
};
|
||||
use lda_strdist::{levenstein_distance, wrapper_::EditOperationVec, EditCost};
|
||||
|
||||
fn main() {
|
||||
let str1 = "this is similar";
|
||||
let str2 = "is this important";
|
||||
let (_, uuh) = levenstein_distance(str1, str2, &EditCost::default());
|
||||
let inp1 = "this is similar";
|
||||
let inp2 = "is this important";
|
||||
let (_, uuh) = levenstein_distance(inp1, inp2, &EditCost::default());
|
||||
println!("{}", EditOperationVec(&uuh));
|
||||
let (str1, str2) = compare2_format(str1, str2 );
|
||||
println!("{str1} {0}changed to {str2}{0}", StyleConstruct::new().style(Style::Reset));
|
||||
println!("{str1:#?} and {str2:#?}");
|
||||
|
||||
{
|
||||
let (str1, str2) = compare_format(inp1, inp2);
|
||||
println!(
|
||||
"{str1} {0}to {str2}{0}",
|
||||
StyleConstruct::new().style(Style::Reset)
|
||||
);
|
||||
println!("{str1:#?} and {str2:#?}");
|
||||
}
|
||||
{
|
||||
let (str1, str2) = formatted_comparison(inp1, inp2);
|
||||
println!(
|
||||
"{str1} {0}to {str2}{0}",
|
||||
StyleConstruct::new().style(Style::Reset)
|
||||
);
|
||||
println!("{str1:#?} and {str2:#?}");
|
||||
}
|
||||
{
|
||||
let (str1, str2) = compare2_format(inp1, inp2);
|
||||
println!(
|
||||
"{str1} {0}to {str2}{0}",
|
||||
StyleConstruct::new().style(Style::Reset)
|
||||
);
|
||||
println!("{str1:#?} and {str2:#?}");
|
||||
}
|
||||
// holy shit it works
|
||||
|
||||
// NO IT DOESNT
|
||||
|
||||
+1
-1
Submodule lda_strdist updated: 34bd99d3b4...827f1e4b75.
Reference in new issue
Block a user