Add `Styled<L: Localize>` struct to support localizing colors

finchie
Aug 6, 2025, 12:38 PM
AE3AZFVKJBURLY6T6H5477BSP5LISUQYPSPDRSPXRO435KGYTRZAC

Dependencies

  • [2] E64LCUDQ Use `camino::Utf8PathBuf` instead of `std::path::PathBuf`
  • [*] HHJDRLLN Create `fluent_embed_runtime` crate
  • [*] WWDZWJTR Implement `Localize` for string types
  • [*] BFL2Y7GN Add relative timestamps using `jiff` and `icu_relativetime`
  • [*] 7M4UI3TW Update dependencies to latest versions
  • [*] VZYZRAO4 Move `output-macros` crate into workspace
  • [*] UKFEFT6L Create basic `Output` proc-macro
  • [*] VQBJBFEX Improve error handling for missing Fluent messages

Change contents

  • file addition: style.rs (----------)
    [4.49]
    use crate::Localize;
    use anstyle::{AnsiColor, Color, Style};
    macro_rules! style_attribute {
    ($function_name:ident) => {
    pub fn $function_name(mut self) -> Self {
    self.style = self.style.map(|style| style.$function_name());
    self
    }
    };
    }
    pub struct Styled<L: Localize> {
    message: L,
    style: Option<Style>,
    }
    impl<L: Localize> Localize for Styled<L> {
    fn message_for_locale(&self, locale: &icu_locale::Locale) -> String {
    let message = self.message.message_for_locale(locale);
    match self.style {
    Some(style) => format!("{style}{message}{style:#}"),
    None => message,
    }
    }
    }
    impl<L: Localize> Styled<L> {
    pub fn new(message: L, enabled: bool) -> Self {
    Self {
    message,
    style: match enabled {
    true => Some(Style::new()),
    false => None,
    },
    }
    }
    style_attribute!(bold);
    style_attribute!(dimmed);
    style_attribute!(italic);
    style_attribute!(strikethrough);
    pub fn color(mut self, color: AnsiColor) -> Self {
    self.style = self
    .style
    .map(|style| style.fg_color(Some(Color::Ansi(color))));
    self
    }
    }
  • edit in fluent_embed/src/lib.rs at line 15
    [5.875]
    [6.4362]
    pub mod style;
  • edit in fluent_embed/Cargo.toml at line 10
    [4.1196]
    [7.5176]
    anstyle.workspace = true
  • edit in Cargo.toml at line 12
    [7.5579]
    [2.1216]
    anstyle = "1.0"
  • edit in Cargo.lock at line 30
    [7.6972]
    [7.6972]
    name = "anstyle"
    version = "1.0.11"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
    [[package]]
  • edit in Cargo.lock at line 322
    [10.9112]
    [10.9126]
    "anstyle",