Skip to content

Commit

Permalink
feat: show last log message below status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Jun 8, 2024
1 parent a8eea4e commit b3aa4c7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
1 change: 1 addition & 0 deletions scripts/base_module.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 *
*************/
Expand Down
83 changes: 66 additions & 17 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<F: Frontend<Style = Style>> {
engine: Engine,
default_pane_notation: pane::PaneNotation<DocDisplayLabel, Style>,
menu_pane_notation: pane::PaneNotation<DocDisplayLabel, Style>,
frontend: F,
layers: LayerManager,
last_log: Option<LogEntry>,
}

impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
Expand All @@ -46,6 +49,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
menu_pane_notation: make_pane_notation(true),
frontend,
layers: LayerManager::new(),
last_log: None,
}
}

Expand Down Expand Up @@ -135,6 +139,49 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
}
}

/***********
* 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 {

Check failure on line 168 in src/runtime.rs

View workflow job for this annotation

GitHub Actions / build

this `if` statement can be collapsed
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 *
***********/
Expand Down Expand Up @@ -167,6 +214,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
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 {
Expand Down Expand Up @@ -239,6 +287,12 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
)
}

fn make_last_log_doc(&mut self) -> (DocName, Option<Node>) {
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 *
******************/
Expand Down Expand Up @@ -486,6 +540,9 @@ fn make_pane_notation(include_menu: bool) -> pane::PaneNotation<DocDisplayLabel,
(PaneSize::Fixed(1), padding),
])),
};
let log_doc = PaneNotation::Doc {
label: DocDisplayLabel::Auxilliary(LAST_LOG_LABEL.to_owned()),
};

let mut main_doc_and_menu = vec![(PaneSize::Proportional(1), main_doc)];
if include_menu {
Expand All @@ -506,6 +563,7 @@ fn make_pane_notation(include_menu: bool) -> pane::PaneNotation<DocDisplayLabel,
]),
),
(PaneSize::Fixed(1), status_bar),
(PaneSize::Fixed(1), log_doc),
])
}

Expand Down Expand Up @@ -743,20 +801,11 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
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());
}
}
10 changes: 8 additions & 2 deletions src/util/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b3aa4c7

Please sign in to comment.