Skip to content

Commit

Permalink
feat: add color aliases
Browse files Browse the repository at this point in the history
closes #75
  • Loading branch information
dj95 committed Jul 15, 2024
1 parent c1a2362 commit 68d8d49
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 78 deletions.
5 changes: 5 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn bench_moduleconfig_render_bar(c: &mut Criterion) {
fn bench_formattedpart_format_string_with_widgets(c: &mut Criterion) {
let mut format = FormattedPart::from_format_string(
"#[fg=#9399B2,bg=#181825,bold,italic] {mode} {datetime} {session} [] ",
&BTreeMap::new(),
);

let mut widgets: BTreeMap<String, Arc<dyn Widget>> = BTreeMap::new();
Expand Down Expand Up @@ -106,6 +107,7 @@ fn bench_formattedpart_from_format_string(c: &mut Criterion) {
b.iter(|| {
FormattedPart::from_format_string(
"#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ",
&BTreeMap::new(),
)
})
});
Expand All @@ -116,6 +118,7 @@ fn bench_formattedpart_from_format_string_cached(c: &mut Criterion) {
b.iter(|| {
formatted_part_from_string_cached(
"#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ",
&BTreeMap::new(),
)
})
});
Expand All @@ -126,6 +129,7 @@ fn bench_formattedpart_multiple_from_format_string(c: &mut Criterion) {
b.iter(|| {
FormattedPart::multiple_from_format_string(
"#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] #[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ",
&BTreeMap::new(),
)
})
});
Expand All @@ -136,6 +140,7 @@ fn bench_formattedparts_from_format_string_cached(c: &mut Criterion) {
b.iter(|| {
formatted_parts_from_string_cached(
"#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] #[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ",
&BTreeMap::new(),
)
})
});
Expand Down
38 changes: 21 additions & 17 deletions plugin-dev-workspace.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ layout {

pane size=2 borderless=true {
plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" {
format_left "{mode}#[fg=#89B4FA,bg=#181825,bold] {session}"
format_center "{tabs}"
color_blue "#89B4FA"
color_yellow "yellow"
color_bg "#181825"

format_left "{mode}#[fg={blue},bg={bg},bold] {session}"
format_center "{tabs} {command_3}"
format_right "{datetime}"
format_space "#[bg=#181825]"
format_space "#[bg={bg}]"
format_precedence "lrc"
format_hide_on_overlength "false"

notification_format_unread "#[fg=#89B4FA,bg=#181825,blink]  #[fg=#89B4FA,bg=#181825] {message} "
notification_format_no_notifications "#[fg=#89B4FA,bg=#181825,dim]  "
notification_format_unread "#[fg={blue},bg={bg},blink]  #[fg={blue},bg={bg}] {message} "
notification_format_no_notifications "#[fg={blue},bg={bg},dim]  "
notification_show_interval "10"

border_enabled "true"
Expand All @@ -26,22 +30,22 @@ layout {

hide_frame_for_single_pane "true"

mode_normal "#[bg=blue] #[bg=yellow] "
mode_tmux "#[bg=yellow] "
mode_normal "#[bg={blue}] #[bg={yellow}] "
mode_tmux "#[bg={yellow}] "
mode_default_to_mode "tmux"

tab_normal "#[fg=#6C7086,bg=#181825] {index} {name} {floating_indicator} "
tab_rename "#[fg=#eba0ac,bg=#181825] {index} {name} {floating_indicator} "
tab_normal_fullscreen "#[fg=#6C7086,bg=#181825] {index} {name} [] "
tab_normal_sync "#[fg=#6C7086,bg=#181825] {index} {name} <> "
tab_active "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} {floating_total_count}{floating_indicator}{sync_indicator}{fullscreen_indicator}"
tab_separator "#[fg=#6C7086,bg=#181825] | "
tab_normal "#[fg=#6C7086,bg={bg}] {index} {name} {floating_indicator} "
tab_rename "#[fg=#eba0ac,bg={bg}] {index} {name} {floating_indicator} "
tab_normal_fullscreen "#[fg=#6C7086,bg={bg}] {index} {name} [] "
tab_normal_sync "#[fg=#6C7086,bg={bg}] {index} {name} <> "
tab_active "#[fg=#9399B2,bg={bg},bold,italic] {index} {name} {floating_total_count}{floating_indicator}{sync_indicator}{fullscreen_indicator}"
tab_separator "#[fg=#6C7086,bg={bg}] | "
tab_floating_indicator "F"
tab_sync_indicator "S"
tab_fullscreen_indicator "FS"
tab_display_count "3"
tab_truncate_start_format "#[fg=red,bg=#181825] < +{count} ..."
tab_truncate_end_format "#[fg=red,bg=#181825] ... +{count} >"
tab_truncate_start_format "#[fg=red,bg={bg}] < +{count} ..."
tab_truncate_end_format "#[fg=red,bg={bg}] ... +{count} >"

command_0_command "echo \"平仮名, ひらがな 📦\""
command_0_clickaction "bash -c \"zellij --session zjstatus-dev pipe 'zjstatus::notify::hello world!' -n zjstatus\""
Expand All @@ -61,12 +65,12 @@ layout {
command_git_branch_format "#[fg=red] {stdout} "
command_git_branch_interval "2"

command_3_command "echo -e \"#[italic,bold] foo #[dim,bold,italic] bar \""
command_3_command "echo -e \"#[fg={yellow},italic,bold] foo #[dim,bold,italic] bar \""
command_3_format "{stdout}"
command_3_interval "10"
command_3_rendermode "dynamic"

datetime "#[fg=#6C7086,bg=#181825,bold] {format} #[bg=#6C7086,fg=#181825,bold] {time}"
datetime "#[fg=#6C7086,bg={bg},bold] {format} #[bg=#6C7086,fg={bg},bold] {time}"
datetime_time_format "%H:%M:%S"
datetime_format "%A, %d %b %Y %H:%M"
datetime_timezone "Europe/Berlin"
Expand Down
4 changes: 2 additions & 2 deletions src/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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,
})
}
19 changes: 11 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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![],
}
Expand All @@ -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,
Expand Down
62 changes: 45 additions & 17 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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") {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
10 changes: 6 additions & 4 deletions src/widgets/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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>>()
Expand Down Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
Loading

0 comments on commit 68d8d49

Please sign in to comment.