diff --git a/README.md b/README.md index 5f277ad..9a3dfe7 100644 --- a/README.md +++ b/README.md @@ -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 = '+' diff --git a/src/print_text.rs b/src/print_text.rs index dc1de54..2cd874e 100644 --- a/src/print_text.rs +++ b/src/print_text.rs @@ -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 { + '&' => "&".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); diff --git a/src/structs/config.rs b/src/structs/config.rs index 0b03c5d..25c3285 100644 --- a/src/structs/config.rs +++ b/src/structs/config.rs @@ -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, + /// 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, } } }