From b3aa4c7117e08e062abd81571cda1a21c6f7d44c Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 8 Jun 2024 18:34:11 -0400 Subject: [PATCH] feat: show last log message below status bar --- scripts/base_module.rhai | 1 + src/engine/engine.rs | 10 ----- src/runtime.rs | 83 ++++++++++++++++++++++++++++++++-------- src/util/log.rs | 10 ++++- 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/scripts/base_module.rhai b/scripts/base_module.rhai index 111fa5e..050b723 100644 --- a/scripts/base_module.rhai +++ b/scripts/base_module.rhai @@ -3,6 +3,7 @@ fn block() { let keyprog = (); try { synless_internals::display(); + s::clear_last_log(); keyprog = synless_internals::block_on_key(); } catch (err) { log_caught_error(err); diff --git a/src/engine/engine.rs b/src/engine/engine.rs index aacefc8..7596b23 100644 --- a/src/engine/engine.rs +++ b/src/engine/engine.rs @@ -12,7 +12,6 @@ use crate::util::{error, SynlessBug, SynlessError}; use partial_pretty_printer as ppp; use partial_pretty_printer::pane; use std::collections::HashMap; -use std::error::Error; use std::path::Path; const STRING_LANGUAGE_NAME: &str = "string"; @@ -55,15 +54,6 @@ impl Engine { } } - /****************** - * Error Handling * - ******************/ - - pub fn report_error(&mut self, _error: &impl Error) { - // make sure to display the actual cause - todo!() - } - /************* * Languages * *************/ diff --git a/src/runtime.rs b/src/runtime.rs index 345f50a..0f702f0 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -7,7 +7,7 @@ use crate::keymap::{KeyLookupResult, KeyProg, Keymap, Layer, LayerManager, MenuS use crate::language::{Construct, Language}; use crate::style::Style; use crate::tree::{Mode, Node}; -use crate::util::{error, log, SynlessBug, SynlessError}; +use crate::util::{error, log, LogEntry, LogLevel, SynlessBug, SynlessError}; use partial_pretty_printer::pane; use std::cell::RefCell; use std::rc::Rc; @@ -21,16 +21,19 @@ const MENU_NAME_LABEL: &str = "menu_name"; const MODE_LABEL: &str = "mode"; const FILENAME_LABEL: &str = "filename"; const SIBLING_INDEX_LABEL: &str = "sibling_index"; -const ERROR_LABEL: &str = "error"; +const LAST_LOG_LABEL: &str = "last_log"; const KEYHINTS_PANE_WIDTH: usize = 15; +const LOG_LEVEL_TO_DISPLAY: LogLevel = LogLevel::Info; + pub struct Runtime> { engine: Engine, default_pane_notation: pane::PaneNotation, menu_pane_notation: pane::PaneNotation, frontend: F, layers: LayerManager, + last_log: Option, } impl + 'static> Runtime { @@ -46,6 +49,7 @@ impl + 'static> Runtime { menu_pane_notation: make_pane_notation(true), frontend, layers: LayerManager::new(), + last_log: None, } } @@ -135,6 +139,49 @@ impl + 'static> Runtime { } } + /*********** + * Logging * + ***********/ + + pub fn log_error(&mut self, message: String) { + self.log(LogLevel::Error, message); + } + + pub fn log_warn(&mut self, message: String) { + self.log(LogLevel::Warn, message); + } + + pub fn log_info(&mut self, message: String) { + self.log(LogLevel::Info, message); + } + + pub fn log_debug(&mut self, message: String) { + self.log(LogLevel::Debug, message); + } + + pub fn log_trace(&mut self, message: String) { + self.log(LogLevel::Trace, message); + } + + fn log(&mut self, level: LogLevel, message: String) { + let entry = LogEntry::new(level, message); + if level >= LOG_LEVEL_TO_DISPLAY { + if self + .last_log + .as_ref() + .map(|old| level > old.level) + .unwrap_or(true) + { + self.last_log = Some(entry.clone()); + } + } + entry.log(); + } + + pub fn clear_last_log(&mut self) { + self.last_log = None; + } + /*********** * Display * ***********/ @@ -167,6 +214,7 @@ impl + 'static> Runtime { self.make_mode_doc(), self.make_filename_doc(), self.make_sibling_index_doc(), + self.make_last_log_doc(), ] { let _ = self.engine.delete_doc(&name); if let Some(node) = node { @@ -239,6 +287,12 @@ impl + 'static> Runtime { ) } + fn make_last_log_doc(&mut self) -> (DocName, Option) { + let opt_message = self.last_log.as_ref().map(|entry| entry.to_string()); + let opt_node = opt_message.map(|msg| self.engine.make_string_doc(msg)); + (DocName::Auxilliary(LAST_LOG_LABEL.to_owned()), opt_node) + } + /****************** * Doc Management * ******************/ @@ -486,6 +540,9 @@ fn make_pane_notation(include_menu: bool) -> pane::PaneNotation pane::PaneNotation + 'static> Runtime { register!(module, rt.redo()?); // Logging - rhai::FuncRegistration::new("log_trace") - .in_internal_namespace() - .set_into_module(module, |msg: String| log!(Trace, "{}", msg)); - rhai::FuncRegistration::new("log_debug") - .in_internal_namespace() - .set_into_module(module, |msg: String| log!(Debug, "{}", msg)); - rhai::FuncRegistration::new("log_info") - .in_internal_namespace() - .set_into_module(module, |msg: String| log!(Info, "{}", msg)); - rhai::FuncRegistration::new("log_warn") - .in_internal_namespace() - .set_into_module(module, |msg: String| log!(Warn, "{}", msg)); - rhai::FuncRegistration::new("log_error") - .in_internal_namespace() - .set_into_module(module, |msg: String| log!(Error, "{}", msg)); + register!(module, rt.log_trace(msg: String)); + register!(module, rt.log_debug(msg: String)); + register!(module, rt.log_info(msg: String)); + register!(module, rt.log_warn(msg: String)); + register!(module, rt.log_error(msg: String)); + register!(module, rt.clear_last_log()); } } diff --git a/src/util/log.rs b/src/util/log.rs index 0c78867..b96e81b 100644 --- a/src/util/log.rs +++ b/src/util/log.rs @@ -16,9 +16,10 @@ pub struct Log { } // TODO: time stamps +#[derive(Debug, Clone)] pub struct LogEntry { - level: LogLevel, - message: String, + pub level: LogLevel, + pub message: String, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -79,6 +80,11 @@ impl LogEntry { pub fn new(level: LogLevel, message: String) -> LogEntry { LogEntry { level, message } } + + /// Adds this entry to the log. Prefer the `log!` macro when possible. + pub fn log(self) { + Log::with_log(|log| log.push(self)); + } } impl fmt::Display for LogEntry {