From 6c622864b388548eaaa595f41993606cc151d585 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Fri, 7 Apr 2023 14:27:16 +0100 Subject: [PATCH] feat: new label module Takes a text label, with the ability to include embedded scripts. Resolves #80. --- docs/_Sidebar.md | 1 + docs/modules/Label.md | 70 +++++++++++++++++++++++++++++++++++++++++++ examples/config.corn | 4 ++- examples/config.json | 4 +++ examples/config.toml | 4 +++ examples/config.yaml | 2 ++ src/bar.rs | 1 + src/config/mod.rs | 2 ++ src/dynamic_string.rs | 3 ++ src/modules/label.rs | 62 ++++++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 1 + 11 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 docs/modules/Label.md create mode 100644 src/modules/label.rs diff --git a/docs/_Sidebar.md b/docs/_Sidebar.md index 61a393f..456c578 100644 --- a/docs/_Sidebar.md +++ b/docs/_Sidebar.md @@ -21,6 +21,7 @@ - [Clock](clock) - [Custom](custom) - [Focused](focused) +- [Label](label) - [Launcher](launcher) - [Music](music) - [Script](script) diff --git a/docs/modules/Label.md b/docs/modules/Label.md new file mode 100644 index 0000000..228a574 --- /dev/null +++ b/docs/modules/Label.md @@ -0,0 +1,70 @@ +Displays custom text, with the ability to embed [scripts](https://github.com/JakeStanger/ironbar/wiki/scripts#embedding). + +## Configuration + +> Type: `label` + +| Name | Type | Default | Description | +|---------|----------|---------|-----------------------------------------| +| `label` | `string` | `null` | Text, optionally with embedded scripts. | + +
+JSON + +```json +{ + "end": [ + { + "type": "label", + "label": "random num: {{500:echo $RANDOM}}" + } + ] +} + +``` + +
+ +
+TOML + +```toml +[[end]] +type = "label" +label = "random num: {{500:echo $RANDOM}}" +``` + +
+ +
+YAML + +```yaml +end: + - type: "label" + label: "random num: {{500:echo $RANDOM}}" +``` + +
+ +
+Corn + +```corn +{ + end = [ + { + type = "label" + label = "random num: {{500:echo $RANDOM}}" + } + ] +} +``` + +
+ +## Styling + +| Selector | Description | +|--------------------------------|------------------------------------------------------------------------------------| +| `#label` | Label widget | \ No newline at end of file diff --git a/examples/config.corn b/examples/config.corn index 9fc572c..70382d1 100644 --- a/examples/config.corn +++ b/examples/config.corn @@ -67,6 +67,8 @@ let { $clipboard = { type = "clipboard" max_items = 3 truncate.mode = "end" truncate.length = 50 } + $label = { type = "label" label = "random num: {{500:echo $RANDOM}}" } + // -- begin custom -- $button = { type = "button" name="power-btn" label = "" on_click = "popup:toggle" } @@ -97,7 +99,7 @@ let { } // -- end custom -- - $left = [ $workspaces $launcher ] + $left = [ $workspaces $launcher $label ] $right = [ $mpd_local $mpd_server $phone_battery $sys_info $clipboard $power_menu $clock ] } in { diff --git a/examples/config.json b/examples/config.json index 05f3e04..0383b93 100644 --- a/examples/config.json +++ b/examples/config.json @@ -126,6 +126,10 @@ "show_icons": true, "show_names": false, "type": "launcher" + }, + { + "label": "random num: {{500:echo $RANDOM}}", + "type": "label" } ] } diff --git a/examples/config.toml b/examples/config.toml index a511b59..b30cdb7 100644 --- a/examples/config.toml +++ b/examples/config.toml @@ -116,3 +116,7 @@ favorites = [ 'Steam', ] +[[start]] +label = 'random num: {{500:echo $RANDOM}}' +type = 'label' + diff --git a/examples/config.yaml b/examples/config.yaml index 59ee18e..485a911 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -82,4 +82,6 @@ start: show_icons: true show_names: false type: launcher + - label: 'random num: {{500:echo $RANDOM}}' + type: label diff --git a/src/bar.rs b/src/bar.rs index 1637974..2d3e702 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -209,6 +209,7 @@ fn add_modules(content: >k::Box, modules: Vec, info: &ModuleInfo ModuleConfig::Clock(mut module) => add_module!(module, id), ModuleConfig::Custom(mut module) => add_module!(module, id), ModuleConfig::Focused(mut module) => add_module!(module, id), + ModuleConfig::Label(mut module) => add_module!(module, id), ModuleConfig::Launcher(mut module) => add_module!(module, id), #[cfg(feature = "music")] ModuleConfig::Music(mut module) => add_module!(module, id), diff --git a/src/config/mod.rs b/src/config/mod.rs index fe73fe0..5a03956 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -7,6 +7,7 @@ use crate::modules::clipboard::ClipboardModule; use crate::modules::clock::ClockModule; use crate::modules::custom::CustomModule; use crate::modules::focused::FocusedModule; +use crate::modules::label::LabelModule; use crate::modules::launcher::LauncherModule; #[cfg(feature = "music")] use crate::modules::music::MusicModule; @@ -47,6 +48,7 @@ pub enum ModuleConfig { Clock(Box), Custom(Box), Focused(Box), + Label(Box), Launcher(Box), #[cfg(feature = "music")] Music(Box), diff --git a/src/dynamic_string.rs b/src/dynamic_string.rs index f3aa8ed..b3f7b79 100644 --- a/src/dynamic_string.rs +++ b/src/dynamic_string.rs @@ -10,9 +10,12 @@ enum DynamicStringSegment { Dynamic(Script), } +/// A string with embedded scripts for dynamic content. pub struct DynamicString; impl DynamicString { + /// Creates a new dynamic string, based off the input template. + /// Runs `f` with the compiled string each time one of the scripts updates. pub fn new(input: &str, f: F) -> Self where F: FnMut(String) -> Continue + 'static, diff --git a/src/modules/label.rs b/src/modules/label.rs new file mode 100644 index 0000000..0ca67c7 --- /dev/null +++ b/src/modules/label.rs @@ -0,0 +1,62 @@ +use crate::config::CommonConfig; +use crate::dynamic_string::DynamicString; +use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; +use crate::try_send; +use color_eyre::Result; +use glib::Continue; +use gtk::prelude::*; +use gtk::Label; +use serde::Deserialize; +use tokio::sync::mpsc; + +#[derive(Debug, Deserialize, Clone)] +pub struct LabelModule { + label: String, + + #[serde(flatten)] + pub common: Option, +} + +impl Module