fix: fixed a bug where incorrect characters are replaced from the format string

This commit is contained in:
Djairo Hougee 2024-04-23 20:50:15 +02:00
parent d34e842f3e
commit 2ea0e57338
1 changed files with 90 additions and 69 deletions

View File

@ -1,15 +1,17 @@
//! This file deals with formatting and outputting to stdout. //! This file deals with formatting and outputting to stdout.
use log::{error, info};
use std::collections::HashMap; use std::collections::HashMap;
use log::{info, error};
use string_builder::Builder; use string_builder::Builder;
use dyn_fmt::AsStrFormatExt;
use crate::structs::{config::{Field, Config}, data::Data}; use crate::structs::{
config::{Config, Field},
data::Data,
};
/// This function finds the last whitespace in a string and returns its' index. /// This function finds the last whitespace in a string and returns its' index.
/// If there is no whitespace it returns usize::MAX instead. /// If there is no whitespace it returns usize::MAX instead.
fn fuzzy_cutoff(str: &str) -> usize { fn fuzzy_cutoff(str: &str) -> usize {
str.rfind(char::is_whitespace).unwrap_or_else( || usize::MAX) str.rfind(char::is_whitespace).unwrap_or_else(|| usize::MAX)
} }
/// This function helps deal with non-UTF8 strings. /// This function helps deal with non-UTF8 strings.
@ -31,7 +33,7 @@ fn get_char_boundary(str: &str, max_len: usize) -> usize {
idx -= 1; idx -= 1;
} }
idx idx
}, }
} }
} }
@ -43,12 +45,19 @@ fn get_char_boundary(str: &str, max_len: usize) -> usize {
/// brk: Optional, character to insert when a string is truncated. /// brk: Optional, character to insert when a string is truncated.
/// fuzzy: Whether to apply the fuzzy truncation function or not. /// fuzzy: Whether to apply the fuzzy truncation function or not.
/// strings: Hashmap containing the strings to be truncated. Key values should match with the names of the Fields Vec. /// strings: Hashmap containing the strings to be truncated. Key values should match with the names of the Fields Vec.
fn cutoff(fields: &Vec<Field>, brk: Option<char>, fuzzy: bool, strings: &mut HashMap<String, String>) { fn cutoff(
fields: &Vec<Field>,
brk: Option<char>,
fuzzy: bool,
strings: &mut HashMap<String, String>,
) {
for field in fields { for field in fields {
if let Some(str) = strings.get_mut(&field.field) { if let Some(str) = strings.get_mut(&field.field) {
if str.len() >= field.num_chars as usize { if str.len() >= field.num_chars as usize {
str.truncate(get_char_boundary(str, field.num_chars as usize)); str.truncate(get_char_boundary(str, field.num_chars as usize));
if fuzzy {str.truncate(fuzzy_cutoff(str))} if fuzzy {
str.truncate(fuzzy_cutoff(str))
}
if let Some(c) = brk { if let Some(c) = brk {
str.push(c); str.push(c);
} }
@ -83,17 +92,21 @@ fn append_fields(b: &mut Builder, cfg: &Config, data: &Data) {
idx += 1; idx += 1;
if cfg.escape_chars { if cfg.escape_chars {
let s: &String = &string.chars() let s: &String = &string
.chars()
.map(|x| match x { .map(|x| match x {
'&' => "&amp;".to_owned(), '&' => "&amp;".to_owned(),
_ => x.to_string(), _ => x.to_string(),
}).collect(); })
.collect();
b.append(field.format.format(&[s.as_str()])); b.append(field.format.replace("{}", s.as_str()));
} else { } else {
b.append(field.format.format(&[string.as_str()])); b.append(field.format.replace("{}", string.as_str()));
} }
if idx < len {b.append(cfg.metadata_separator.as_str())}; if idx < len {
b.append(cfg.metadata_separator.as_str())
};
} else { } else {
info!("failed to get {} value!", field.field); info!("failed to get {} value!", field.field);
} }
@ -131,10 +144,18 @@ fn build_string(cfg: &Config, data: &Data) -> String {
/// cfg: Config struct for the program. /// cfg: Config struct for the program.
/// data: mutable Data struct containing the state of the program. /// data: mutable Data struct containing the state of the program.
pub fn print_text(cfg: &Config, data: &mut Data) { pub fn print_text(cfg: &Config, data: &mut Data) {
if (cfg.hide_output && data.current_player.is_none()) || data.field_text.is_empty() || cfg.metadata_fields.is_empty() { if (cfg.hide_output && data.current_player.is_none())
|| data.field_text.is_empty()
|| cfg.metadata_fields.is_empty()
{
println!(""); println!("");
} else { } else {
cutoff(&cfg.metadata_fields, cfg.break_character, cfg.fuzzy, &mut data.field_text); cutoff(
&cfg.metadata_fields,
cfg.break_character,
cfg.fuzzy,
&mut data.field_text,
);
println!("{}", build_string(cfg, data)); println!("{}", build_string(cfg, data));
} }
} }