Add Console Colors & Styles
This commit is contained in:
parent
ee4bde9962
commit
13206d58d9
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1229,6 +1229,7 @@ dependencies = [
|
|||||||
name = "pumpkin-core"
|
name = "pumpkin-core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"colored",
|
||||||
"fastnbt",
|
"fastnbt",
|
||||||
"serde",
|
"serde",
|
||||||
"uuid",
|
"uuid",
|
||||||
|
|||||||
@ -7,3 +7,4 @@ edition.workspace = true
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
|
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
|
||||||
uuid.workspace = true
|
uuid.workspace = true
|
||||||
|
colored = "2"
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
use colored::{ColoredString, Colorize};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Text color
|
/// Text color
|
||||||
@ -13,6 +14,32 @@ pub enum Color {
|
|||||||
Named(NamedColor),
|
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
|
/// Named Minecraft color
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
|
|||||||
@ -3,6 +3,7 @@ use std::borrow::Cow;
|
|||||||
|
|
||||||
use click::ClickEvent;
|
use click::ClickEvent;
|
||||||
use color::Color;
|
use color::Color;
|
||||||
|
use colored::Colorize;
|
||||||
use fastnbt::SerOpts;
|
use fastnbt::SerOpts;
|
||||||
use hover::HoverEvent;
|
use hover::HoverEvent;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -38,6 +39,36 @@ impl<'a> TextComponent<'a> {
|
|||||||
style: Style::default(),
|
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> {
|
impl<'a> serde::Serialize for TextComponent<'a> {
|
||||||
|
|||||||
@ -24,7 +24,7 @@ impl<'a> CommandSender<'a> {
|
|||||||
pub fn send_message(&mut self, text: TextComponent) {
|
pub fn send_message(&mut self, text: TextComponent) {
|
||||||
match self {
|
match self {
|
||||||
// TODO: add color and stuff to console
|
// 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::Player(c) => c.send_system_message(text),
|
||||||
CommandSender::Rcon(s) => s.push(format!("{:?}", text.content)),
|
CommandSender::Rcon(s) => s.push(format!("{:?}", text.content)),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,7 +94,9 @@ fn main() -> io::Result<()> {
|
|||||||
stdin
|
stdin
|
||||||
.read_line(&mut out)
|
.read_line(&mut out)
|
||||||
.expect("Failed to read console line");
|
.expect("Failed to read console line");
|
||||||
handle_command(&mut commands::CommandSender::Console, &out);
|
if !out.is_empty() {
|
||||||
|
handle_command(&mut commands::CommandSender::Console, &out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user