mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-30 20:14:23 +00:00
Add Console Colors & Styles
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1229,6 +1229,7 @@ dependencies = [
|
||||
name = "pumpkin-core"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"colored",
|
||||
"fastnbt",
|
||||
"serde",
|
||||
"uuid",
|
||||
|
||||
@@ -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"
|
||||
@@ -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")]
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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)),
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user