Enum FormattedText
pub enum FormattedText {
Text(TextComponent),
Translatable(TranslatableComponent),
}
Expand description
A chat component, basically anything you can see in chat.
Variants§
Implementations§
§impl FormattedText
A chat component
impl FormattedText
A chat component
pub fn get_base_mut(&mut self) -> &mut BaseComponent
pub fn get_base(&self) -> &BaseComponent
pub fn to_custom_format<F, S, C>(
&self,
style_formatter: F,
text_formatter: S,
cleanup_formatter: C,
default_style: &Style,
) -> String
pub fn to_custom_format<F, S, C>( &self, style_formatter: F, text_formatter: S, cleanup_formatter: C, default_style: &Style, ) -> String
Render all components into a single String
, using your custom
closures to drive styling, text transformation, and final cleanup.
§Type params
F
:(running, component, default) -> (prefix, suffix)
for per-component stylingS
:&str -> String
for text tweaks (escaping, mapping, etc.)C
:&final_running_style -> String
for any trailing cleanup
§Args
style_formatter
: how to open/close each component’s styletext_formatter
: how to turn raw text into output textcleanup_formatter
: emit after all components (e.g. reset codes)default_style
: where to reset when a component’sreset
is true
§Example
use azalea_chat::{FormattedText, DEFAULT_STYLE};
use serde::de::Deserialize;
let component = FormattedText::deserialize(&serde_json::json!({
"text": "Hello, world!",
"color": "red",
})).unwrap();
let ansi = component.to_custom_format(
|running, new| (running.compare_ansi(new), "".to_string()),
|text| text.to_string(),
|style| {
if !style.is_empty() {
"\u{1b}[m".to_string()
} else {
"".to_string()
}
},
&DEFAULT_STYLE,
);
println!("{ansi}");
pub fn to_ansi_with_custom_style(&self, default_style: &Style) -> String
pub fn to_ansi_with_custom_style(&self, default_style: &Style) -> String
Convert this component into an ANSI string.
This is the same as FormattedText::to_ansi
, but you can specify a
default [Style
] to use.
pub fn to_ansi(&self) -> String
pub fn to_ansi(&self) -> String
Convert this component into an ANSI string, so you can print it to your terminal and get styling.
This is technically a shortcut for
FormattedText::to_ansi_with_custom_style
with a default [Style
]
colored white.
If you don’t want the result to be styled at all, use
Self::to_string
.
§Examples
use azalea_chat::FormattedText;
use serde::de::Deserialize;
let component = FormattedText::deserialize(&serde_json::json!({
"text": "Hello, world!",
"color": "red",
})).unwrap();
println!("{}", component.to_ansi());
pub fn to_html(&self) -> String
pub fn to_html(&self) -> String
Similar to Self::to_ansi
but renders the result as HTML instead.
§impl FormattedText
impl FormattedText
pub fn from_nbt_compound(compound: NbtCompound<'_, '_>) -> Option<FormattedText>
Trait Implementations§
§impl AzaleaRead for FormattedText
impl AzaleaRead for FormattedText
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<FormattedText, BufReadError>
§impl AzaleaWrite for FormattedText
impl AzaleaWrite for FormattedText
§impl Clone for FormattedText
impl Clone for FormattedText
§fn clone(&self) -> FormattedText
fn clone(&self) -> FormattedText
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl Debug for FormattedText
impl Debug for FormattedText
§impl Default for FormattedText
impl Default for FormattedText
§fn default() -> FormattedText
fn default() -> FormattedText
§impl<'de> Deserialize<'de> for FormattedText
impl<'de> Deserialize<'de> for FormattedText
§fn deserialize<D>(
de: D,
) -> Result<FormattedText, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
de: D,
) -> Result<FormattedText, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl Display for FormattedText
impl Display for FormattedText
§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Render the text in the component but without any formatting/styling.
If you want the text to be styled, consider using Self::to_ansi
or
Self::to_html
.
§impl From<&Mutf8Str> for FormattedText
impl From<&Mutf8Str> for FormattedText
§fn from(s: &Mutf8Str) -> FormattedText
fn from(s: &Mutf8Str) -> FormattedText
§impl From<&str> for FormattedText
impl From<&str> for FormattedText
§fn from(s: &str) -> FormattedText
fn from(s: &str) -> FormattedText
§impl From<String> for FormattedText
impl From<String> for FormattedText
§fn from(s: String) -> FormattedText
fn from(s: String) -> FormattedText
§impl From<TextComponent> for FormattedText
impl From<TextComponent> for FormattedText
§fn from(c: TextComponent) -> FormattedText
fn from(c: TextComponent) -> FormattedText
§impl From<TranslatableComponent> for FormattedText
impl From<TranslatableComponent> for FormattedText
§fn from(c: TranslatableComponent) -> FormattedText
fn from(c: TranslatableComponent) -> FormattedText
§impl FromNbtTag for FormattedText
impl FromNbtTag for FormattedText
fn from_nbt_tag(tag: NbtTag<'_, '_>) -> Option<FormattedText>
fn from_optional_nbt_tag( tag: Option<NbtTag<'_, '_>>, ) -> Result<Option<Self>, DeserializeError>
§impl IntoIterator for FormattedText
impl IntoIterator for FormattedText
§fn into_iter(self) -> <FormattedText as IntoIterator>::IntoIter
fn into_iter(self) -> <FormattedText as IntoIterator>::IntoIter
Recursively call the function for every component in this component
§type Item = FormattedText
type Item = FormattedText
§type IntoIter = IntoIter<<FormattedText as IntoIterator>::Item>
type IntoIter = IntoIter<<FormattedText as IntoIterator>::Item>
§impl PartialEq for FormattedText
impl PartialEq for FormattedText
§impl Serialize for FormattedText
impl Serialize for FormattedText
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
§impl Serialize for FormattedText
impl Serialize for FormattedText
fn to_compound(self) -> NbtCompound
fn to_nbt(self) -> BaseNbt
impl StructuralPartialEq for FormattedText
Auto Trait Implementations§
impl Freeze for FormattedText
impl RefUnwindSafe for FormattedText
impl Send for FormattedText
impl Sync for FormattedText
impl Unpin for FormattedText
impl UnwindSafe for FormattedText
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
]. Read more§impl<T> ToNbtTag for Twhere
T: Serialize,
impl<T> ToNbtTag for Twhere
T: Serialize,
fn to_nbt_tag(self) -> NbtTag
fn to_optional_nbt_tag(self) -> Option<NbtTag>
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.