Skip to content

Commit

Permalink
Add Console Colors & Styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 21, 2024
1 parent ee4bde9 commit 13206d5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pumpkin-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
File renamed without changes.
27 changes: 27 additions & 0 deletions pumpkin-core/src/text/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use colored::{ColoredString, Colorize};
use serde::{Deserialize, Serialize};

/// Text color
Expand All @@ -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")]
Expand Down
31 changes: 31 additions & 0 deletions pumpkin-core/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
4 changes: 3 additions & 1 deletion pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
}
Expand Down

0 comments on commit 13206d5

Please sign in to comment.