Skip to content

Commit 6565959

Browse files
Fix all the things
* Use `rgb` crate * Explain usage in Windows console * Axe `main.rs` * Fix "temporary value dropped while borrowed" * Fix formatting of pointers * Package the crate better
1 parent 90a405e commit 6565959

File tree

11 files changed

+312
-142
lines changed

11 files changed

+312
-142
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust.all_targets": true
3+
}

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
[package]
22
name = "ansi_rgb"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Matt Thomas <[email protected]>"]
55
edition = "2018"
6-
description = "Simple `no_std` compatible ANSI escape character coloring"
7-
license-file = "LICENSE"
6+
description = "Colorful console text using ANSI escape sequences"
7+
license = "MIT"
8+
categories = ["no-std", "command-line-interface"]
9+
keywords = ["ansi", "color", "rgb", "no_std", "console"]
10+
readme = "README.md"
11+
repository = "https://github.com/rust-osdev/ansi_rgb"
12+
homepage = "https://github.com/rust-osdev/ansi_rgb"
13+
documentation = "https://docs.rs/ansi_rgb"
14+
15+
[badges]
16+
maintenance = { status = "actively-developed" }
817

918
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1019

1120
[dependencies]
21+
rgb = "0.8"

README.md

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,81 @@
11
# ansi_rgb
2-
ANSI escape code colors for `no_std` environments.
2+
Colorful console text using [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters).
3+
4+
* Very simple API
5+
* Full color (using the [`rgb` crate](https://crates.io/crates/rgb))
6+
* Colors all the [formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)
7+
* `no_std` compliant
8+
9+
[![crates.io badge](https://img.shields.io/crates/v/ansi_rgb.svg)](https://crates.io/crates/ansi_rgb)<br/>
10+
[![docs.rs badge](https://docs.rs/ansi_rgb/badge.svg)](https://docs.rs/ansi_rgb)<br/>
11+
[![Downloads badge](https://img.shields.io/crates/d/ansi_rgb.svg)](https://crates.io/crates/ansi_rgb)
12+
13+
Cargo.toml:
14+
```toml
15+
ansi_rgb = "0.2.0"
16+
```
17+
18+
# Foreground colors
19+
20+
```rust
21+
use ansi_rgb::{ Foreground, red };
22+
23+
println!("{}", "Hello, world!".fg(red()));
24+
```
25+
26+
Output:
27+
28+
<code style="color: red">Hello, world!</code>
29+
30+
# Background colors
31+
32+
```rust
33+
use ansi_rgb::{ Background, red };
34+
35+
println!("{}", "Hello, world!".bg(red()));
36+
```
37+
38+
Output:
39+
40+
<code style="background: red">Hello, world!</code>
41+
42+
# Mix and match
43+
44+
```toml
45+
# Cargo.toml
46+
[dependencies]
47+
rbg = "0.8"
48+
```
49+
50+
```rust
51+
use ansi_rgb::{ Foreground, Background };
52+
use rgb::RGB8;
53+
54+
let fg = RGB8::new(123, 231, 111);
55+
let bg = RGB8::new(10, 100, 20);
56+
println!("{}", "Yuck".fg(fg).bg(bg));
57+
```
58+
59+
Output:
60+
61+
<code style="color: #7BE76F; background: #0A6414">Yuck</code>
62+
63+
# Anything formattable
364

465
```rust
5-
use ansi_rgb::*;
6-
7-
fn main() {
8-
println!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
9-
"Red".fg(red()),
10-
"Orange".fg(orange()),
11-
"Yellow".fg(yellow()),
12-
"Yellow green".fg(yellow_green()),
13-
"Green".fg(green()),
14-
"Green cyan".fg(green_cyan()),
15-
"Cyan".fg(cyan()),
16-
"Cyan blue".fg(white()).bg(cyan_blue()),
17-
"Blue".fg(white()).bg(blue()),
18-
"Blue magenta".fg(white()).bg(blue_magenta()),
19-
"Magenta".fg(magenta()),
20-
"Magenta pink".fg(magenta_pink()),
21-
"Custom color".fg(Rgb::new(123, 231, 111)).bg(Rgb::new(10, 100, 20))
22-
);
23-
}
66+
#[derive(Debug)]
67+
struct Foo(i32, i32);
68+
69+
let foo = Foo(1, 2);
70+
println!("{:?}", foo.fg(green()));
2471
```
2572

26-
![Colorful output](https://imgur.com/9j1FLiU.png)
73+
Output:
74+
75+
<code style="color: #00FF00">Foo(1, 2)</code>
76+
77+
# Windows users
78+
79+
You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this:
2780

28-
Context here: https://github.com/phil-opp/blog_os/issues/603
81+
`�[48;2;159;114;0m �[0m`

src/background.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
use crate::rgb::*;
21
use core::fmt;
2+
use rgb::RGB8;
33

4+
/// Adds a background color
45
pub trait Background: Sized {
5-
fn bg(self, rgb: Rgb) -> WithBackground<Self>;
6+
/// Adds the given background color
7+
fn bg(self, rgb: RGB8) -> WithBackground<Self>;
68
}
79

10+
/// Something with a background color
811
pub struct WithBackground<T> {
912
t: T,
10-
rgb: Rgb
13+
rgb: RGB8
1114
}
1215

13-
impl<T> Background for &T {
14-
fn bg(self, rgb: Rgb) -> WithBackground<Self> {
16+
impl<T> Background for T {
17+
fn bg(self, rgb: RGB8) -> WithBackground<Self> {
1518
WithBackground {
1619
t: self,
1720
rgb

src/colors.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use rgb::RGB8;
2+
3+
/// Makes <code style="color: black; background: #FFFFFF">white</code>
4+
pub const fn white() -> RGB8 {
5+
RGB8::new(255, 255, 255)
6+
}
7+
8+
/// Makes <code style="color: white; background: #000000">black</code>
9+
pub const fn black() -> RGB8 {
10+
RGB8::new(0, 0, 0)
11+
}
12+
13+
/// Makes <code style="color: black; background: #FF0000">red</code>
14+
pub const fn red() -> RGB8 {
15+
RGB8::new(255, 0, 0)
16+
}
17+
18+
/// Makes <code style="color: black; background: #FF8000">orange</code>
19+
pub const fn orange() -> RGB8 {
20+
RGB8::new(255, 128, 0)
21+
}
22+
23+
/// Makes <code style="color: black; background: #FFFF00">yellow</code>
24+
pub const fn yellow() -> RGB8 {
25+
RGB8::new(255, 255, 0)
26+
}
27+
28+
/// Makes <code style="color: black; background: #80FF00">yellow green</code>
29+
pub const fn yellow_green() -> RGB8 {
30+
RGB8::new(128, 255, 0)
31+
}
32+
33+
/// Makes <code style="color: black; background: #00FF00">green</code>
34+
pub const fn green() -> RGB8 {
35+
RGB8::new(0, 255, 0)
36+
}
37+
38+
/// Makes <code style="color: black; background: #00FF80">green cyan</code>
39+
pub const fn green_cyan() -> RGB8 {
40+
RGB8::new(0, 255, 128)
41+
}
42+
43+
/// Makes <code style="color: black; background: #00FFFF">cyan</code>
44+
pub const fn cyan() -> RGB8 {
45+
RGB8::new(0, 255, 255)
46+
}
47+
48+
/// Makes <code style="color: white; background: #0080FF">cyan blue</code>
49+
pub const fn cyan_blue() -> RGB8 {
50+
RGB8::new(0, 128, 255)
51+
}
52+
53+
/// Makes <code style="color: white; background: #0000FF">blue</code>
54+
pub const fn blue() -> RGB8 {
55+
RGB8::new(0, 0, 255)
56+
}
57+
58+
/// Makes <code style="color: white; background: #8000FF">blue magenta</code>
59+
pub const fn blue_magenta() -> RGB8 {
60+
RGB8::new(128, 0, 255)
61+
}
62+
63+
/// Makes <code style="color: black; background: #FF00FF">magenta</code>
64+
pub const fn magenta() -> RGB8 {
65+
RGB8::new(255, 0, 255)
66+
}
67+
68+
/// Makes <code style="color: black; background: #FF0080">magenta pink</code>
69+
pub const fn magenta_pink() -> RGB8 {
70+
RGB8::new(255, 0, 128)
71+
}

src/foreground.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
use crate::rgb::*;
21
use core::fmt;
2+
use rgb::RGB8;
33

4+
/// Adds a foreground color
45
pub trait Foreground: Sized {
5-
fn fg(self, rgb: Rgb) -> WithForeground<Self>;
6+
/// Adds the given foreground color
7+
fn fg(self, rgb: RGB8) -> WithForeground<Self>;
68
}
79

10+
/// Something with a foreground color
811
pub struct WithForeground<T> {
912
t: T,
10-
rgb: Rgb
13+
rgb: RGB8
1114
}
1215

13-
impl<T> Foreground for &T {
14-
fn fg(self, rgb: Rgb) -> WithForeground<Self> {
16+
impl<T> Foreground for T {
17+
fn fg(self, rgb: RGB8) -> WithForeground<Self> {
1518
WithForeground {
1619
t: self,
1720
rgb

src/lib.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
11
#![no_std]
2+
/*!
3+
Colorful console text using [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters).
4+
5+
* Very simple API
6+
* Full color (using the [`rgb` crate](https://crates.io/crates/rgb))
7+
* Colors all the [formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)
8+
* `no_std` compliant
9+
10+
# Foreground colors
11+
12+
```rust
13+
use ansi_rgb::{ Foreground, red };
14+
15+
println!("{}", "Hello, world!".fg(red()));
16+
```
17+
18+
Output:
19+
20+
<code style="color: red">Hello, world!</code>
21+
22+
# Background colors
23+
24+
```rust
25+
use ansi_rgb::{ Background, red };
26+
27+
println!("{}", "Hello, world!".bg(red()));
28+
```
29+
30+
Output:
31+
32+
<code style="background: red">Hello, world!</code>
33+
34+
# Mix and match
35+
36+
```toml
37+
# Cargo.toml
38+
[dependencies]
39+
rbg = "0.8"
40+
```
41+
42+
```rust
43+
use ansi_rgb::{ Foreground, Background };
44+
use rgb::RGB8;
45+
46+
let fg = RGB8::new(123, 231, 111);
47+
let bg = RGB8::new(10, 100, 20);
48+
println!("{}", "Yuck".fg(fg).bg(bg));
49+
```
50+
51+
Output:
52+
53+
<code style="color: #7BE76F; background: #0A6414">Yuck</code>
54+
55+
# Anything formattable
56+
57+
```rust
58+
# use ansi_rgb::*;
59+
#[derive(Debug)]
60+
struct Foo(i32, i32);
61+
62+
let foo = Foo(1, 2);
63+
println!("{:?}", foo.fg(green()));
64+
```
65+
66+
Output:
67+
68+
<code style="color: #00FF00">Foo(1, 2)</code>
69+
70+
# Windows users
71+
72+
You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this:
73+
74+
`�[48;2;159;114;0m �[0m`
75+
*/
276

377
mod background;
78+
mod colors;
479
mod foreground;
5-
mod rgb;
680

781
pub use background::*;
8-
pub use foreground::*;
9-
pub use rgb::*;
82+
pub use colors::*;
83+
pub use foreground::*;

src/main.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)