From 6651e5d897b5a82c4681a7bf9925e50f151e1ee5 Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 9 Jun 2024 12:20:22 -0400 Subject: [PATCH] feat: color mode in status bar --- Cargo.toml | 2 +- data/string_lang.ron | 19 ++++++++++++++++--- src/engine/engine.rs | 17 ++++++++++++++--- src/runtime.rs | 27 ++++++++++++++------------- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7323333..7a71520 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ ron = "0.8.1" [dependencies.partial-pretty-printer] #path = "../ppp/" git = "https://github.com/justinpombrio/partial-pretty-printer" - version = "0.9.0" + version = "0.9.1" features = ["serialization"] [dependencies.no-nonsense-flamegraphs] version = "0.2.*" diff --git a/data/string_lang.ron b/data/string_lang.ron index e4868de..7f0b222 100644 --- a/data/string_lang.ron +++ b/data/string_lang.ron @@ -5,14 +5,22 @@ LanguageSpec( constructs: [ ConstructSpec( name: "Root", - arity: Fixed([SortSpec(["String"])]), + arity: Fixed([SortSpec(["any"])]), + ), + ConstructSpec( + name: "BgBase08", + arity: Fixed([SortSpec(["any"])]), + ), + ConstructSpec( + name: "BgBase0B", + arity: Fixed([SortSpec(["any"])]), ), ConstructSpec( name: "String", arity: Texty, ), ], - sorts: [], + sorts: [("any", SortSpec(["BgBase08", "BgBase0B", "String"]))], root_construct: "Root", ), default_display_notation: "DefaultDisplay", @@ -20,7 +28,12 @@ LanguageSpec( notations: [ NotationSetSpec( name: "DefaultDisplay", - notations: [("Root", Child(0)), ("String", Text)], + notations: [ + ("Root", Child(0)), + ("BgBase08", Style(Properties(bg_color: Some(Base08)), Child(0))), + ("BgBase0B", Style(Properties(bg_color: Some(Base0B)), Child(0))), + ("String", Text) + ], ) ] ) diff --git a/src/engine/engine.rs b/src/engine/engine.rs index 7596b23..33f4224 100644 --- a/src/engine/engine.rs +++ b/src/engine/engine.rs @@ -7,8 +7,9 @@ use super::Settings; use crate::language::{Language, LanguageSpec, NotationSetSpec, Storage}; use crate::parsing::{Parse, ParseError}; use crate::pretty_doc::DocRef; +use crate::style::Base16Color; use crate::tree::{Mode, Node}; -use crate::util::{error, SynlessBug, SynlessError}; +use crate::util::{bug, error, SynlessBug, SynlessError}; use partial_pretty_printer as ppp; use partial_pretty_printer::pane; use std::collections::HashMap; @@ -252,7 +253,7 @@ impl Engine { .get_content(&self.storage, label, &self.settings) } - pub fn make_string_doc(&mut self, string: String) -> Node { + pub fn make_string_doc(&mut self, string: String, bg_color: Option) -> Node { let lang = self .storage .language(STRING_LANGUAGE_NAME) @@ -260,7 +261,17 @@ impl Engine { let c_root = lang.root_construct(&self.storage); let c_string = lang.construct(&self.storage, "String").bug(); let string_node = Node::with_text(&mut self.storage, c_string, string).bug(); - Node::with_children(&mut self.storage, c_root, [string_node]).bug() + let node = if let Some(color) = bg_color { + let c_color = match color { + Base16Color::Base08 => lang.construct(&self.storage, "BgBase08").bug(), + Base16Color::Base0B => lang.construct(&self.storage, "BgBase0B").bug(), + _ => bug!("make_string_doc: specified bg color not yet supported"), + }; + Node::with_children(&mut self.storage, c_color, [string_node]).bug() + } else { + string_node + }; + Node::with_children(&mut self.storage, c_root, [node]).bug() } /*********** diff --git a/src/runtime.rs b/src/runtime.rs index 0f702f0..5b5e55a 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -165,15 +165,14 @@ impl + 'static> Runtime { fn log(&mut self, level: LogLevel, message: String) { let entry = LogEntry::new(level, message); - if level >= LOG_LEVEL_TO_DISPLAY { - if self + if level >= LOG_LEVEL_TO_DISPLAY + && self .last_log .as_ref() .map(|old| level > old.level) .unwrap_or(true) - { - self.last_log = Some(entry.clone()); - } + { + self.last_log = Some(entry.clone()); } entry.log(); } @@ -246,16 +245,18 @@ impl + 'static> Runtime { let opt_node = self .layers .menu_description() - .map(|menu_name| self.engine.make_string_doc(menu_name.to_owned())); + .map(|menu_name| self.engine.make_string_doc(menu_name.to_owned(), None)); (DocName::Auxilliary(MENU_NAME_LABEL.to_owned()), opt_node) } fn make_mode_doc(&mut self) -> (DocName, Option) { - let mode = match self.engine.mode() { - Mode::Tree => "TREE".to_owned(), - Mode::Text => "TEXT".to_owned(), + use crate::style::Base16Color; + + let (mode, color) = match self.engine.mode() { + Mode::Tree => ("[TREE]".to_owned(), None), + Mode::Text => ("[TEXT]".to_owned(), Some(Base16Color::Base0B)), }; - let node = self.engine.make_string_doc(mode); + let node = self.engine.make_string_doc(mode, color); (DocName::Auxilliary(MODE_LABEL.to_owned()), Some(node)) } @@ -269,7 +270,7 @@ impl + 'static> Runtime { DocName::Metadata(label) => format!("metadata:{}", label), DocName::Auxilliary(label) => format!("auxilliary:{}", label), }); - let opt_node = opt_label.map(|label| self.engine.make_string_doc(label)); + let opt_node = opt_label.map(|label| self.engine.make_string_doc(label, None)); (DocName::Auxilliary(FILENAME_LABEL.to_owned()), opt_node) } @@ -280,7 +281,7 @@ impl + 'static> Runtime { let (numerator, denominator) = cursor.sibling_index_info(s); format!("sibling {}/{}", numerator, denominator) }); - let opt_node = opt_label.map(|label| self.engine.make_string_doc(label)); + let opt_node = opt_label.map(|label| self.engine.make_string_doc(label, None)); ( DocName::Auxilliary(SIBLING_INDEX_LABEL.to_owned()), opt_node, @@ -289,7 +290,7 @@ 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)); + let opt_node = opt_message.map(|msg| self.engine.make_string_doc(msg, None)); (DocName::Auxilliary(LAST_LOG_LABEL.to_owned()), opt_node) }