Skip to content

Commit

Permalink
Merge pull request #106 from hacknus/implement_egui_logger
Browse files Browse the repository at this point in the history
implement egui_logger
  • Loading branch information
hacknus authored Nov 26, 2024
2 parents 90c0069 + 3c7d786 commit c7244dd
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 394 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ All notable changes to the `Serial Monitor` crate will be documented in this fil

### Added:

* Up to 4 Sentences highlightments using regex
* Groups settings in the side bar by category into collapsing menu.
* removed the custom implementation of `Print` and `ScrollArea` and implemented the `log` crate and `egui_logger`
* Up to 4 Sentences highlightings using regex (thanks [@simon0356](https://github.com/simon0356))
* Groups settings in the side bar by category into collapsing menu. (thanks [@simon0356](https://github.com/simon0356))

## 0.3.0 - 14.10.2024 - Automatic Reconnection

Expand Down
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ egui_extras = { version = "0.29.0", features = ["serde"] }
egui_plot = "0.29.0"
egui-phosphor = { git = "https://github.com/hacknus/egui-phosphor" }
egui-theme-switch = { version = "0.2.0", default-features = true }
egui_logger = "0.6.1"
hex = "0.4"
image = { version = "0.25", default-features = false, features = ["png"] }
itertools-num = "0.1"
Expand All @@ -27,6 +28,7 @@ rfd = "0.15.0"
safe-transmute = "0.11"
serde = { version = "1.0", features = ["derive"] }
serialport = { git = "https://github.com/serialport/serialport-rs", features = ["serde"] }
log = "0.4.22"

[package.metadata.bundle]
name = "Serial Monitor"
Expand Down
2 changes: 1 addition & 1 deletion src/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Color
mesh.colored_vertex(pos2(x, rect.top()), color);
mesh.colored_vertex(pos2(x, rect.bottom()), color);
if i < N {
mesh.add_triangle((2 * i + 0) as u32, (2 * i + 1) as u32, (2 * i + 2) as u32);
mesh.add_triangle((2 * i) as u32, (2 * i + 1) as u32, (2 * i + 2) as u32);
mesh.add_triangle((2 * i + 1) as u32, (2 * i + 2) as u32, (2 * i + 3) as u32);
}
}
Expand Down
122 changes: 62 additions & 60 deletions src/custom_highlighter.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,115 @@


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

use eframe::egui::{FontFamily, FontId};

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
#[derive(Debug, 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{
impl HighLightElement {
pub fn new(pos_start: usize, pos_end: usize, token_idx: usize) -> Self {
Self {
pos_start,
pos_end,
token_idx
token_idx,
}
}
}
pub fn highlight_impl(
_ctx: &egui::Context,
text: &str,
tokens: Vec<String>,
default_color:Color32
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
{
for token in my_tokens.clone() {
if token.is_empty() {
let index = my_tokens.iter().position(|x| *x == token).unwrap();
my_tokens.remove(index);
}
}

let content_string = String::from(text);
// let _ = file.read_to_string(&mut isi);
let mut regexs:Vec<Regex> = Vec::new();
let mut regexs: Vec<Regex> = Vec::new();
for sentence in my_tokens.clone() {
match Regex::new(&sentence){
match Regex::new(&sentence) {
Ok(re) => {
regexs.push(re);
},
Err(_err) =>{

},
}
Err(_err) => {}
};
}

let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new();
match RegexSet::new(my_tokens.clone()){
let mut highlight_list: Vec<HighLightElement> = Vec::<HighLightElement>::new();
match RegexSet::new(my_tokens.clone()) {
Ok(set) => {
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().start(),
caps.get(0).unwrap().end(),
idx));
idx,
));
}
}
},
Err(_err) => {

}
Err(_err) => {}
};

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
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)));
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();
previous = matches;
}
job.append(&text[previous.pos_end..], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color));

job.append(
&text[previous.pos_end..],
0.0,
TextFormat::simple(DEFAULT_FONT_ID, default_color),
);

Some(job)
}

Loading

0 comments on commit c7244dd

Please sign in to comment.