From c960b06863024087437644524894f247e8a4b448 Mon Sep 17 00:00:00 2001 From: iximeow Date: Thu, 28 Mar 2019 02:31:06 -0700 Subject: tweak how ColorSettings is used enum to tie together a color and a thing to be colored, helper methods to build that, and an impl on Option that gives non-colored variants --- src/lib.rs | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 188 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 05f994e..f11c9d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -217,24 +217,194 @@ impl Default for ColorSettings { } } -impl ColorSettings { - pub fn arithmetic_op(&self) -> &color::Fg<&'static color::Color> { &self.arithmetic } - pub fn stack_op(&self) -> &color::Fg<&'static color::Color> { &self.stack } - pub fn nop_op(&self) -> &color::Fg<&'static color::Color> { &self.nop } - pub fn stop_op(&self) -> &color::Fg<&'static color::Color> { &self.stop } - pub fn control_flow_op(&self) -> &color::Fg<&'static color::Color> { &self.control } - pub fn data_op(&self) -> &color::Fg<&'static color::Color> { &self.data } - pub fn comparison_op(&self) -> &color::Fg<&'static color::Color> { &self.comparison } - pub fn invalid_op(&self) -> &color::Fg<&'static color::Color> { &self.invalid } - - pub fn register(&self) -> &color::Fg<&'static color::Color> { &self.register } - pub fn number(&self) -> &color::Fg<&'static color::Color> { &self.number } - pub fn zero(&self) -> &color::Fg<&'static color::Color> { &self.zero } - pub fn one(&self) -> &color::Fg<&'static color::Color> { &self.one } - pub fn minus_one(&self) -> &color::Fg<&'static color::Color> { &self.minus_one } - pub fn address(&self) -> &color::Fg<&'static color::Color> { &self.address } - pub fn symbol(&self) -> &color::Fg<&'static color::Color> { &self.symbol } - pub fn function(&self) -> &color::Fg<&'static color::Color> { &self.function } +pub trait YaxColors { + fn arithmetic_op(&self, t: T) -> Colored; + fn stack_op(&self, t: T) -> Colored; + fn nop_op(&self, t: T) -> Colored; + fn stop_op(&self, t: T) -> Colored; + fn control_flow_op(&self, t: T) -> Colored; + fn data_op(&self, t: T) -> Colored; + fn comparison_op(&self, t: T) -> Colored; + fn invalid_op(&self, t: T) -> Colored; + + fn register(&self, t: T) -> Colored; + fn number(&self, t: T) -> Colored; + fn zero(&self, t: T) -> Colored; + fn one(&self, t: T) -> Colored; + fn minus_one(&self, t: T) -> Colored; + fn address(&self, t: T) -> Colored; + fn symbol(&self, t: T) -> Colored; + fn function(&self, t: T) -> Colored; +} + +impl YaxColors for ColorSettings { + fn arithmetic_op(&self, t: T) -> Colored { + Colored::Color(t, self.arithmetic) + } + fn stack_op(&self, t: T) -> Colored { + Colored::Color(t, self.stack) + } + fn nop_op(&self, t: T) -> Colored { + Colored::Color(t, self.nop) + } + fn stop_op(&self, t: T) -> Colored { + Colored::Color(t, self.stop) + } + fn control_flow_op(&self, t: T) -> Colored { + Colored::Color(t, self.control) + } + fn data_op(&self, t: T) -> Colored { + Colored::Color(t, self.data) + } + fn comparison_op(&self, t: T) -> Colored { + Colored::Color(t, self.comparison) + } + fn invalid_op(&self, t: T) -> Colored { + Colored::Color(t, self.invalid) + } + + fn register(&self, t: T) -> Colored { + Colored::Color(t, self.register) + } + fn number(&self, t: T) -> Colored { + Colored::Color(t, self.number) + } + fn zero(&self, t: T) -> Colored { + Colored::Color(t, self.zero) + } + fn one(&self, t: T) -> Colored { + Colored::Color(t, self.one) + } + fn minus_one(&self, t: T) -> Colored { + Colored::Color(t, self.minus_one) + } + fn address(&self, t: T) -> Colored { + Colored::Color(t, self.address) + } + fn symbol(&self, t: T) -> Colored { + Colored::Color(t, self.symbol) + } + fn function(&self, t: T) -> Colored { + Colored::Color(t, self.function) + } +} + +impl <'a> YaxColors for Option<&'a ColorSettings> { + fn arithmetic_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.arithmetic_op(t) } + None => { Colored::Just(t) } + } + } + fn stack_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.stack_op(t) } + None => { Colored::Just(t) } + } + } + fn nop_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.nop_op(t) } + None => { Colored::Just(t) } + } + } + fn stop_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.stop_op(t) } + None => { Colored::Just(t) } + } + } + fn control_flow_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.control_flow_op(t) } + None => { Colored::Just(t) } + } + } + fn data_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.data_op(t) } + None => { Colored::Just(t) } + } + } + fn comparison_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.comparison_op(t) } + None => { Colored::Just(t) } + } + } + fn invalid_op(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.invalid_op(t) } + None => { Colored::Just(t) } + } + } + + fn register(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.register(t) } + None => { Colored::Just(t) } + } + } + fn number(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.number(t) } + None => { Colored::Just(t) } + } + } + fn zero(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.zero(t) } + None => { Colored::Just(t) } + } + } + fn one(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.one(t) } + None => { Colored::Just(t) } + } + } + fn minus_one(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.minus_one(t) } + None => { Colored::Just(t) } + } + } + fn address(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.address(t) } + None => { Colored::Just(t) } + } + } + fn symbol(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.symbol(t) } + None => { Colored::Just(t) } + } + } + fn function(&self, t: T) -> Colored { + match self { + Some(colors) => { colors.function(t) } + None => { Colored::Just(t) } + } + } +} + +pub enum Colored { + Color(T, color::Fg<&'static color::Color>), + Just(T) +} + +impl Display for Colored { + fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { + match self { + Colored::Color(t, color) => { + write!(fmt, "{}{}{}", color, t, color::Fg(color::Reset)) + }, + Colored::Just(t) => { + write!(fmt, "{}", t) + } + } + } } /* -- cgit v1.1