From e99a822995c80e1f95c5f7a69e0d8c5d131af20f Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 4 Dec 2022 20:58:14 -0600 Subject: [PATCH] change to_ansi to not take args --- azalea-chat/src/component.rs | 16 +++++++++++++--- azalea-chat/src/text_component.rs | 4 ++-- azalea-chat/tests/integration_test.rs | 6 +++--- azalea-client/src/ping.rs | 2 +- azalea/src/lib.rs | 2 +- azalea/src/swarm/mod.rs | 2 +- bot/src/main.rs | 2 +- 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 95387248..b60beaf5 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -60,6 +60,9 @@ impl Component { /// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code), so you /// can print it to your terminal and get styling. /// + /// This is technically a shortcut for [`Component::to_ansi_custom_style`] with a + /// default [`Style`] colored white. + /// /// # Examples /// /// ```rust @@ -71,12 +74,19 @@ impl Component { /// "color": "red", /// })).unwrap(); /// - /// println!("{}", component.to_ansi(None)); + /// println!("{}", component.to_ansi()); /// ``` - pub fn to_ansi(&self, default_style: Option<&Style>) -> String { + pub fn to_ansi(&self) -> String { // default the default_style to white if it's not set - let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE); + self.to_ansi_custom_style(&DEFAULT_STYLE) + } + /// Convert this component into an + /// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code). + /// + /// This is the same as [`Component::to_ansi`], but you can specify a + /// default [`Style`] to use. + pub fn to_ansi_custom_style(&self, default_style: &Style) -> String { // this contains the final string will all the ansi escape codes let mut built_string = String::new(); // this style will update as we visit components diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index 6715c93e..e5cc054e 100755 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -127,7 +127,7 @@ mod tests { TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_string()) .get(); assert_eq!( - component.to_ansi(None), + component.to_ansi(), format!( "{GREEN}Hypixel Network {RED}[1.8-1.18]\n{BOLD}{AQUA}HAPPY HOLIDAYS{RESET}", GREEN = Ansi::rgb(ChatFormatting::Green.color().unwrap()), @@ -143,7 +143,7 @@ mod tests { fn test_legacy_color_code_to_component() { let component = TextComponent::new("§lHello §r§1w§2o§3r§4l§5d".to_string()).get(); assert_eq!( - component.to_ansi(None), + component.to_ansi(), format!( "{BOLD}Hello {RESET}{DARK_BLUE}w{DARK_GREEN}o{DARK_AQUA}r{DARK_RED}l{DARK_PURPLE}d{RESET}", BOLD = Ansi::BOLD, diff --git a/azalea-chat/tests/integration_test.rs b/azalea-chat/tests/integration_test.rs index cc976888..35fab8d3 100755 --- a/azalea-chat/tests/integration_test.rs +++ b/azalea-chat/tests/integration_test.rs @@ -17,7 +17,7 @@ fn basic_ansi_test() { .unwrap(); let component = Component::deserialize(&j).unwrap(); assert_eq!( - component.to_ansi(None), + component.to_ansi(), "\u{1b}[1m\u{1b}[38;2;255;85;85mhello\u{1b}[m" ); } @@ -53,7 +53,7 @@ fn complex_ansi_test() { .unwrap(); let component = Component::deserialize(&j).unwrap(); assert_eq!( - component.to_ansi(None), + component.to_ansi(), format!( "{bold}{italic}{underlined}{red}hello{reset}{bold}{italic}{red} {reset}{italic}{strikethrough}{abcdef}world{reset}{abcdef} asdf{bold}!{reset}", bold = Ansi::BOLD, @@ -71,5 +71,5 @@ fn complex_ansi_test() { fn component_from_string() { let j: Value = serde_json::from_str("\"foo\"").unwrap(); let component = Component::deserialize(&j).unwrap(); - assert_eq!(component.to_ansi(None), "foo"); + assert_eq!(component.to_ansi(), "foo"); } diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs index ed4197ba..289622b6 100755 --- a/azalea-client/src/ping.rs +++ b/azalea-client/src/ping.rs @@ -40,7 +40,7 @@ pub enum PingError { /// #[tokio::main] /// async fn main() { /// let response = ping::ping_server("play.hypixel.net").await.unwrap(); -/// println!("{}", response.description.to_ansi(None)); +/// println!("{}", response.description.to_ansi()); /// } /// ``` pub async fn ping_server( diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index 05d92f33..462459e7 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -64,7 +64,7 @@ //! async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> { //! match event { //! Event::Chat(m) => { -//! println!(m.message().to_ansi(None)); +//! println!(m.message().to_ansi()); //! } //! _ => {} //! } diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index c45014d2..4956adde 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -197,7 +197,7 @@ pub enum SwarmStartError { /// swarm.add(account, State::default()).await?; /// } /// SwarmEvent::Chat(m) => { -/// println!("{}", m.message().to_ansi(None)); +/// println!("{}", m.message().to_ansi()); /// } /// _ => {} /// } diff --git a/bot/src/main.rs b/bot/src/main.rs index e50da728..2b85d802 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -136,7 +136,7 @@ async fn swarm_handle( swarm.add(account, State::default()).await?; } SwarmEvent::Chat(m) => { - println!("swarm chat message: {}", m.message().to_ansi(None)); + println!("swarm chat message: {}", m.message().to_ansi()); if m.message().to_string() == " world" { for (name, world) in &swarm.worlds.read().worlds { println!("world name: {}", name);