feat: added escape_chars config option

This commit is contained in:
Djairo Hougee 2024-01-21 19:22:22 +01:00
parent c56a59b126
commit c074b4279e
3 changed files with 21 additions and 1 deletions

View File

@ -126,6 +126,9 @@ fuzzy = false
# whether to display the prefix characters in the output string at all
# boolean
render_prefix = true
# whether to escape special characters like '&' to '&'
# boolean
escape_chars = false
# time taken between updates of the output string, in milliseconds
# u64 (0 <= u64 <= 18446744073709551615)
@ -208,6 +211,7 @@ Whenever a config file is specified that does not actually exist, the script cre
hide_output = true
fuzzy = false
render_prefix = true
escape_chars = false
update_delay = 300
metadata_separator = ' | '
array_separator = '+'

View File

@ -81,7 +81,18 @@ fn append_fields(b: &mut Builder, cfg: &Config, data: &Data) {
for field in &cfg.metadata_fields {
if let Some(string) = data.field_text.get(&field.field) {
idx += 1;
b.append(field.format.format(&[string.as_str()]));
if cfg.escape_chars {
let s: &String = &string.chars()
.map(|x| match x {
'&' => "&amp;".to_owned(),
_ => x.to_string(),
}).collect();
b.append(field.format.format(&[s.as_str()]));
} else {
b.append(field.format.format(&[string.as_str()]));
}
if idx < len {b.append(cfg.metadata_separator.as_str())};
} else {
info!("failed to get {} value!", field.field);

View File

@ -140,6 +140,10 @@ pub struct Config {
/// Hashmap which maps Player Identities (strings; key) to prefixes (char; value).
/// If left blank all players will use the default prefix character ('>').
pub player_prefixes: HashMap<String, char>,
/// Boolean which tells the program to escape special characters or not.
/// This is useful for some bar implementations (i.e. waybar needs to escape the '&' character).
/// Currently only escapes '&', i will be adding more as i run into them.
pub escape_chars: bool,
}
/// Defaults for the Config struct.
@ -158,6 +162,7 @@ impl Default for Config {
rating_icons: Some(Rating::default()),
metadata_fields: Config::default_metadata_fields(),
player_prefixes: Config::default_player_prefixes(),
escape_chars: false,
}
}
}