aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2024-07-15 16:16:46 +0200
committerDaniel Jankowski <daniel.jankowski@rub.de>2024-07-15 16:16:46 +0200
commit68d8d4998547e859527ad7aa7112d09ee74f0fbb (patch)
tree117f770a008eb44099410c7378d2770122f30895 /src
parentchore: update cargo deps (diff)
downloadsl0pstatus-68d8d4998547e859527ad7aa7112d09ee74f0fbb.tar.gz
sl0pstatus-68d8d4998547e859527ad7aa7112d09ee74f0fbb.tar.xz
sl0pstatus-68d8d4998547e859527ad7aa7112d09ee74f0fbb.zip
feat: add color aliases
closes #75
Diffstat (limited to 'src')
-rw-r--r--src/border.rs4
-rw-r--r--src/config.rs19
-rw-r--r--src/render.rs62
-rw-r--r--src/widgets/command.rs10
-rw-r--r--src/widgets/datetime.rs2
-rw-r--r--src/widgets/mode.rs28
-rw-r--r--src/widgets/notification.rs8
-rw-r--r--src/widgets/swap_layout.rs2
-rw-r--r--src/widgets/tabs.rs20
9 files changed, 94 insertions, 61 deletions
diff --git a/src/border.rs b/src/border.rs
index f1720db..c0c06d5 100644
--- a/src/border.rs
+++ b/src/border.rs
@@ -36,7 +36,7 @@ impl BorderConfig {
}
}
-pub fn parse_border_config(config: BTreeMap<String, String>) -> Option<BorderConfig> {
+pub fn parse_border_config(config: &BTreeMap<String, String>) -> Option<BorderConfig> {
let enabled = match config.get("border_enabled") {
Some(e) => matches!(e.as_str(), "true"),
None => {
@@ -65,7 +65,7 @@ pub fn parse_border_config(config: BTreeMap<String, String>) -> Option<BorderCon
Some(BorderConfig {
enabled,
char: char.to_owned(),
- format: FormattedPart::from_format_string(format),
+ format: FormattedPart::from_format_string(format, config),
position,
})
}
diff --git a/src/config.rs b/src/config.rs
index 875c31e..1e51ceb 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -131,16 +131,16 @@ impl ModuleConfig {
None => false,
};
- let border_config = parse_border_config(config.clone()).unwrap_or_default();
+ let border_config = parse_border_config(config).unwrap_or_default();
Ok(Self {
left_parts_config: left_parts_config.to_owned(),
- left_parts: parts_from_config(Some(&left_parts_config.to_owned())),
+ left_parts: parts_from_config(Some(&left_parts_config.to_owned()), config),
center_parts_config: center_parts_config.to_owned(),
- center_parts: parts_from_config(Some(&center_parts_config.to_owned())),
+ center_parts: parts_from_config(Some(&center_parts_config.to_owned()), config),
right_parts_config: right_parts_config.to_owned(),
- right_parts: parts_from_config(Some(&right_parts_config.to_owned())),
- format_space: FormattedPart::from_format_string(format_space_config),
+ right_parts: parts_from_config(Some(&right_parts_config.to_owned()), config),
+ format_space: FormattedPart::from_format_string(format_space_config, config),
hide_frame_for_single_pane,
border: border_config,
format_precedence,
@@ -478,11 +478,14 @@ impl ModuleConfig {
}
}
-fn parts_from_config(format: Option<&String>) -> Vec<FormattedPart> {
+fn parts_from_config(
+ format: Option<&String>,
+ config: &BTreeMap<String, String>,
+) -> Vec<FormattedPart> {
match format {
Some(format) => format
.split("#[")
- .map(FormattedPart::from_format_string)
+ .map(|s| FormattedPart::from_format_string(s, config))
.collect(),
None => vec![],
}
@@ -497,7 +500,7 @@ mod test {
fn test_formatted_part_from_string() {
let input = "#[fg=#ff0000,bg=#00ff00,bold,italic]foo";
- let part = FormattedPart::from_format_string(input);
+ let part = FormattedPart::from_format_string(input, &BTreeMap::new());
assert_eq!(
part,
diff --git a/src/render.rs b/src/render.rs
index d8a1756..352cdbb 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -44,28 +44,37 @@ pub struct FormattedPart {
create = "{ SizedCache::with_size(100) }",
convert = r#"{ (format.to_owned()) }"#
)]
-pub fn formatted_part_from_string_cached(format: &str) -> FormattedPart {
- FormattedPart::from_format_string(format)
+pub fn formatted_part_from_string_cached(
+ format: &str,
+ config: &BTreeMap<String, String>,
+) -> FormattedPart {
+ FormattedPart::from_format_string(format, config)
}
#[cached(
ty = "SizedCache<String, Vec<FormattedPart>>",
create = "{ SizedCache::with_size(100) }",
- convert = r#"{ (config.to_owned()) }"#
+ convert = r#"{ (config_string.to_owned()) }"#
)]
-pub fn formatted_parts_from_string_cached(config: &str) -> Vec<FormattedPart> {
- FormattedPart::multiple_from_format_string(config)
+pub fn formatted_parts_from_string_cached(
+ config_string: &str,
+ config: &BTreeMap<String, String>,
+) -> Vec<FormattedPart> {
+ FormattedPart::multiple_from_format_string(config_string, config)
}
impl FormattedPart {
- pub fn multiple_from_format_string(config: &str) -> Vec<Self> {
- config
+ pub fn multiple_from_format_string(
+ config_string: &str,
+ config: &BTreeMap<String, String>,
+ ) -> Vec<Self> {
+ config_string
.split("#[")
- .map(FormattedPart::from_format_string)
+ .map(|s| FormattedPart::from_format_string(s, config))
.collect()
}
- pub fn from_format_string(format: &str) -> Self {
+ pub fn from_format_string(format: &str, config: &BTreeMap<String, String>) -> Self {
let format = match format.starts_with("#[") {
true => format.strip_prefix("#[").unwrap(),
false => format,
@@ -91,15 +100,15 @@ impl FormattedPart {
for part in parts {
if part.starts_with("fg=") {
- result.fg = parse_color(part.strip_prefix("fg=").unwrap());
+ result.fg = parse_color(part.strip_prefix("fg=").unwrap(), config);
}
if part.starts_with("bg=") {
- result.bg = parse_color(part.strip_prefix("bg=").unwrap());
+ result.bg = parse_color(part.strip_prefix("bg=").unwrap(), config);
}
if part.starts_with("us=") {
- result.us = parse_color(part.strip_prefix("us=").unwrap());
+ result.us = parse_color(part.strip_prefix("us=").unwrap(), config);
}
if part.eq("reverse") {
@@ -290,8 +299,17 @@ fn hex_to_rgb(s: &str) -> anyhow::Result<Vec<u8>> {
create = "{ SizedCache::with_size(100) }",
convert = r#"{ (color.to_owned()) }"#
)]
-fn parse_color(color: &str) -> Option<Color> {
+fn parse_color(color: &str, config: &BTreeMap<String, String>) -> Option<Color> {
let mut color = color;
+ if color.starts_with('{') {
+ let alias_name = &color[1..color.len() - 1];
+
+ color = match config.get(&format!("color_{alias_name}")) {
+ Some(color_str) => color_str,
+ None => return None,
+ };
+ }
+
if color.starts_with('#') {
let rgb = match hex_to_rgb(color.strip_prefix('#').unwrap()) {
Ok(rgb) => rgb,
@@ -376,18 +394,28 @@ mod test {
#[test]
fn test_parse_color() {
- let result = parse_color("#010203");
+ let mut config: BTreeMap<String, String> = BTreeMap::new();
+ config.insert("color_green".to_owned(), "#00ff00".to_owned());
+
+ let result = parse_color("#010203", &config);
let expected = RgbColor(1, 2, 3);
assert_eq!(result, Some(expected.into()));
- let result = parse_color("255");
+ let result = parse_color("255", &config);
let expected = Ansi256Color(255);
assert_eq!(result, Some(expected.into()));
- let result = parse_color("365");
+ let result = parse_color("365", &config);
assert_eq!(result, None);
- let result = parse_color("#365");
+ let result = parse_color("#365", &config);
+ assert_eq!(result, None);
+
+ let result = parse_color("{green}", &config);
+ let expected = RgbColor(0, 255, 0);
+ assert_eq!(result, Some(expected.into()));
+
+ let result = parse_color("{blue}", &config);
assert_eq!(result, None);
}
}
diff --git a/src/widgets/command.rs b/src/widgets/command.rs
index 91ff991..c5a902f 100644
--- a/src/widgets/command.rs
+++ b/src/widgets/command.rs
@@ -50,12 +50,14 @@ pub struct CommandResult {
pub struct CommandWidget {
config: BTreeMap<String, CommandConfig>,
+ zj_conf: BTreeMap<String, String>,
}
impl CommandWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
Self {
config: parse_config(config),
+ zj_conf: config.clone(),
}
}
}
@@ -123,7 +125,7 @@ impl Widget for CommandWidget {
match command_config.render_mode {
RenderMode::Static => content,
- RenderMode::Dynamic => render_dynamic_formatted_content(&content),
+ RenderMode::Dynamic => render_dynamic_formatted_content(&content, &self.zj_conf),
RenderMode::Raw => content,
}
}
@@ -153,8 +155,8 @@ impl Widget for CommandWidget {
}
}
-fn render_dynamic_formatted_content(content: &str) -> String {
- formatted_parts_from_string_cached(content)
+fn render_dynamic_formatted_content(content: &str, config: &BTreeMap<String, String>) -> String {
+ formatted_parts_from_string_cached(content, config)
.iter()
.map(|fp| fp.format_string(&fp.content))
.collect::<Vec<String>>()
@@ -263,7 +265,7 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC
if key.ends_with("format") {
command_conf.format =
- FormattedPart::multiple_from_format_string(zj_conf.get(&key).unwrap());
+ FormattedPart::multiple_from_format_string(zj_conf.get(&key).unwrap(), zj_conf);
}
if key.ends_with("interval") {
diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs
index 1d3a23b..5e9c099 100644
--- a/src/widgets/datetime.rs
+++ b/src/widgets/datetime.rs
@@ -51,7 +51,7 @@ impl DateTimeWidget {
format: format.to_owned(),
date_format: date_format.to_owned(),
time_format: time_format.to_owned(),
- color_format: FormattedPart::multiple_from_format_string(color_format),
+ color_format: FormattedPart::multiple_from_format_string(color_format, config),
time_zone,
}
}
diff --git a/src/widgets/mode.rs b/src/widgets/mode.rs
index 68c9bfa..8544cf9 100644
--- a/src/widgets/mode.rs
+++ b/src/widgets/mode.rs
@@ -28,72 +28,72 @@ pub struct ModeWidget {
impl ModeWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
let normal_format = match config.get("mode_normal") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let locked_format = match config.get("mode_locked") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let resize_format = match config.get("mode_resize") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let pane_format = match config.get("mode_pane") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let tab_format = match config.get("mode_tab") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let scroll_format = match config.get("mode_scroll") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let enter_search_format = match config.get("mode_enter_search") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let search_format = match config.get("mode_search") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let rename_tab_format = match config.get("mode_rename_tab") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let rename_pane_format = match config.get("mode_rename_pane") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let session_format = match config.get("mode_session") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let move_format = match config.get("mode_move") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let prompt_format = match config.get("mode_prompt") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
let tmux_format = match config.get("mode_tmux") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => vec![],
};
diff --git a/src/widgets/notification.rs b/src/widgets/notification.rs
index 1b8cfcd..458d12a 100644
--- a/src/widgets/notification.rs
+++ b/src/widgets/notification.rs
@@ -20,13 +20,13 @@ pub struct NotificationWidget {
impl NotificationWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
let format_unread = match config.get("notification_format_unread") {
- Some(f) => FormattedPart::multiple_from_format_string(f),
- None => FormattedPart::multiple_from_format_string(""),
+ Some(f) => FormattedPart::multiple_from_format_string(f, config),
+ None => FormattedPart::multiple_from_format_string("", config),
};
let format_no_notifications = match config.get("notification_format_no_notifications") {
- Some(f) => FormattedPart::multiple_from_format_string(f),
- None => FormattedPart::multiple_from_format_string(""),
+ Some(f) => FormattedPart::multiple_from_format_string(f, config),
+ None => FormattedPart::multiple_from_format_string("", config),
};
let show_interval = match config.get("notification_show_interval") {
diff --git a/src/widgets/swap_layout.rs b/src/widgets/swap_layout.rs
index d8f9ffb..8c9e036 100644
--- a/src/widgets/swap_layout.rs
+++ b/src/widgets/swap_layout.rs
@@ -13,7 +13,7 @@ impl SwapLayoutWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
let mut format: Vec<FormattedPart> = Vec::new();
if let Some(form) = config.get("swap_layout_format") {
- format = FormattedPart::multiple_from_format_string(form);
+ format = FormattedPart::multiple_from_format_string(form, config);
}
let hide_if_empty = match config.get("swap_layout_hide_if_empty") {
diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs
index ee88769..46753bf 100644
--- a/src/widgets/tabs.rs
+++ b/src/widgets/tabs.rs
@@ -30,36 +30,36 @@ impl TabsWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
let mut normal_tab_format: Vec<FormattedPart> = Vec::new();
if let Some(form) = config.get("tab_normal") {
- normal_tab_format = FormattedPart::multiple_from_format_string(form);
+ normal_tab_format = FormattedPart::multiple_from_format_string(form, config);
}
let normal_tab_fullscreen_format = match config.get("tab_normal_fullscreen") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => normal_tab_format.clone(),
};
let normal_tab_sync_format = match config.get("tab_normal_sync") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => normal_tab_format.clone(),
};
let mut active_tab_format = normal_tab_format.clone();
if let Some(form) = config.get("tab_active") {
- active_tab_format = FormattedPart::multiple_from_format_string(form);
+ active_tab_format = FormattedPart::multiple_from_format_string(form, config);
}
let active_tab_fullscreen_format = match config.get("tab_active_fullscreen") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => active_tab_format.clone(),
};
let active_tab_sync_format = match config.get("tab_active_sync") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => active_tab_format.clone(),
};
let rename_tab_format = match config.get("tab_rename") {
- Some(form) => FormattedPart::multiple_from_format_string(form),
+ Some(form) => FormattedPart::multiple_from_format_string(form, config),
None => active_tab_format.clone(),
};
@@ -73,15 +73,15 @@ impl TabsWidget {
let tab_truncate_start_format = config
.get("tab_truncate_start_format")
- .map(|form| FormattedPart::from_format_string(form));
+ .map(|form| FormattedPart::from_format_string(form, config));
let tab_truncate_end_format = config
.get("tab_truncate_end_format")
- .map(|form| FormattedPart::from_format_string(form));
+ .map(|form| FormattedPart::from_format_string(form, config));
let separator = config
.get("tab_separator")
- .map(|s| FormattedPart::from_format_string(s));
+ .map(|s| FormattedPart::from_format_string(s, config));
Self {
normal_tab_format,