aboutsummaryrefslogtreecommitdiff
path: root/minecraft-chat/src/style.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-09 00:51:52 -0600
committermat <github@matdoes.dev>2021-12-09 00:51:52 -0600
commit221e54c7a648bd2e3d9be5514fdc4d4ed37a75b2 (patch)
treed1c80b289b9079b50a434c3f199a7de378bd1191 /minecraft-chat/src/style.rs
parentbda5362bdf5331c77fdd8f36fae003853dd34bba (diff)
downloadazalea-drasl-221e54c7a648bd2e3d9be5514fdc4d4ed37a75b2.tar.xz
Implement more stuff with chat
Still need to parse styling from json
Diffstat (limited to 'minecraft-chat/src/style.rs')
-rw-r--r--minecraft-chat/src/style.rs196
1 files changed, 163 insertions, 33 deletions
diff --git a/minecraft-chat/src/style.rs b/minecraft-chat/src/style.rs
index 3e72ac82..d8cd224b 100644
--- a/minecraft-chat/src/style.rs
+++ b/minecraft-chat/src/style.rs
@@ -1,4 +1,4 @@
-#[derive(Clone)]
+#[derive(Clone, PartialEq)]
struct TextColor {
value: u32,
name: Option<String>,
@@ -15,6 +15,57 @@ struct ChatFormatting<'a> {
}
impl<'a> ChatFormatting<'a> {
+ const BLACK: ChatFormatting<'a> = ChatFormatting::new("BLACK", '0', false, 0, Some(0));
+ const DARK_BLUE: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_BLUE", '1', false, 1, Some(170));
+ const DARK_GREEN: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_GREEN", '2', false, 2, Some(43520));
+ const DARK_AQUA: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_AQUA", '3', false, 3, Some(43690));
+ const DARK_RED: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_RED", '4', false, 4, Some(1114112));
+ const DARK_PURPLE: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_PURPLE", '5', false, 5, Some(11141290));
+ const GOLD: ChatFormatting<'a> = ChatFormatting::new("GOLD", '6', false, 6, Some(16755200));
+ const GRAY: ChatFormatting<'a> = ChatFormatting::new("GRAY", '7', false, 7, Some(11184810));
+ const DARK_GRAY: ChatFormatting<'a> =
+ ChatFormatting::new("DARK_GRAY", '8', false, 8, Some(5592405));
+ const BLUE: ChatFormatting<'a> = ChatFormatting::new("BLUE", '9', false, 9, Some(5592575));
+ const GREEN: ChatFormatting<'a> = ChatFormatting::new("GREEN", 'a', false, 10, Some(5635925));
+ const AQUA: ChatFormatting<'a> = ChatFormatting::new("AQUA", 'b', false, 11, Some(5636095));
+ const RED: ChatFormatting<'a> = ChatFormatting::new("RED", 'c', false, 12, Some(16733525));
+ const LIGHT_PURPLE: ChatFormatting<'a> =
+ ChatFormatting::new("LIGHT_PURPLE", 'd', false, 13, Some(16733695));
+ const YELLOW: ChatFormatting<'a> =
+ ChatFormatting::new("YELLOW", 'e', false, 14, Some(16777045));
+ const WHITE: ChatFormatting<'a> = ChatFormatting::new("WHITE", 'f', false, 15, Some(16777215));
+ const OBFUSCATED: ChatFormatting<'a> = ChatFormatting::new("OBFUSCATED", 'k', true, -1, None);
+ const STRIKETHROUGH: ChatFormatting<'a> =
+ ChatFormatting::new("STRIKETHROUGH", 'm', true, -1, None);
+ const BOLD: ChatFormatting<'a> = ChatFormatting::new("BOLD", 'l', true, -1, None);
+ const UNDERLINE: ChatFormatting<'a> = ChatFormatting::new("UNDERLINE", 'n', true, -1, None);
+ const ITALIC: ChatFormatting<'a> = ChatFormatting::new("ITALIC", 'o', true, -1, None);
+ const RESET: ChatFormatting<'a> = ChatFormatting::new("RESET", 'r', true, -1, None);
+
+ pub const FORMATTERS: [ChatFormatting<'a>; 16] = [
+ ChatFormatting::BLACK,
+ ChatFormatting::DARK_BLUE,
+ ChatFormatting::DARK_GREEN,
+ ChatFormatting::DARK_AQUA,
+ ChatFormatting::DARK_RED,
+ ChatFormatting::DARK_PURPLE,
+ ChatFormatting::GOLD,
+ ChatFormatting::GRAY,
+ ChatFormatting::DARK_GRAY,
+ ChatFormatting::BLUE,
+ ChatFormatting::GREEN,
+ ChatFormatting::AQUA,
+ ChatFormatting::RED,
+ ChatFormatting::LIGHT_PURPLE,
+ ChatFormatting::YELLOW,
+ ChatFormatting::WHITE,
+ ];
+
const fn new(
name: &str,
code: char,
@@ -32,33 +83,6 @@ impl<'a> ChatFormatting<'a> {
}
}
-// pub const BLACK: ChatFormatting = ChatFormatting::new("BLACK", '0', false, 0, Some(0));
-// pub const DARK_BLUE: ChatFormatting = ChatFormatting::new("DARK_BLUE", '1', false, 1, Some(170));
-// pub const DARK_GREEN: ChatFormatting =
-// ChatFormatting::new("DARK_GREEN", '2', false, 2, Some(43520));
-// pub const DARK_AQUA: ChatFormatting = ChatFormatting::new("DARK_AQUA", '3', false, 3, Some(43690));
-// pub const DARK_RED: ChatFormatting = ChatFormatting::new("DARK_RED", '4', false, 4, Some(1114112));
-// pub const DARK_PURPLE: ChatFormatting =
-// ChatFormatting::new("DARK_PURPLE", '5', false, 5, Some(11141290));
-// pub const GOLD: ChatFormatting = ChatFormatting::new("GOLD", '6', false, 6, Some(16755200));
-// pub const GRAY: ChatFormatting = ChatFormatting::new("GRAY", '7', false, 7, Some(11184810));
-// pub const DARK_GRAY: ChatFormatting =
-// ChatFormatting::new("DARK_GRAY", '8', false, 8, Some(5592405));
-// pub const BLUE: ChatFormatting = ChatFormatting::new("BLUE", '9', false, 9, Some(5592575));
-// pub const GREEN: ChatFormatting = ChatFormatting::new("GREEN", 'a', false, 10, Some(5635925));
-// pub const AQUA: ChatFormatting = ChatFormatting::new("AQUA", 'b', false, 11, Some(5636095));
-// pub const RED: ChatFormatting = ChatFormatting::new("RED", 'c', false, 12, Some(16733525));
-// pub const LIGHT_PURPLE: ChatFormatting =
-// ChatFormatting::new("LIGHT_PURPLE", 'd', false, 13, Some(16733695));
-// pub const YELLOW: ChatFormatting = ChatFormatting::new("YELLOW", 'e', false, 14, Some(16777045));
-// pub const WHITE: ChatFormatting = ChatFormatting::new("WHITE", 'f', false, 15, Some(16777215));
-// pub const OBFUSCATED: ChatFormatting = ChatFormatting::new("OBFUSCATED", 'k', true, -1, None);
-// pub const STRIKETHROUGH: ChatFormatting = ChatFormatting::new("STRIKETHROUGH", 'm', true, -1, None);
-// pub const BOLD: ChatFormatting = ChatFormatting::new("BOLD", 'l', true, -1, None);
-// pub const UNDERLINE: ChatFormatting = ChatFormatting::new("UNDERLINE", 'n', true, -1, None);
-// pub const ITALIC: ChatFormatting = ChatFormatting::new("ITALIC", 'o', true, -1, None);
-// pub const RESET: ChatFormatting = ChatFormatting::new("RESET", 'r', true, -1, None);
-
impl TextColor {
fn new(value: u32, name: Option<String>) -> Self {
Self { value, name }
@@ -113,11 +137,11 @@ impl Style {
pub fn new() -> Style {
Style {
color: None,
- bold: Some(false),
- italic: Some(false),
- underlined: Some(false),
- strikethrough: Some(false),
- obfuscated: Some(false),
+ bold: None,
+ italic: None,
+ underlined: None,
+ strikethrough: None,
+ obfuscated: None,
}
}
@@ -130,4 +154,110 @@ impl Style {
&& self.strikethrough.is_none()
&& self.obfuscated.is_none()
}
+
+ /// find the necessary ansi code to get from this style to another
+ pub fn compare_ansi(&self, after: &Style) -> String {
+ let should_reset = {
+ // if it used to be bold and now it's not, reset
+ if self.bold.unwrap_or(false) && !after.bold.unwrap_or(false) {
+ true
+ }
+ // if it used to be italic and now it's not, reset
+ else if self.italic.unwrap_or(false) && !after.italic.unwrap_or(false) {
+ true
+ // if it used to be underlined and now it's not, reset
+ } else if self.underlined.unwrap_or(false) && !after.underlined.unwrap_or(false) {
+ true
+ // if it used to be strikethrough and now it's not, reset
+ } else if self.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(false) {
+ true
+ // if it used to be obfuscated and now it's not, reset
+ } else if self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(false) {
+ true
+ // if it used to have a color and now it doesn't, reset
+ } else if self.color.is_some() && after.color.is_none() {
+ true
+ } else {
+ false
+ }
+ };
+
+ let mut ansi_codes = String::new();
+
+ let before = if should_reset {
+ ansi_codes.push_str("\x1b[0m");
+ Style::new()
+ } else {
+ self.clone()
+ };
+
+ // if bold used to be false/default and now it's true, set bold
+ if before.bold.unwrap_or(false) && !after.bold.unwrap_or(false) {
+ ansi_codes.push_str("\x1b[1m");
+ }
+ // if italic used to be false/default and now it's true, set italic
+ if before.italic.unwrap_or(false) && !after.italic.unwrap_or(false) {
+ ansi_codes.push_str("\x1b[3m");
+ }
+ // if underlined used to be false/default and now it's true, set underlined
+ if before.underlined.unwrap_or(false) && !after.underlined.unwrap_or(false) {
+ ansi_codes.push_str("\x1b[4m");
+ }
+ // if strikethrough used to be false/default and now it's true, set strikethrough
+ if before.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(false) {
+ ansi_codes.push_str("\x1b[9m");
+ }
+ // if obfuscated used to be false/default and now it's true, set obfuscated
+ if before.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(false) {
+ ansi_codes.push_str("\x1b[8m");
+ }
+
+ // if the new color is different and not none, set color
+ let color_changed = {
+ if before.color.is_none() && after.color.is_some() {
+ true
+ } else if before.color.is_some() && after.color.is_some() {
+ before.color.unwrap().value != after.color.as_ref().unwrap().value
+ } else {
+ false
+ }
+ };
+
+ if color_changed {
+ let after_color = after.color.as_ref().unwrap();
+ ansi_codes.push_str(&format!(
+ "\x1b[38;2;{};{};{}m",
+ // r
+ (after_color.value >> 16) & 0xFF,
+ // g
+ (after_color.value >> 8) & 0xFF,
+ // b
+ after_color.value & 0xFF
+ ));
+ }
+
+ return "".to_string();
+ }
+
+ /// Apply another style to this one
+ pub fn apply(&mut self, style: &Style) {
+ if let Some(color) = &style.color {
+ self.color = Some(color.clone());
+ }
+ if let Some(bold) = &style.bold {
+ self.bold = Some(*bold);
+ }
+ if let Some(italic) = &style.italic {
+ self.italic = Some(*italic);
+ }
+ if let Some(underlined) = &style.underlined {
+ self.underlined = Some(*underlined);
+ }
+ if let Some(strikethrough) = &style.strikethrough {
+ self.strikethrough = Some(*strikethrough);
+ }
+ if let Some(obfuscated) = &style.obfuscated {
+ self.obfuscated = Some(*obfuscated);
+ }
+ }
}