feat: added support for formatting strings
This commit is contained in:
parent
1fbdd2463c
commit
137c3b1456
|
|
@ -18,3 +18,4 @@ log = "0.4"
|
|||
signal-hook = "0.3.*"
|
||||
toml = "0.5.*"
|
||||
clap = { version = "4.2.*", features = ["derive"] }
|
||||
dyn-fmt = "0.4.0"
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ mod structs;
|
|||
mod print_players;
|
||||
|
||||
/// This function deals with an incoming (USR1) signal.
|
||||
/// It is hard-coded to play/payse the active player.
|
||||
/// It is hard-coded to play/pause the active player.
|
||||
///
|
||||
/// input:
|
||||
/// data: Data struct for active configuration.
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
use std::collections::HashMap;
|
||||
use log::{info, error};
|
||||
use string_builder::Builder;
|
||||
use dyn_fmt::AsStrFormatExt;
|
||||
|
||||
use crate::structs::{config::{Field, Config}, data::Data};
|
||||
|
||||
|
|
@ -34,7 +35,7 @@ fn get_char_boundary(str: &str, max_len: usize) -> usize {
|
|||
}
|
||||
}
|
||||
|
||||
/// This function applies truncation to each strings in the given hashmap, as dictated by the values in the given Fields.
|
||||
/// This function applies truncation to each string in the given hashmap, as dictated by the values in the given Fields.
|
||||
/// It also applies fuzzy cutoff if the configuration option for this is enabled.
|
||||
///
|
||||
/// Input:
|
||||
|
|
@ -80,7 +81,7 @@ 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(string.as_str());
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -1,14 +1,17 @@
|
|||
//! This file contains structs and functions concerning themselves with the configuration of the program.
|
||||
use std::{collections::HashMap};
|
||||
use std::collections::HashMap;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
/// This struct represents one metadata field to be rendered, as well as the maximum length of its' output.
|
||||
/// There is also support for custom formatting.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Field {
|
||||
/// The name of the metadata field.
|
||||
pub field: String,
|
||||
/// The maximum length of the metadata field's output.
|
||||
pub num_chars: u8
|
||||
pub num_chars: u32,
|
||||
/// Formatting to apply. (the value "{}" is substituted with the actual string)
|
||||
pub format: String
|
||||
}
|
||||
|
||||
impl Field {
|
||||
|
|
@ -16,13 +19,15 @@ impl Field {
|
|||
/// input:
|
||||
/// field: name of the field
|
||||
/// num_chars: maximum length of the field
|
||||
/// format: what formatting to apply to the field
|
||||
///
|
||||
/// returns:
|
||||
/// a new Field with the given parameters.
|
||||
fn new(field: String, num_chars: u8) -> Self {
|
||||
fn new(field: String, num_chars: u32, format: String) -> Self {
|
||||
Field {
|
||||
field,
|
||||
num_chars
|
||||
num_chars,
|
||||
format
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,11 +35,16 @@ impl Field {
|
|||
/// input:
|
||||
/// field: name of the field
|
||||
/// num_chars: maximum length of the field
|
||||
/// format: (optional), formatting to apply.
|
||||
///
|
||||
/// returns:
|
||||
/// a new Field with the given parameters.
|
||||
pub fn constructor(field: &str, num_chars: u8) -> Self {
|
||||
Self::new(field.to_owned(), num_chars)
|
||||
pub fn constructor(field: &str, num_chars: u32, format: Option<String>) -> Self {
|
||||
if let Some(format) = format {
|
||||
Self::new(field.to_owned(), num_chars, format)
|
||||
} else {
|
||||
Self::new(field.to_owned(), num_chars, "{}".to_owned())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -189,8 +199,8 @@ impl Config {
|
|||
/// It contains the "title" and "artist" fields, with 40 and 20 maximum characters respectively.
|
||||
fn default_metadata_fields() -> Vec<Field> {
|
||||
vec![
|
||||
Field::constructor("xesam:title", 40),
|
||||
Field::constructor("xesam:artist", 20)
|
||||
Field::constructor("xesam:title", 40, None),
|
||||
Field::constructor("xesam:artist", 20, None)
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! This file deals with updating the actual message, including proper formatting.
|
||||
use log::{debug, trace};
|
||||
use mpris::{MetadataValue};
|
||||
use mpris::MetadataValue;
|
||||
|
||||
use crate::structs::{config::Config, data::Data};
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ fn update_prefix(cfg: &Config, data: &mut char, name: &str) {
|
|||
/// pf: PlayerFinder instance of the program.
|
||||
/// cfg: Config struct of the program, containing the list of acceptable players.
|
||||
/// data: mutable Data struct of the program, containing a marker for the currently active player.
|
||||
pub fn update_players(pf: &PlayerFinder, cfg: &Config, mut data: &mut Data) {
|
||||
pub fn update_players(pf: &PlayerFinder, cfg: &Config, data: &mut Data) {
|
||||
// get all acceptable players
|
||||
let players = pf.find_all().unwrap_or(Vec::new());
|
||||
if players.is_empty() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue