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,10 +1,12 @@
//! This file deals with formatting and outputting to stdout.
use log::{error, info};
use std::collections::HashMap;
use log::{info, error};
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.
/// If there is no whitespace it returns usize::MAX instead.
@ -31,7 +33,7 @@ fn get_char_boundary(str: &str, max_len: usize) -> usize {
idx -= 1;
}
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.
/// 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.
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 {
if let Some(str) = strings.get_mut(&field.field) {
if str.len() >= 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 {
str.push(c);
}
@ -83,17 +92,21 @@ fn append_fields(b: &mut Builder, cfg: &Config, data: &Data) {
idx += 1;
if cfg.escape_chars {
let s: &String = &string.chars()
let s: &String = &string
.chars()
.map(|x| match x {
'&' => "&amp;".to_owned(),
_ => x.to_string(),
}).collect();
})
.collect();
b.append(field.format.format(&[s.as_str()]));
b.append(field.format.replace("{}", s.as_str()));
} 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 {
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.
/// data: mutable Data struct containing the state of the program.
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!("");
} 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));
}
}