diff options
| author | mat <git@matdoes.dev> | 2025-05-07 14:24:07 +0900 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-05-07 14:24:07 +0900 |
| commit | 8dff973d2602d065193003815eed39f0c8964ef2 (patch) | |
| tree | 96127c05dc5c9cedaf1cc3e7237267d0f678fe71 /azalea-chat/src | |
| parent | 4a1fdf01217b27d48741bb13dea4f0b5b57d42ca (diff) | |
| download | azalea-drasl-8dff973d2602d065193003815eed39f0c8964ef2.tar.xz | |
support legacy hex colors
Diffstat (limited to 'azalea-chat/src')
| -rw-r--r-- | azalea-chat/src/text_component.rs | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index 7332adfa..62547d0d 100644 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -2,7 +2,11 @@ use std::fmt::Display; use serde::{__private::ser::FlatMapSerializer, Serialize, Serializer, ser::SerializeMap}; -use crate::{FormattedText, base_component::BaseComponent, style::ChatFormatting}; +use crate::{ + FormattedText, + base_component::BaseComponent, + style::{ChatFormatting, TextColor}, +}; /// A component that contains text that's the same in all locales. #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] @@ -69,13 +73,26 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo i += 1; continue; }; - if let Some(formatter) = ChatFormatting::from_code(formatting_code) { + if formatting_code == '#' { + let color = legacy_color_code + .chars() + .skip(i + 1) + // 7 to include the # + .take(7) + .collect::<String>(); + if components.is_empty() || !components.last().unwrap().text.is_empty() { components.push(TextComponent::new("".to_string())); } + let style = &mut components.last_mut().unwrap().base.style; + style.color = TextColor::parse(color); + i += 6; + } else if let Some(formatter) = ChatFormatting::from_code(formatting_code) { + if components.is_empty() || !components.last().unwrap().text.is_empty() { + components.push(TextComponent::new("".to_string())); + } let style = &mut components.last_mut().unwrap().base.style; - // if the formatter is a reset, then we need to reset the style to the default style.apply_formatting(&formatter); } i += 1; @@ -213,4 +230,17 @@ mod tests { ) ); } + + #[test] + fn test_legacy_color_code_with_rgb() { + let component = TextComponent::new("ยง#Ff0000This is a test message".to_string()).get(); + assert_eq!( + component.to_ansi(), + format!( + "{RED}This is a test message{RESET}", + RED = Ansi::rgb(0xff0000), + RESET = Ansi::RESET + ) + ); + } } |
