diff options
Diffstat (limited to 'minecraft-chat/src/component.rs')
| -rw-r--r-- | minecraft-chat/src/component.rs | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/minecraft-chat/src/component.rs b/minecraft-chat/src/component.rs index aa2c598e..c0e4f8ac 100644 --- a/minecraft-chat/src/component.rs +++ b/minecraft-chat/src/component.rs @@ -1,15 +1,14 @@ +use std::borrow::BorrowMut; + use serde_json; use crate::{ base_component::BaseComponent, + style::Style, text_component::TextComponent, translatable_component::{StringOrComponent, TranslatableComponent}, }; -// pub struct Component { -// base: BaseComponent, -// } - #[derive(Clone)] pub enum Component { TextComponent(TextComponent), @@ -149,14 +148,18 @@ impl Component { Ok(component) } - /// Add a component as a sibling of this one - fn append(&mut self, sibling: Component) { + pub fn get_base(&mut self) -> &mut BaseComponent { match self { - Self::TextComponent(c) => c.base.siblings.push(sibling), - Self::TranslatableComponent(c) => c.base.siblings.push(sibling), + Self::TextComponent(c) => &mut c.base, + Self::TranslatableComponent(c) => &mut c.base, } } + /// Add a component as a sibling of this one + fn append(&mut self, sibling: Component) { + self.get_base().siblings.push(sibling); + } + /// Get the "separator" component from the json fn parse_separator(json: &serde_json::Value) -> Result<Option<Component>, String> { if json.get("separator").is_some() { @@ -165,5 +168,38 @@ impl Component { Ok(None) } - fn to_ansi(&self) {} + /// Convert this component into an ansi string, using parent_style as the running style. + pub fn to_ansi(&self, parent_style: Option<&mut Style>) -> String { + // the siblings of this component + let base; + let mut text; + match self { + Self::TextComponent(c) => { + base = &c.base; + text = c.text.clone(); + } + Self::TranslatableComponent(c) => { + base = &c.base; + text = c.key.clone(); + } + }; + + // we'll fall back to this if there's no parent style + let default_style = &mut Style::new(); + + // apply the style of this component to the current style + let current_style: &mut Style = parent_style.unwrap_or(default_style); + let new_style = &base.style; + current_style.apply(new_style); + + let ansi_text = base.style.compare_ansi(&new_style); + + text.push_str(&ansi_text); + + for sibling in &base.siblings { + text.push_str(&sibling.to_ansi(Some(current_style))); + } + + text.clone() + } } |
