Skip to content

Commit

Permalink
Fix all the things
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
matthew-a-thomas committed Jul 17, 2019
1 parent 90a405e commit 6565959
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust.all_targets": true
}
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
[package]
name = "ansi_rgb"
version = "0.1.0"
version = "0.2.0"
authors = ["Matt Thomas <me@matthewathomas.com>"]
edition = "2018"
description = "Simple `no_std` compatible ANSI escape character coloring"
license-file = "LICENSE"
description = "Colorful console text using ANSI escape sequences"
license = "MIT"
categories = ["no-std", "command-line-interface"]
keywords = ["ansi", "color", "rgb", "no_std", "console"]
readme = "README.md"
repository = "https://github.com/rust-osdev/ansi_rgb"
homepage = "https://github.com/rust-osdev/ansi_rgb"
documentation = "https://docs.rs/ansi_rgb"

[badges]
maintenance = { status = "actively-developed" }

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

[dependencies]
rgb = "0.8"
97 changes: 75 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,81 @@
# ansi_rgb
ANSI escape code colors for `no_std` environments.
Colorful console text using [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters).

* Very simple API
* Full color (using the [`rgb` crate](https://crates.io/crates/rgb))
* Colors all the [formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)
* `no_std` compliant

[![crates.io badge](https://img.shields.io/crates/v/ansi_rgb.svg)](https://crates.io/crates/ansi_rgb)<br/>
[![docs.rs badge](https://docs.rs/ansi_rgb/badge.svg)](https://docs.rs/ansi_rgb)<br/>
[![Downloads badge](https://img.shields.io/crates/d/ansi_rgb.svg)](https://crates.io/crates/ansi_rgb)

Cargo.toml:
```toml
ansi_rgb = "0.2.0"
```

# Foreground colors

```rust
use ansi_rgb::{ Foreground, red };

println!("{}", "Hello, world!".fg(red()));
```

Output:

<code style="color: red">Hello, world!</code>

# Background colors

```rust
use ansi_rgb::{ Background, red };

println!("{}", "Hello, world!".bg(red()));
```

Output:

<code style="background: red">Hello, world!</code>

# Mix and match

```toml
# Cargo.toml
[dependencies]
rbg = "0.8"
```

```rust
use ansi_rgb::{ Foreground, Background };
use rgb::RGB8;

let fg = RGB8::new(123, 231, 111);
let bg = RGB8::new(10, 100, 20);
println!("{}", "Yuck".fg(fg).bg(bg));
```

Output:

<code style="color: #7BE76F; background: #0A6414">Yuck</code>

# Anything formattable

```rust
use ansi_rgb::*;

fn main() {
println!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
"Red".fg(red()),
"Orange".fg(orange()),
"Yellow".fg(yellow()),
"Yellow green".fg(yellow_green()),
"Green".fg(green()),
"Green cyan".fg(green_cyan()),
"Cyan".fg(cyan()),
"Cyan blue".fg(white()).bg(cyan_blue()),
"Blue".fg(white()).bg(blue()),
"Blue magenta".fg(white()).bg(blue_magenta()),
"Magenta".fg(magenta()),
"Magenta pink".fg(magenta_pink()),
"Custom color".fg(Rgb::new(123, 231, 111)).bg(Rgb::new(10, 100, 20))
);
}
#[derive(Debug)]
struct Foo(i32, i32);

let foo = Foo(1, 2);
println!("{:?}", foo.fg(green()));
```

![Colorful output](https://imgur.com/9j1FLiU.png)
Output:

<code style="color: #00FF00">Foo(1, 2)</code>

# Windows users

You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this:

Context here: https://github.com/phil-opp/blog_os/issues/603
`�[48;2;159;114;0m �[0m`
13 changes: 8 additions & 5 deletions src/background.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::rgb::*;
use core::fmt;
use rgb::RGB8;

/// Adds a background color
pub trait Background: Sized {
fn bg(self, rgb: Rgb) -> WithBackground<Self>;
/// Adds the given background color
fn bg(self, rgb: RGB8) -> WithBackground<Self>;
}

/// Something with a background color
pub struct WithBackground<T> {
t: T,
rgb: Rgb
rgb: RGB8
}

impl<T> Background for &T {
fn bg(self, rgb: Rgb) -> WithBackground<Self> {
impl<T> Background for T {
fn bg(self, rgb: RGB8) -> WithBackground<Self> {
WithBackground {
t: self,
rgb
Expand Down
71 changes: 71 additions & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use rgb::RGB8;

/// Makes <code style="color: black; background: #FFFFFF">white</code>
pub const fn white() -> RGB8 {
RGB8::new(255, 255, 255)
}

/// Makes <code style="color: white; background: #000000">black</code>
pub const fn black() -> RGB8 {
RGB8::new(0, 0, 0)
}

/// Makes <code style="color: black; background: #FF0000">red</code>
pub const fn red() -> RGB8 {
RGB8::new(255, 0, 0)
}

/// Makes <code style="color: black; background: #FF8000">orange</code>
pub const fn orange() -> RGB8 {
RGB8::new(255, 128, 0)
}

/// Makes <code style="color: black; background: #FFFF00">yellow</code>
pub const fn yellow() -> RGB8 {
RGB8::new(255, 255, 0)
}

/// Makes <code style="color: black; background: #80FF00">yellow green</code>
pub const fn yellow_green() -> RGB8 {
RGB8::new(128, 255, 0)
}

/// Makes <code style="color: black; background: #00FF00">green</code>
pub const fn green() -> RGB8 {
RGB8::new(0, 255, 0)
}

/// Makes <code style="color: black; background: #00FF80">green cyan</code>
pub const fn green_cyan() -> RGB8 {
RGB8::new(0, 255, 128)
}

/// Makes <code style="color: black; background: #00FFFF">cyan</code>
pub const fn cyan() -> RGB8 {
RGB8::new(0, 255, 255)
}

/// Makes <code style="color: white; background: #0080FF">cyan blue</code>
pub const fn cyan_blue() -> RGB8 {
RGB8::new(0, 128, 255)
}

/// Makes <code style="color: white; background: #0000FF">blue</code>
pub const fn blue() -> RGB8 {
RGB8::new(0, 0, 255)
}

/// Makes <code style="color: white; background: #8000FF">blue magenta</code>
pub const fn blue_magenta() -> RGB8 {
RGB8::new(128, 0, 255)
}

/// Makes <code style="color: black; background: #FF00FF">magenta</code>
pub const fn magenta() -> RGB8 {
RGB8::new(255, 0, 255)
}

/// Makes <code style="color: black; background: #FF0080">magenta pink</code>
pub const fn magenta_pink() -> RGB8 {
RGB8::new(255, 0, 128)
}
13 changes: 8 additions & 5 deletions src/foreground.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::rgb::*;
use core::fmt;
use rgb::RGB8;

/// Adds a foreground color
pub trait Foreground: Sized {
fn fg(self, rgb: Rgb) -> WithForeground<Self>;
/// Adds the given foreground color
fn fg(self, rgb: RGB8) -> WithForeground<Self>;
}

/// Something with a foreground color
pub struct WithForeground<T> {
t: T,
rgb: Rgb
rgb: RGB8
}

impl<T> Foreground for &T {
fn fg(self, rgb: Rgb) -> WithForeground<Self> {
impl<T> Foreground for T {
fn fg(self, rgb: RGB8) -> WithForeground<Self> {
WithForeground {
t: self,
rgb
Expand Down
80 changes: 77 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,83 @@
#![no_std]
/*!
Colorful console text using [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters).
* Very simple API
* Full color (using the [`rgb` crate](https://crates.io/crates/rgb))
* Colors all the [formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)
* `no_std` compliant
# Foreground colors
```rust
use ansi_rgb::{ Foreground, red };
println!("{}", "Hello, world!".fg(red()));
```
Output:
<code style="color: red">Hello, world!</code>
# Background colors
```rust
use ansi_rgb::{ Background, red };
println!("{}", "Hello, world!".bg(red()));
```
Output:
<code style="background: red">Hello, world!</code>
# Mix and match
```toml
# Cargo.toml
[dependencies]
rbg = "0.8"
```
```rust
use ansi_rgb::{ Foreground, Background };
use rgb::RGB8;
let fg = RGB8::new(123, 231, 111);
let bg = RGB8::new(10, 100, 20);
println!("{}", "Yuck".fg(fg).bg(bg));
```
Output:
<code style="color: #7BE76F; background: #0A6414">Yuck</code>
# Anything formattable
```rust
# use ansi_rgb::*;
#[derive(Debug)]
struct Foo(i32, i32);
let foo = Foo(1, 2);
println!("{:?}", foo.fg(green()));
```
Output:
<code style="color: #00FF00">Foo(1, 2)</code>
# Windows users
You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this:
`�[48;2;159;114;0m �[0m`
*/

mod background;
mod colors;
mod foreground;
mod rgb;

pub use background::*;
pub use foreground::*;
pub use rgb::*;
pub use colors::*;
pub use foreground::*;
19 changes: 0 additions & 19 deletions src/main.rs

This file was deleted.

Loading

0 comments on commit 6565959

Please sign in to comment.