Skip to content

Commit 896469a

Browse files
Implement ANSI formatting stuff
1 parent c57c75a commit 896469a

File tree

12 files changed

+294
-0
lines changed

12 files changed

+294
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
**/*.rs.bk

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "colored"
3+
version = "0.1.0"
4+
authors = ["Matt Thomas <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

src/background.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::colors::*;
2+
use core::fmt;
3+
4+
pub trait Background: Sized {
5+
fn background(self, rgb: Rgb) -> WithBackground<Self>;
6+
}
7+
8+
pub struct WithBackground<T> {
9+
t: T,
10+
rgb: Rgb
11+
}
12+
13+
impl<T> Background for &T {
14+
fn background(self, rgb: Rgb) -> WithBackground<Self> {
15+
WithBackground {
16+
t: self,
17+
rgb
18+
}
19+
}
20+
}
21+
22+
impl<T> fmt::Display for WithBackground<T>
23+
where T: fmt::Display {
24+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
write!(f, "\x1B[48;2;{};{};{}m{}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
26+
}
27+
}
28+
29+
impl<T> fmt::Debug for WithBackground<T>
30+
where T: fmt::Debug {
31+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32+
write!(f, "\x1B[48;2;{};{};{}m{:?}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
33+
}
34+
}

src/blink.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use core::fmt;
2+
3+
pub trait Blink: Sized {
4+
fn blink(self) -> WithBlink<Self>;
5+
}
6+
7+
pub struct WithBlink<T> {
8+
t: T
9+
}
10+
11+
impl<T> Blink for &T {
12+
fn blink(self) -> WithBlink<Self> {
13+
WithBlink {
14+
t: self
15+
}
16+
}
17+
}
18+
19+
impl<T> fmt::Display for WithBlink<T>
20+
where T: fmt::Display {
21+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22+
write!(f, "\x1B[5m{}\x1B[0m", self.t)
23+
}
24+
}
25+
26+
impl<T> fmt::Debug for WithBlink<T>
27+
where T: fmt::Debug {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
write!(f, "\x1B[5m{:?}\x1B[0m", self.t)
30+
}
31+
}

src/bold.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use core::fmt;
2+
3+
pub trait Bold<T>: Sized {
4+
fn bold(self) -> WithBold<Self>;
5+
}
6+
7+
pub struct WithBold<T> {
8+
t: T
9+
}
10+
11+
impl<T> Bold<T> for &T {
12+
fn bold(self) -> WithBold<Self> {
13+
WithBold {
14+
t: self
15+
}
16+
}
17+
}
18+
19+
impl<T> fmt::Display for WithBold<T>
20+
where T: fmt::Display {
21+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22+
write!(f, "\x1B[1m{}\x1B[0m", self.t)
23+
}
24+
}
25+
26+
impl<T> fmt::Debug for WithBold<T>
27+
where T: fmt::Debug {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
write!(f, "\x1B[1m{:?}\x1B[0m", self.t)
30+
}
31+
}

src/colors.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#[derive(Clone)]
2+
#[derive(Copy)]
3+
#[derive(Debug)]
4+
pub struct Rgb {
5+
pub r: u8,
6+
pub g: u8,
7+
pub b: u8
8+
}
9+
10+
impl Rgb {
11+
pub const fn new(r: u8, g: u8, b: u8) -> Self {
12+
Self {
13+
r,
14+
g,
15+
b
16+
}
17+
}
18+
}
19+
20+
pub struct Colors();
21+
22+
impl Colors {
23+
pub fn red() -> Rgb {
24+
Rgb::new(255, 0, 0)
25+
}
26+
27+
pub fn orange() -> Rgb {
28+
Rgb::new(255, 127, 0)
29+
}
30+
31+
pub fn yellow() -> Rgb {
32+
Rgb::new(255, 255, 0)
33+
}
34+
35+
pub fn green() -> Rgb {
36+
Rgb::new(0, 255, 0)
37+
}
38+
39+
pub fn cyan() -> Rgb {
40+
Rgb::new(0, 255, 255)
41+
}
42+
43+
pub fn blue() -> Rgb {
44+
Rgb::new(0, 0, 255)
45+
}
46+
47+
pub fn purple() -> Rgb {
48+
Rgb::new(127, 0, 127)
49+
}
50+
51+
pub fn violet() -> Rgb {
52+
Rgb::new(127, 0, 255)
53+
}
54+
}

src/foreground.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::colors::*;
2+
use core::fmt;
3+
4+
pub trait Foreground: Sized {
5+
fn foreground(self, rgb: Rgb) -> WithForeground<Self>;
6+
}
7+
8+
pub struct WithForeground<T> {
9+
t: T,
10+
rgb: Rgb
11+
}
12+
13+
impl<T> Foreground for &T {
14+
fn foreground(self, rgb: Rgb) -> WithForeground<Self> {
15+
WithForeground {
16+
t: self,
17+
rgb
18+
}
19+
}
20+
}
21+
22+
impl<T> fmt::Display for WithForeground<T>
23+
where T: fmt::Display {
24+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
write!(f, "\x1B[38;2;{};{};{}m{}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
26+
}
27+
}
28+
29+
impl<T> fmt::Debug for WithForeground<T>
30+
where T: fmt::Debug {
31+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32+
write!(f, "\x1B[38;2;{};{};{}m{:?}\x1B[0m", self.rgb.r, self.rgb.g, self.rgb.b, self.t)
33+
}
34+
}

src/italic.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use core::fmt;
2+
3+
pub trait Italic: Sized {
4+
fn italic(self) -> WithItalic<Self>;
5+
}
6+
7+
pub struct WithItalic<T> {
8+
t: T
9+
}
10+
11+
impl<T> Italic for &T {
12+
fn italic(self) -> WithItalic<Self> {
13+
WithItalic {
14+
t: self
15+
}
16+
}
17+
}
18+
19+
impl<T> fmt::Display for WithItalic<T>
20+
where T: fmt::Display {
21+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22+
write!(f, "\x1B[3m{}\x1B[0m", self.t)
23+
}
24+
}
25+
26+
impl<T> fmt::Debug for WithItalic<T>
27+
where T: fmt::Debug {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
write!(f, "\x1B[3m{:?}\x1B[0m", self.t)
30+
}
31+
}

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![no_std]
2+
3+
mod background;
4+
mod blink;
5+
mod bold;
6+
mod colors;
7+
mod foreground;
8+
mod italic;
9+
mod underline;
10+
11+
pub use background::*;
12+
pub use blink::*;
13+
pub use bold::*;
14+
pub use colors::*;
15+
pub use foreground::*;
16+
pub use italic::*;
17+
pub use underline::*;

0 commit comments

Comments
 (0)