Skip to content

Commit

Permalink
add one-two-threes to user guide, also rename and reorganize sections
Browse files Browse the repository at this point in the history
  • Loading branch information
apparebit committed Sep 3, 2024
1 parent 2440a69 commit 3e74402
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 38 deletions.
6 changes: 3 additions & 3 deletions docs/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def test0() -> None:
print('Testing file "docs/src/overview/2020s.md", line 141, chapter "2020s High-Resolution Colors"',
print('Testing file "docs/src/colors/2020s.md", line 141, chapter "2020s High-Resolution Colors"',
file=sys.stderr)

from prettypretty.color import Color, ColorSpace
Expand All @@ -25,7 +25,7 @@ def test0() -> None:


def test1() -> None:
print('Testing file "docs/src/overview/1970s.md", line 384, chapter "1970s Terminal Colors"',
print('Testing file "docs/src/colors/1970s.md", line 384, chapter "1970s Terminal Colors"',
file=sys.stderr)

from prettypretty.color import Color
Expand Down Expand Up @@ -64,7 +64,7 @@ def test1() -> None:


def test2() -> None:
print('Testing file "docs/src/overview/integration.md", line 215, chapter "Accommodating All Colors"',
print('Testing file "docs/src/colors/integration.md", line 215, chapter "Accommodating All Colors"',
file=sys.stderr)

from prettypretty.color import Color, OkVersion
Expand Down
9 changes: 5 additions & 4 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

# Getting Started

- [Installing and Building Prettypretty](installation.md)
- [Installing and Building Prettypretty](starting/installation.md)
- [Prettypretty's One-Two-Three](starting/one-two-three.md)

# All the Colors

- [2020s High-Resolution Colors](overview/2020s.md)
- [1970s Terminal Colors](overview/1970s.md)
- [Accommodating All Colors](overview/integration.md)
- [2020s High-Resolution Colors](colors/2020s.md)
- [1970s Terminal Colors](colors/1970s.md)
- [Accommodating All Colors](colors/integration.md)

# Deep Dives

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions docs/src/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[`ColorSpace`]: https://apparebit.github.io/prettypretty/prettypretty/enum.ColorSpace.html
[`EmbeddedRgb`]: https://apparebit.github.io/prettypretty/prettypretty/style/struct.EmbeddedRgb.html
[`Fidelity`]: https://apparebit.github.io/prettypretty/prettypretty/style/enum.Fidelity.html
[`Fidelity::from_environment`]: https://apparebit.github.io/prettypretty/prettypretty/style/enum.Fidelity.html#method.from_environment
[`Float`]: https://apparebit.github.io/prettypretty/prettypretty/type.Float.html
[`Format`]: https://apparebit.github.io/prettypretty/prettypretty/style/format/struct.Format.html
[`GrayGradient`]: https://apparebit.github.io/prettypretty/prettypretty/style/struct.GrayGradient.html
Expand All @@ -24,5 +25,6 @@
[`ThemeEntry`]: https://apparebit.github.io/prettypretty/prettypretty/trans/enum.ThemeEntry.html
[`ThemeEntryIterator`]: https://apparebit.github.io/prettypretty/prettypretty/trans/struct.ThemeEntryIterator.html
[`Translator`]: https://apparebit.github.io/prettypretty/prettypretty/trans/struct.Translator.html
[`Translator::is_dark_theme`]: https://apparebit.github.io/prettypretty/prettypretty/trans/struct.Translator.html#method.is_dark_theme
[`TrueColor`]: https://apparebit.github.io/prettypretty/prettypretty/style/struct.TrueColor.html
[`VGA_COLORS`]: https://apparebit.github.io/prettypretty/prettypretty/trans/constant.VGA_COLORS.html
30 changes: 0 additions & 30 deletions docs/src/overview/summary.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ $ ./rr.sh help
```


{{#include links.md}}
{{#include ../links.md}}
128 changes: 128 additions & 0 deletions docs/src/starting/one-two-three.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Prettypretty's One-Two-Three

Prettypretty's integration of 2020s color science with 1970s terminal output
enables a fresh take on styling the output of command line applications. Similar
to CSS on the web, that approach treats styles as dynamic, with rendered results
looking differently on different terminals. However, while CSS favors
progressive enhancement, i.e., starting with a more basic design and adding
features and flourishes as screen sizes and computing power increase,
prettypretty builds on graceful degradation, i.e., starting with expressive
styles and automatically adjusting them to user preferences and less capable
terminals. Very much like CSS, prettypretty delivers better results, when you
author for variability. Prettypretty's workflow is based on three major steps.
Here's an overview of them.


## 1. Fluently Assembling Styles

The first step is the fluent assembly of [`Style`] objects with [`stylist`]s,
i.e., style builders. Each such style can optionally reset terminal appearance,
may include a text [`Format`], a foreground color using any supported color
format, and a background color using any supported color format.

The following example fluently assembles a style for bold black text on yellow
background:

```rust
# extern crate prettypretty;
# use prettypretty::{Color, ColorSpace};
# use prettypretty::style::{stylist, Colorant, format::Format, TrueColor};
let style = stylist()
.bold()
.foreground(Color::default())
.background(TrueColor::new(0xff, 0xe0, 0x6c))
.et_voila();

assert_eq!(style.format(), Some(Format::new().bold()));
assert_eq!(style.foreground(),
Some(Colorant::HiRes(Color::new(
ColorSpace::Xyz, [0.0, 0.0, 0.0]))).as_ref());
assert_eq!(style.background(),
Some(Colorant::Rgb(TrueColor::new(255, 224, 108))).as_ref());
```
<div class=color-swatch>
<div style="background-color: color(xyz 0 0 0);"></div>
<div style="background-color: #ffe06c;"></div>
</div>
<br>

Best practice is to define a complimentary style for dark mode as well. Since
colors tend to appear more saturated in dark mode, simply switching foreground
and background colors doesn't work. Instead pick a less saturated yellow and
also a less dark black. Once you picked those colors, how about assembling the
corresponding style in Rust?


## 2. Adjusting the Fidelity of Styles

The second step is adjusting those pretty assembled styles to the capabilities
of the current terminal and to user preferences, e.g., `NO_COLOR`, as captured
by [`Fidelity`] levels. [`Fidelity::from_environment`] determines likely
terminal capabilities based on environment variables. Meanwhile, [`Translator`]
performs the actual conversion. You instantiate a translator with the colors for
the current color theme.

Once you have the right fidelity level and a translator, you pick between
styles for light or dark mode with the [`Translator::is_dark_theme`]. And you
adjust the selected styles with [`Style::cap`].

The example code shows how to adjust the style from the previous example for a
terminal that renders 8-bit colorts only.

```rust
# extern crate prettypretty;
# use prettypretty::{Color, OkVersion};
# use prettypretty::style::{stylist, AnsiColor, Colorant, Fidelity, TrueColor};
# use prettypretty::trans::{Translator, VGA_COLORS};
# let style = stylist()
# .bold()
# .foreground(Color::default())
# .background(TrueColor::new(0xff, 0xe0, 0x6c))
# .et_voila();
let translator = Translator::new(
OkVersion::Revised, VGA_COLORS.clone());
let style = style.cap(Fidelity::Ansi, &translator);

assert_eq!(style.foreground(),
Some(Colorant::Ansi(AnsiColor::Black)).as_ref());
assert_eq!(style.background(),
Some(Colorant::Ansi(AnsiColor::BrightYellow)).as_ref());
```
<div class=color-swatch>
<div style="background-color: rgb(0 0 0);"></div>
<div style="background-color: rgb(255 255 85);"></div>
</div>


## 3. Applying and Reverting Styles

The third step is actually using the assembled and adjusted styles. Applying a
style to text, say, `prettypretty`, is as simple as writing its display to the
terminal. Reverting the style again takes nothing more than writing the display
of the negation to the terminal.

The example illustrates how to apply the style from the example above to this
package's name, `prettypretty`.

```rust
# extern crate prettypretty;
# use prettypretty::{Color, OkVersion};
# use prettypretty::style::{stylist, AnsiColor, Colorant, Fidelity, TrueColor};
# use prettypretty::trans::{Translator, VGA_COLORS};
# let style = stylist()
# .bold()
# .foreground(Color::default())
# .background(TrueColor::new(0xff, 0xe0, 0x6c))
# .et_voila();
# let translator = Translator::new(
# OkVersion::Revised, VGA_COLORS.clone());
# let style = style.cap(Fidelity::Ansi, &translator);
let s = format!("{}prettypretty{}", style, !&style);

assert_eq!(s, "\x1b[1;30;103mprettypretty\x1b[22;39;49m")
```

That's all!


{{#include ../links.md}}

0 comments on commit 3e74402

Please sign in to comment.