Skip to content

Commit

Permalink
In the middle of reworking everything
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorS committed Sep 4, 2021
1 parent 283f094 commit 57832be
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 30 deletions.
40 changes: 25 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use eframe::{egui, epi};

use crate::highlighter::CachingHighlighter;
use crate::settings::Settings;

#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default()))]
pub struct App {
line: String,
lines: Vec<String>,
highlighter: CachingHighlighter,
}

impl Default for App {
fn default() -> Self {
impl App {
pub fn new(settings: Settings) -> Self {
Self {
line: "Ada has become a Cook.".into(),
highlighter: CachingHighlighter::default(),
lines: vec![
"Trevor has been stung by a bee!".into(),
"Ada has been stung by a bee!".into(),
"Faith-Anne has been stung by a bee!".into(),
],
highlighter: CachingHighlighter::new(settings),
}
}
}
Expand Down Expand Up @@ -45,24 +50,29 @@ impl epi::App for App {
});

egui::CentralPanel::default().show(ctx, |ui| {
let Self { line, highlighter } = self;
let Self { lines, highlighter } = self;

let text_style = eframe::egui::TextStyle::Body;
let row_height = ui.fonts()[text_style].row_height();
let num_rows = lines.len();

let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
let mut layout_job = highlighter.highlight(ui.visuals().dark_mode, string);

layout_job.wrap_width = wrap_width;

ui.fonts().layout_job(layout_job)
};

let announcements = egui::TextEdit::multiline(line)
.text_style(egui::TextStyle::Monospace)
.enabled(false)
.frame(false)
.desired_width(f32::INFINITY)
.layouter(&mut layouter);
egui::ScrollArea::vertical().show_rows(ui, row_height, num_rows, |ui, row_range| {
let mut text = lines[row_range].join("\n");
let log = egui::TextEdit::multiline(&mut text)
.text_style(text_style)
.desired_width(f32::INFINITY)
.enabled(false)
.frame(false)
.layouter(&mut layouter);

egui::ScrollArea::vertical().show(ui, |ui| {
ui.add_sized(ui.available_size(), announcements);
ui.add(log);
});
});
}
Expand Down
101 changes: 87 additions & 14 deletions src/highlighter.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use eframe::egui;
use eframe::egui::text::{LayoutJob, TextFormat};
use eframe::egui::Color32;

use egui::text::{LayoutJob, TextFormat};
use egui::Color32;
use crate::settings::Settings;

// TODO: Implement more complex layout caching.
// TODO: Implement more sophisticated layout caching.
pub struct CachingHighlighter {
is_dark_mode: bool,
string: String,
output: LayoutJob,
highlighter: Highlighter,
}

impl Default for CachingHighlighter {
fn default() -> Self {
impl CachingHighlighter {
pub fn new(settings: Settings) -> Self {
Self {
is_dark_mode: false,
string: "".to_owned(),
string: "".into(),
output: LayoutJob::default(),
highlighter: Highlighter {},
highlighter: Highlighter::new(settings),
}
}
}
Expand All @@ -32,17 +32,90 @@ impl CachingHighlighter {
}
}

pub struct Highlighter {}
#[derive(Debug, Clone)]
pub struct ParsedLine {
line: String,
group: Option<String>,
category: Option<String>,
color: Option<String>,
highlights: Vec<(String, String)>,
icons: Vec<(String, String)>,
}

impl ParsedLine {
pub fn get_words(&self) -> Vec<&str> {
self.line.split_whitespace().collect()
}

pub fn get_base_text_color(&self) -> Color32 {
if let Some(hex) = &self.color {
hex_to_color(&hex)
} else {
Color32::WHITE
}
}
}

pub struct Highlighter {
settings: Settings,
}

impl Highlighter {
pub fn highlight(&mut self, _is_dark_mode: bool, string: &str) -> LayoutJob {
let format = create_text_format(Color32::WHITE, Color32::BLACK);
pub fn new(settings: Settings) -> Self {
Self { settings }
}

pub fn parse_line(&self, line: &str) -> ParsedLine {
let line = String::from(line);

let mut highlights = vec![];
let mut icons = vec![];

for (word, color) in self.settings.get_highlights() {
if line.contains(word) {
let color = self.settings.translate_color(&color);

highlights.push((word.to_owned(), color));
}
}

for (word, icon) in self.settings.get_icons() {
if line.contains(word) {
icons.push((word.to_owned(), icon.to_owned()))
}
}

for filter in self.settings.get_filters() {
if filter.matches(&line) {
return ParsedLine {
line,
group: Some(filter.group.to_owned()),
category: Some(filter.category.to_owned()),
color: filter.color.to_owned(),
highlights,
icons,
};
}
}

ParsedLine {
line,
group: None,
category: None,
color: None,
highlights,
icons,
}
}

pub fn highlight(&mut self, _is_dark_mode: bool, line: &str) -> LayoutJob {
let mut job = LayoutJob::default();
job.append(&string, 0.0, format);
let parsed_line = self.parse_line(line);

dbg!(&parsed_line);

let fa = create_text_format(Color32::RED, Color32::BLACK);
job.append("\nHello Faith-Anne!", 0.0, fa);
let text_format = create_text_format(parsed_line.get_base_text_color(), Color32::BLACK);
job.append(&parsed_line.line, 0.0, text_format);

job
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod settings;

use std::path::PathBuf;

use app::App;
use gamelog::Gamelog;
use settings::Settings;

Expand All @@ -23,7 +24,7 @@ fn main() {

let _rx = gamelog.connect().expect("Failed to read gamelog.txt!");

let df_app = app::App::default();
let df_app = App::new(settings.clone());
let native_options = eframe::NativeOptions::default();

eframe::run_native(Box::new(df_app), native_options);
Expand Down

0 comments on commit 57832be

Please sign in to comment.