diff --git a/Cargo.lock b/Cargo.lock index 28e90afa..1bb3825f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1229,6 +1229,7 @@ dependencies = [ name = "pumpkin-core" version = "0.1.0" dependencies = [ + "colored", "fastnbt", "serde", "uuid", diff --git a/pumpkin-core/Cargo.toml b/pumpkin-core/Cargo.toml index 34ad5239..a77ec031 100644 --- a/pumpkin-core/Cargo.toml +++ b/pumpkin-core/Cargo.toml @@ -7,3 +7,4 @@ edition.workspace = true serde = { version = "1.0", features = ["derive"] } fastnbt = { git = "https://github.com/owengage/fastnbt.git" } uuid.workspace = true +colored = "2" \ No newline at end of file diff --git a/pumpkin-core/src/text/_README.md b/pumpkin-core/src/text/README.md similarity index 100% rename from pumpkin-core/src/text/_README.md rename to pumpkin-core/src/text/README.md diff --git a/pumpkin-core/src/text/color.rs b/pumpkin-core/src/text/color.rs index 6fa25219..94d83f5b 100644 --- a/pumpkin-core/src/text/color.rs +++ b/pumpkin-core/src/text/color.rs @@ -1,3 +1,4 @@ +use colored::{ColoredString, Colorize}; use serde::{Deserialize, Serialize}; /// Text color @@ -13,6 +14,32 @@ pub enum Color { Named(NamedColor), } +impl Color { + pub fn console_color(&self, text: &str) -> ColoredString { + match self { + Color::Reset => text.clear(), + Color::Named(color) => match color { + NamedColor::Black => text.black(), + NamedColor::DarkBlue => text.blue(), + NamedColor::DarkGreen => text.green(), + NamedColor::DarkAqua => text.cyan(), + NamedColor::DarkRed => text.red(), + NamedColor::DarkPurple => text.purple(), + NamedColor::Gold => text.yellow(), + NamedColor::Gray => text.bright_black(), + NamedColor::DarkGray => text.bright_black(), // ? + NamedColor::Blue => text.bright_blue(), + NamedColor::Green => text.bright_green(), + NamedColor::Aqua => text.cyan(), + NamedColor::Red => text.red(), + NamedColor::LightPurple => text.bright_purple(), + NamedColor::Yellow => text.bright_yellow(), + NamedColor::White => text.white(), + }, + } + } +} + /// Named Minecraft color #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] diff --git a/pumpkin-core/src/text/mod.rs b/pumpkin-core/src/text/mod.rs index 9bd4499d..706c034e 100644 --- a/pumpkin-core/src/text/mod.rs +++ b/pumpkin-core/src/text/mod.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use click::ClickEvent; use color::Color; +use colored::Colorize; use fastnbt::SerOpts; use hover::HoverEvent; use serde::{Deserialize, Serialize}; @@ -38,6 +39,36 @@ impl<'a> TextComponent<'a> { style: Style::default(), } } + + pub fn to_pretty_console(self) -> String { + let style = self.style; + let color = style.color; + let mut text = match self.content { + TextContent::Text { text } => text.into_owned(), + TextContent::Translate { translate, with: _ } => translate.into_owned(), + TextContent::EntityNames { + selector, + separator: _, + } => selector.into_owned(), + TextContent::Keybind { keybind } => keybind.into_owned(), + }; + if let Some(color) = color { + text = color.console_color(&text).to_string(); + } + if style.bold.is_some() { + text = text.bold().to_string(); + } + if style.italic.is_some() { + text = text.italic().to_string(); + } + if style.underlined.is_some() { + text = text.underline().to_string(); + } + if style.strikethrough.is_some() { + text = text.strikethrough().to_string(); + } + text + } } impl<'a> serde::Serialize for TextComponent<'a> { diff --git a/pumpkin/src/commands/mod.rs b/pumpkin/src/commands/mod.rs index d6b2f4bd..8eea87a1 100644 --- a/pumpkin/src/commands/mod.rs +++ b/pumpkin/src/commands/mod.rs @@ -24,7 +24,7 @@ impl<'a> CommandSender<'a> { pub fn send_message(&mut self, text: TextComponent) { match self { // TODO: add color and stuff to console - CommandSender::Console => log::info!("{:?}", text.content), + CommandSender::Console => log::info!("{}", text.to_pretty_console()), CommandSender::Player(c) => c.send_system_message(text), CommandSender::Rcon(s) => s.push(format!("{:?}", text.content)), } diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index e4db1387..69f839c9 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -94,7 +94,9 @@ fn main() -> io::Result<()> { stdin .read_line(&mut out) .expect("Failed to read console line"); - handle_command(&mut commands::CommandSender::Console, &out); + if !out.is_empty() { + handle_command(&mut commands::CommandSender::Console, &out); + } } }); }