Skip to content

Commit 636bc0d

Browse files
committed
[all] Clean up duplication & clarify relationship between r3bl_tui and r3bl_ansi_color
- Rename names in sizes.rs, for stack allocated structs (aka inline structs) to: inline_string!, InlineString, TinyInlineString, InlineVec, etc. - Clean up all the namespaces for symbols like `Color` and `Style`. They are no longer generic, and have namespace prefixes to disambiguate one from one another. - Remove duplication between ansi_color and tui crates. `term.rs` now lives in core. However, low level terminal color capability detection and color conversions are in ansi_color. - Provide lots of helpers to make AnsiStyledText ergonomic to use! Constructor functions like `magenta("hi")` and `bg_dark_grey()`, `fg_rgb()` and `bg_rgb()` make this possible. Along with `rgb_color!()`. - Clean up the internal structure of ansi_color so that the conversions are clear and easy to understand. - Provide easy interop between tui and ansi_color. Here's an example `let msg_fmt = from_rgb(ASTColor::from(tui_color!(lizard_green)), &msg);` in main.rs of tui demo examples. - Remove the use of crossterm::style::Stylize, and use only r3bl_* stuff. The r3bl_* stuff does not allocate Strings and makes the whole codebase more cohesive, easier to read, understand, write, and maintain.
1 parent f30b8fc commit 636bc0d

File tree

131 files changed

+2266
-2390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+2266
-2390
lines changed

2.code-search

+7-686
Large diffs are not rendered by default.

CHANGELOG.md

+53-12
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,16 @@ Updated:
457457
the `write!` macro to write to a pre-existing buffer.
458458

459459
Added:
460+
- New `tui_color!` decl macro that allows all the delightful color palette used in
461+
`giti` to be available to anyone using this crate! `r3bl_ansi_color` is updated as
462+
well to work with this macro. `AnsiStyledText` has constructor function `fg_rgb_color()`
463+
and method `AnsiStyledText::bg_rgb_color()` that when combined make it very easy to use the
464+
`tui_color!` macro to create fun colors and then use them in the `AnsiStyledText`
465+
struct (via these new easy to use functions). This is very common when colorizing
466+
terminal output for log formatting or a welcome message. You can see this in the demo
467+
example for `r3bl_tui` which uses this
468+
`let msg_fmt = fg_rgb_color(ASTColor::from(tui_color!(lizard_green)), &msg);`. Also `r3bl_ansi_color`
469+
has an equivalent macro to this called `rgb_color!`.
460470
- Add new target in `run` Nushell script called `nu run release-examples-no-log` to run
461471
the examples without logging. This is useful for performance testing. Now that HUD is
462472
displayed in the examples, there is no need to enable logging just to see this
@@ -884,7 +894,7 @@ idioms and design patterns are used in this release.
884894
transform other types into these types.
885895
- The `graphemes` module containing `UnicodeString` handling have been totally rewritten.
886896
They are now no allocation structures! In all past versions, the `UnicodeString` was a
887-
`String` under the hood. Now it is a `StringStorage` which is a `smallstr` that
897+
`String` under the hood. Now it is a `InlineString` which is a `smallstr` that
888898
initially allocates on the stack, and it can spill over into the heap if it needs to.
889899
The struct is now named `GCString`. The "newtype" design pattern / idiom and
890900
`arg: impl Into<T>` in `GCString` and other specific types are used, such as `SegIndex`,
@@ -945,7 +955,7 @@ Changed:
945955
- The `graphemes` module containing `UnicodeString` handling have been totally
946956
rewritten. They are now optimized for memory latency (access, mutation, and
947957
allocation). For performance reasons they are not "no allocation" structures.
948-
`GCString` now owns a `StringStorage` under the hood. The
958+
`GCString` now owns a `InlineString` under the hood. The
949959
`UnicodeStringSegment`, now called `Seg`, does not own anything (no heap or string
950960
allocation, and is a very "scalarized" struct), and needs a `GCString` to be
951961
able to do anything. All the existing code that relies on this has been rewritten to
@@ -974,6 +984,10 @@ Changed:
974984
- Replace the use of `bool` with meaningful enums to enhance code readability.
975985

976986
- Added:
987+
- Move `term.rs` from `r3bl_ansi_color` to `r3bl_core` crate. This is where the functions
988+
to get the terminal window size and width belong, and whether the terminal is
989+
interactive or not. Terminal color detection capabilities and low level color output
990+
manipulation are still in `r3bl_ansi_color`.
977991
- [Archive](#archived-2025-03-11) the `tui_style!` proc macro. Replace it with an easier
978992
to use decl macro `new_style!`. This allows the `r3bl_macro` crate to be removed from
979993
the workspace, and all the crates in it. `new_style!` makes it a breeze to work with
@@ -990,10 +1004,13 @@ Changed:
9901004
- [PR 1](https://github.com/r3bl-org/r3bl-open-core/pull/372)
9911005
- [PR 2](https://github.com/r3bl-org/r3bl-open-core/pull/373)
9921006
- New hashmap which remembers insertion order called `OrderedMap`.
993-
- New module `stack_alloc_types` that contain data structures that are allocated on the stack.
994-
If they grow too big, they are then moved to the heap. Under the covers, the `small_vec` and
995-
`small_str` crates are used. Lots of macros are provided to make it easy to work with these
996-
data structures. And to make it easy to write into them without allocating them.
1007+
- New module `stack_alloc_types` that contain data structures that are allocated on the
1008+
stack. If they grow too big, they are then moved to the heap. Under the covers, the
1009+
`small_vec` and `small_str` crates are used. Lots of macros are provided to make it
1010+
easy to work with these data structures (eg: `inline_string!`, `tiny_inline_string!`).
1011+
And to make it easy to write into them without allocating them. Please note that if
1012+
you make the `r3bl_ansi_color::sizing::DEFAULT_STRING_STORAGE_SIZE` number too large,
1013+
eg: more than `16`, then it will slow down the TUI editor performance use.
9971014
- `Percent` struct now has a `as_glyph()` method which displays the percentage as a
9981015
Unicode glyph ranging from `STATS_25P_GLYPH` to `STATS_50P_GLYPH` to `STATS_75P_GLYPH`
9991016
to `STATS_100P_GLYPH` depending on its value.
@@ -1473,17 +1490,41 @@ This is the first release of this crate.
14731490
### v_next_release_r3bl_ansi_color
14741491

14751492
This is a minor change that adds `vscode` to the list of environment variables that mean
1476-
that `truecolor` is supported.
1493+
that `truecolor` is supported. It removes duplication of `term.rs` in the `r3bl-open-core`
1494+
repo and workspace. The names are also cleaned up so there's no confusion about using
1495+
`Color` or `Style` which are so generic and used in many other crates.
14771496

14781497
- Updated:
14791498
- Use the latest Rust 2024 edition.
14801499

14811500
- Added:
1482-
- Support for `$TERM_PROGRAM` = `vscode` to the list of environment variables that mean
1483-
that `truecolor` is supported. This is in `check_ansi_color.rs` file.
1484-
- Lots of new helper functions to make it easy to colorize `&str` like `red("hello")`,
1485-
`green("world")`, etc. This removes the requirement to depend on `crossterm::Stylize`
1486-
for colorizing strings that are intended to be displayed in console stdout / stderr.
1501+
- Support for inline (stack allocated) data structures (`InlineVecASTStyles` and
1502+
`AnsiStyledText::to_small_str()`). Please note that if you make the
1503+
`r3bl_ansi_color::sizing::DEFAULT_STRING_STORAGE_SIZE` number too large, eg: more than
1504+
`16`, then it will slow down the TUI editor performance use.
1505+
- Lots of new easy constructor functions to make it easy to colorize `&str` like
1506+
`red("hello")`, `green("world")`, etc. This removes the requirement to depend on
1507+
`crossterm::Stylize` for colorizing strings that are intended to be displayed in
1508+
console stdout / stderr. Methods are provided to add background colors as well
1509+
`AnsiStyledText::bg_dark_grey()`.
1510+
- Easy constructor functions are provided `fg_rgb_color()` and
1511+
`AnsiStyledText::bg_rgb_color()`. Together they allow easy integration with
1512+
`tui_color!` and make it trivial to write code that uses styles / colors from the
1513+
`r3bl_tui` crate and apply them to `AnsiStyledText` which is a very common pattern
1514+
when colorizing log output.
1515+
- Easy macro `rgb_color!` to create lots of beautiful colors with ease. This is similar
1516+
to `r3bl_tui` crate's `tui_color!` macro.
1517+
1518+
- Removed:
1519+
- `term.rs` is now in `r3bl_core`. Support for `$TERM_PROGRAM` = `vscode` to the list of
1520+
environment variables that mean that `truecolor` is supported. This is in
1521+
`check_ansi_color.rs` file.
1522+
1523+
- Changed:
1524+
- `Color` is now `ASTColor`.
1525+
- `Style` is now `ASTStyle`.
1526+
- Proper converters `From` implementations are provided to convert between `ASTColor`
1527+
and `RGBColor`, and `Ansi256Color`.
14871528

14881529
### v0.7.0 (2024-10-18)
14891530

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ansi_color/README.md

+26-8
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,34 @@ The main struct that we have to consider is `AnsiStyledText`. It has two fields:
125125
Here's an example.
126126

127127
```rust
128-
use r3bl_ansi_color::{AnsiStyledText, Style, Color};
129-
128+
use r3bl_ansi_color::*;
129+
130+
// Using the constructor functions.
131+
let red_text = red("This is red text.");
132+
let red_text_on_dark_grey = red_text.bg_dark_grey();
133+
println!("{red_text_on_dark_grey}");
134+
red_text_on_dark_grey.println();
135+
136+
// Combine constructor functions.
137+
let dim_red_text_on_dark_grey = dim("text").fg_rgb_color((255, 0, 0)).bg_rgb_color((50, 50, 50));
138+
println!("{dim_red_text_on_dark_grey}");
139+
dim_red_text_on_dark_grey.println();
140+
141+
// Flexible construction using RGB color codes.
142+
let blue_text = fg_rgb_color(RgbColor::from((0, 0, 255)), "This is blue text.");
143+
let blue_text_on_white = blue_text.bg_rgb_color((255, 255, 255));
144+
println!("{blue_text_on_white}");
145+
blue_text_on_white.println();
146+
147+
// Verbose struct construction.
130148
AnsiStyledText {
131149
text: "Print a formatted (bold, italic, underline) string w/ ANSI color codes.",
132-
style: &[
133-
Style::Bold,
134-
Style::Italic,
135-
Style::Underline,
136-
Style::Foreground(Color::Rgb(50, 50, 50)),
137-
Style::Background(Color::Rgb(100, 200, 1)),
150+
style: smallvec::smallvec![
151+
ASTStyle::Bold,
152+
ASTStyle::Italic,
153+
ASTStyle::Underline,
154+
ASTStyle::Foreground(ASTColor::Rgb(50, 50, 50)),
155+
ASTStyle::Background(ASTColor::Rgb(100, 200, 1)),
138156
],
139157
}
140158
.println();

ansi_color/examples/main.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,36 @@
1515
* limitations under the License.
1616
*/
1717

18-
use r3bl_ansi_color::{AnsiStyledText, Color, ColorSupport, Style, global_color_support};
18+
use r3bl_ansi_color::{ASTColor,
19+
ASTStyle,
20+
AnsiStyledText,
21+
ColorSupport,
22+
global_color_support};
1923

2024
fn main() {
2125
// Print a string w/ ANSI color codes.
2226
{
2327
AnsiStyledText {
2428
text:
2529
"Print a formatted (bold, italic, underline) string w/ ANSI color codes.",
26-
style: &[
27-
Style::Bold,
28-
Style::Italic,
29-
Style::Underline,
30-
Style::Foreground(Color::Rgb(50, 50, 50)),
31-
Style::Background(Color::Rgb(100, 200, 1)),
30+
style: smallvec::smallvec![
31+
ASTStyle::Bold,
32+
ASTStyle::Italic,
33+
ASTStyle::Underline,
34+
ASTStyle::Foreground(ASTColor::Rgb(50, 50, 50)),
35+
ASTStyle::Background(ASTColor::Rgb(100, 200, 1)),
3236
],
3337
}
3438
.println();
3539

3640
AnsiStyledText {
3741
text: "Dim, Overline and Strikethrough line.",
38-
style: &[
39-
Style::Dim,
40-
Style::Strikethrough,
41-
Style::Overline,
42-
Style::Foreground(Color::Rgb(200, 50, 50)),
43-
Style::Background(Color::Rgb(200, 200, 1)),
42+
style: smallvec::smallvec![
43+
ASTStyle::Dim,
44+
ASTStyle::Strikethrough,
45+
ASTStyle::Overline,
46+
ASTStyle::Foreground(ASTColor::Rgb(200, 50, 50)),
47+
ASTStyle::Background(ASTColor::Rgb(200, 200, 1)),
4448
],
4549
}
4650
.println();
@@ -90,28 +94,28 @@ fn main() {
9094
fn print_text(msg: &str) {
9195
AnsiStyledText {
9296
text: msg,
93-
style: &[
94-
Style::Underline,
95-
Style::Foreground(Color::Rgb(200, 200, 1)),
96-
Style::Background(Color::Rgb(100, 60, 150)),
97+
style: smallvec::smallvec![
98+
ASTStyle::Underline,
99+
ASTStyle::Foreground(ASTColor::Rgb(200, 200, 1)),
100+
ASTStyle::Background(ASTColor::Rgb(100, 60, 150)),
97101
],
98102
}
99103
.println();
100104

101105
let eg_1 = AnsiStyledText {
102106
text: "Hello",
103-
style: &[
104-
Style::Foreground(Color::Rgb(100, 60, 150)),
105-
Style::Background(Color::Rgb(100, 200, 50)),
107+
style: smallvec::smallvec![
108+
ASTStyle::Foreground(ASTColor::Rgb(100, 60, 150)),
109+
ASTStyle::Background(ASTColor::Rgb(100, 200, 50)),
106110
],
107111
};
108112
println!("eg_1: {0}", eg_1);
109113

110114
let eg_2 = AnsiStyledText {
111115
text: "World",
112-
style: &[
113-
Style::Foreground(Color::Ansi256(150)),
114-
Style::Background(Color::Rgb(50, 50, 100)),
116+
style: smallvec::smallvec![
117+
ASTStyle::Foreground(ASTColor::Ansi256(150)),
118+
ASTStyle::Background(ASTColor::Rgb(50, 50, 100)),
115119
],
116120
};
117121
println!("eg_2: {0}", eg_2);

ansi_color/src/ansi256_color.rs

-44
This file was deleted.

0 commit comments

Comments
 (0)