diff options
Diffstat (limited to 'minecraft-chat/src/text_component.rs')
| -rw-r--r-- | minecraft-chat/src/text_component.rs | 91 |
1 files changed, 62 insertions, 29 deletions
diff --git a/minecraft-chat/src/text_component.rs b/minecraft-chat/src/text_component.rs index 66bde690..a5030fa1 100644 --- a/minecraft-chat/src/text_component.rs +++ b/minecraft-chat/src/text_component.rs @@ -1,4 +1,10 @@ -use crate::{base_component::BaseComponent, component::Component, style::ChatFormatting}; +use std::fmt; + +use crate::{ + base_component::BaseComponent, + component::Component, + style::{ChatFormatting, Style, TextColor}, +}; #[derive(Clone, Debug)] pub struct TextComponent { @@ -10,7 +16,7 @@ const LEGACY_FORMATTING_CODE_SYMBOL: char = '§'; /// Convert a legacy color code string into a Component /// Technically in Minecraft this is done when displaying the text, but AFAIK it's the same as just doing it in TextComponent -pub fn legacy_color_code_to_component(legacy_color_code: &str) -> Component { +pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextComponent { let mut components: Vec<TextComponent> = Vec::with_capacity(1); // iterate over legacy_color_code, if it starts with LEGACY_COLOR_CODE_SYMBOL then read the next character and get the style from that // otherwise, add the character to the text @@ -21,24 +27,15 @@ pub fn legacy_color_code_to_component(legacy_color_code: &str) -> Component { if legacy_color_code.chars().nth(i).unwrap() == LEGACY_FORMATTING_CODE_SYMBOL { let formatting_code = legacy_color_code.chars().nth(i + 1).unwrap(); if let Ok(formatter) = ChatFormatting::from_code(formatting_code) { - if components.is_empty() || components.last().unwrap().text.is_empty() { + if components.is_empty() { + components.push(TextComponent::new("".to_string())); + } else if !components.last().unwrap().text.is_empty() { components.push(TextComponent::new("".to_string())); } - println!( - "applying formatter {:?} {:?}", - components.last_mut().unwrap().base.style, - formatter - ); - components - .last_mut() - .unwrap() - .base - .style - .apply_formatting(formatter); - println!( - "applied formatter {:?}", - components.last_mut().unwrap().base.style - ); + + 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; } else { @@ -60,36 +57,72 @@ pub fn legacy_color_code_to_component(legacy_color_code: &str) -> Component { final_component.base.siblings.push(component.get()); } - final_component.get() + final_component } impl<'a> TextComponent { pub fn new(text: String) -> Self { - Self { - base: BaseComponent::new(), - text, + // if it contains a LEGACY_FORMATTING_CODE_SYMBOL, format it + if text.contains(LEGACY_FORMATTING_CODE_SYMBOL) { + legacy_color_code_to_text_component(&text) + } else { + Self { + base: BaseComponent::new(), + text, + } } } - pub fn to_string(&self) -> String { - self.text.clone() - } - fn get(self) -> Component { Component::Text(self) } } +impl fmt::Display for TextComponent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.text.clone()) + } +} + #[cfg(test)] mod tests { + use crate::style::Ansi; + use super::*; #[test] + fn test_hypixel_motd() { + let component = + TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_string()) + .get(); + assert_eq!( + component.to_ansi(None), + format!( + "{GREEN}Hypixel Network {RED}[1.8-1.18]\n{BOLD}{AQUA}HAPPY HOLIDAYS{RESET}", + GREEN = Ansi::rgb(ChatFormatting::GREEN.color.unwrap()), + RED = Ansi::rgb(ChatFormatting::RED.color.unwrap()), + AQUA = Ansi::rgb(ChatFormatting::AQUA.color.unwrap()), + BOLD = Ansi::BOLD, + RESET = Ansi::RESET + ) + ); + } + + #[test] fn test_legacy_color_code_to_component() { - let component = legacy_color_code_to_component("§lHello §r§1w§2o§3r§4l§5d"); + let component = TextComponent::new("§lHello §r§1w§2o§3r§4l§5d".to_string()).get(); assert_eq!( - component.to_ansi(), - "\u{1b}[38;2;170;0;170mHello world\u{1b}[m" + component.to_ansi(None), + format!( + "{BOLD}Hello {RESET}{DARK_BLUE}w{DARK_GREEN}o{DARK_AQUA}r{DARK_RED}l{DARK_PURPLE}d{RESET}", + BOLD = Ansi::BOLD, + RESET = Ansi::RESET, + DARK_BLUE = Ansi::rgb(ChatFormatting::DARK_BLUE.color.unwrap()), + DARK_GREEN = Ansi::rgb(ChatFormatting::DARK_GREEN.color.unwrap()), + DARK_AQUA = Ansi::rgb(ChatFormatting::DARK_AQUA.color.unwrap()), + DARK_RED = Ansi::rgb(ChatFormatting::DARK_RED.color.unwrap()), + DARK_PURPLE = Ansi::rgb(ChatFormatting::DARK_PURPLE.color.unwrap()) + ) ); } } |
