Skip to content

Commit 798e389

Browse files
committed
Split side panel settings into tabs :
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 <simon@sirocha.fr>
1 parent cb945f1 commit 798e389

File tree

4 files changed

+494
-270
lines changed

4 files changed

+494
-270
lines changed

src/custom_highlighter.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
3+
use eframe::egui::{FontFamily, FontId};
4+
use eframe::egui::{self, text::LayoutJob, Color32, TextFormat};
5+
6+
7+
extern crate regex;
8+
use regex::Regex;
9+
use regex::RegexSet;
10+
const DEFAULT_FONT_ID: FontId = FontId::new(14.0, FontFamily::Monospace);
11+
12+
13+
#[derive(Debug)]
14+
#[derive(Clone, Copy)]
15+
pub struct HighLightElement
16+
{
17+
pos_start:usize,
18+
pos_end:usize,
19+
token_idx:usize
20+
}
21+
impl HighLightElement{
22+
pub fn new(pos_start: usize, pos_end:usize,token_idx:usize) -> Self{
23+
Self{
24+
pos_start,
25+
pos_end,
26+
token_idx
27+
}
28+
}
29+
}
30+
pub fn highlight_impl(
31+
_ctx: &egui::Context,
32+
text: &str,
33+
tokens: Vec<String>,
34+
default_color:Color32
35+
) -> Option<LayoutJob> {
36+
// Extremely simple syntax highlighter for when we compile without syntect
37+
38+
39+
let mut my_tokens = tokens.clone();
40+
for token in my_tokens.clone()
41+
{
42+
if token.len() < 1
43+
{
44+
let index = my_tokens.iter().position(|x| *x == token).unwrap();
45+
my_tokens.remove(index);
46+
}
47+
}
48+
let set = RegexSet::new(my_tokens.clone()).unwrap();
49+
let content_string = String::from(text);
50+
// let _ = file.read_to_string(&mut isi);
51+
let mut regexs:Vec<Regex> = Vec::new();
52+
for sentence in my_tokens.clone() {
53+
let re = Regex::new(&sentence).unwrap(); // should not panic because we parsed Regexes above already
54+
regexs.push(re);
55+
}
56+
57+
let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new();
58+
59+
for idx in set.matches(&content_string).into_iter() {
60+
for caps in regexs[idx].captures_iter(&content_string) {
61+
highlight_list.push(HighLightElement::new(
62+
caps.get(0).unwrap().start(),
63+
caps.get(0).unwrap().end(),
64+
idx));
65+
}
66+
}
67+
68+
highlight_list.sort_by_key(|item| (item.pos_start, item.pos_end));
69+
70+
71+
let mut job = LayoutJob::default();
72+
let mut previous = HighLightElement::new(0,0, 0);
73+
for matches in highlight_list
74+
{
75+
if previous.pos_end >= matches.pos_start
76+
{
77+
continue
78+
}
79+
job.append(&text[previous.pos_end..(matches.pos_start)], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color));
80+
if matches.token_idx == 0
81+
{
82+
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(255, 100, 100)));
83+
}else if matches.token_idx == 1
84+
{
85+
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(225, 159, 0)));
86+
87+
}else if matches.token_idx == 2
88+
{
89+
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(87, 165, 171)));
90+
}else if matches.token_idx == 3
91+
{
92+
job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(109, 147, 226)));
93+
}
94+
previous = matches.clone();
95+
}
96+
job.append(&text[previous.pos_end..], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color));
97+
98+
99+
Some(job)
100+
}
101+

0 commit comments

Comments
 (0)