Enabled background color functionality

renamed ColorCode to Color and Color to Paint
bump version to 0.1.1
This commit is contained in:
wpbirney 2022-06-02 14:03:31 -04:00
parent fc746a613d
commit 22a5217493
4 changed files with 68 additions and 60 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mirc" name = "mirc"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
authors = ["wpbirney <wpb@360scada.com"] authors = ["wpbirney <wpb@360scada.com"]
description = "Simple mirc color formatting for irc clients" description = "Simple mirc color formatting for irc clients"

View File

@ -5,10 +5,10 @@ A simple mirc color code formatter partially inspired by [yansi](https://github.
## Usage ## Usage
```rust ```rust
use mirc::Color; use mirc::Paint;
irc.send_privmsg("#channel", Color::red("red text")); irc.send_privmsg("#channel", Paint::red("red text"));
irc.send_privmsg("#channel", format!("Hello: {}", Color::blue("nick"))); irc.send_privmsg("#channel", format!("Hello: {}", Paint::blue("nick")));
``` ```
Works on any type that impl's fmt::Display Works on any type that impl's fmt::Display

47
src/color.rs Normal file
View File

@ -0,0 +1,47 @@
use std::fmt;
pub enum Color {
White,
Black,
Blue,
Green,
Red,
Brown,
Purple,
Orange,
Yellow,
LightGreen,
Cyan,
LightCyan,
LightBlue,
Pink,
Grey,
LightGrey,
Raw(i32),
Unset,
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Color::White => write!(f, "00"),
Color::Black => write!(f, "01"),
Color::Blue => write!(f, "02"),
Color::Green => write!(f, "03"),
Color::Red => write!(f, "04"),
Color::Brown => write!(f, "05"),
Color::Purple => write!(f, "06"),
Color::Orange => write!(f, "07"),
Color::Yellow => write!(f, "08"),
Color::LightGreen => write!(f, "09"),
Color::Cyan => write!(f, "10"),
Color::LightCyan => write!(f, "11"),
Color::LightBlue => write!(f, "12"),
Color::Pink => write!(f, "13"),
Color::Grey => write!(f, "14"),
Color::LightGrey => write!(f, "15"),
Color::Raw(c) => write!(f, "{:02}", c),
Color::Unset => Ok(()),
}
}
}

View File

@ -1,78 +1,39 @@
pub mod color;
use color::Color;
use std::fmt; use std::fmt;
pub enum ColorCode { pub struct Paint<T> {
White, fg: Color,
Black, bg: Color,
Blue,
Green,
Red,
Brown,
Purple,
Orange,
Yellow,
LightGreen,
Cyan,
LightCyan,
LightBlue,
Pink,
Grey,
LightGrey,
Raw(i32),
Unset,
}
impl fmt::Display for ColorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ColorCode::White => write!(f, "00"),
ColorCode::Black => write!(f, "01"),
ColorCode::Blue => write!(f, "02"),
ColorCode::Green => write!(f, "03"),
ColorCode::Red => write!(f, "04"),
ColorCode::Brown => write!(f, "05"),
ColorCode::Purple => write!(f, "06"),
ColorCode::Orange => write!(f, "07"),
ColorCode::Yellow => write!(f, "08"),
ColorCode::LightGreen => write!(f, "09"),
ColorCode::Cyan => write!(f, "10"),
ColorCode::LightCyan => write!(f, "11"),
ColorCode::LightBlue => write!(f, "12"),
ColorCode::Pink => write!(f, "13"),
ColorCode::Grey => write!(f, "14"),
ColorCode::LightGrey => write!(f, "15"),
ColorCode::Raw(c) => write!(f, "{:02}", c),
ColorCode::Unset => Ok(()),
}
}
}
pub struct Color<T> {
fg: ColorCode,
bg: ColorCode,
content: T, content: T,
} }
impl<T> fmt::Display for Color<T> impl<T> fmt::Display for Paint<T>
where where
T: fmt::Display, T: fmt::Display,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\x03{:02}{}\x03", self.fg, self.content) match self.bg {
Color::Unset => write!(f, "\x03{:02}{}\x03", self.fg, self.content),
_ => write!(f, "\x03{:02},{:02}{}\x03", self.fg, self.bg, self.content),
}
} }
} }
macro_rules! constructors_for { macro_rules! constructors_for {
($T:ty, $($name:ident: $color:ident),*) => ($( ($T:ty, $($name:ident: $color:ident),*) => ($(
#[inline] #[inline]
pub fn $name(content: $T) -> Color<$T> { pub fn $name(content: $T) -> Paint<$T> {
Color::new(ColorCode::$color, ColorCode::Unset, content) Paint::new(Color::$color, Color::Unset, content)
} }
)*); )*);
} }
impl<T> Color<T> { impl<T> Paint<T> {
pub fn new(fg: ColorCode, bg: ColorCode, content: T) -> Color<T> { pub fn new(fg: Color, bg: Color, content: T) -> Paint<T> {
Color { Paint {
fg, fg,
bg, bg,
content, content,