Skip to content

Commit

Permalink
Split side panel settings into tabs :
Browse files Browse the repository at this point in the history
Create plot settings tab and highlights settings tab.
- The highlights settings allow user to set until 4 regex sentence to find in content and highlight them with predefined color
- Edits the plot settings to hide the plot view if number of plot equals 0

Signed-off-by: stropee <[email protected]>
  • Loading branch information
simon0356 committed Oct 6, 2024
1 parent 25e5563 commit 5f9e68a
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 244 deletions.
101 changes: 101 additions & 0 deletions src/custom_highlighter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@


use eframe::egui::{FontFamily, FontId};
use eframe::egui::{self, text::LayoutJob, Color32, TextFormat};


extern crate regex;
use regex::Regex;
use regex::RegexSet;
const DEFAULT_FONT_ID: FontId = FontId::new(14.0, FontFamily::Monospace);


#[derive(Debug)]
#[derive(Clone, Copy)]
pub struct HighLightElement
{
pos_start:usize,
pos_end:usize,
token_idx:usize
}
impl HighLightElement{
pub fn new(pos_start: usize, pos_end:usize,token_idx:usize) -> Self{
Self{
pos_start,
pos_end,
token_idx
}
}
}
pub fn highlight_impl(
_ctx: &egui::Context,
text: &str,
tokens: Vec<String>,
default_color:Color32
) -> Option<LayoutJob> {
// Extremely simple syntax highlighter for when we compile without syntect


let mut my_tokens = tokens.clone();
for token in my_tokens.clone()
{
if token.len() < 1
{
let index = my_tokens.iter().position(|x| *x == token).unwrap();
my_tokens.remove(index);
}
}
let set = RegexSet::new(my_tokens.clone()).unwrap();
let content_string = String::from(text);
// let _ = file.read_to_string(&mut isi);
let mut regexs:Vec<Regex> = Vec::new();
for sentence in my_tokens.clone() {
let re = Regex::new(&sentence).unwrap(); // should not panic because we parsed Regexes above already
regexs.push(re);
}

let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new();

for idx in set.matches(&content_string).into_iter() {
for caps in regexs[idx].captures_iter(&content_string) {
highlight_list.push(HighLightElement::new(
caps.get(0).unwrap().start(),
caps.get(0).unwrap().end(),
idx));
}
}

highlight_list.sort_by_key(|item| (item.pos_start, item.pos_end));


let mut job = LayoutJob::default();
let mut previous = HighLightElement::new(0,0, 0);
for matches in highlight_list
{
if previous.pos_end >= matches.pos_start
{
continue
}
job.append(&text[previous.pos_end..(matches.pos_start)], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color));
if matches.token_idx == 0
{
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(255, 100, 100)));
}else if matches.token_idx == 1
{
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(225, 159, 0)));

}else if matches.token_idx == 2
{
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(87, 165, 171)));
}else if matches.token_idx == 3
{
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(109, 147, 226)));
}
previous = matches.clone();
}
job.append(&text[previous.pos_end..], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color));


Some(job)
}

Loading

0 comments on commit 5f9e68a

Please sign in to comment.