1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Bar example - Draws a filled circle using bars with red gradient.
//!
//! This is a port of Python Rich's bars.py example.
//!
//! Run with: `cargo run --example bars`
//!
//! Note: Requires `pub mod bar;` to be added to src/lib.rs
use rich_rs::bar::Bar;
use rich_rs::{Align, Console, SimpleColor};
const SIZE: usize = 40;
fn main() -> std::io::Result<()> {
let mut console = Console::new();
for row in 0..SIZE {
// Map row to y coordinate in [-1, 1]
let y = (row as f64 / (SIZE - 1) as f64) * 2.0 - 1.0;
// Calculate x using circle equation: x^2 + y^2 = 1
// x = sqrt(1 - y^2)
let x = (1.0 - y * y).sqrt();
// Create red gradient based on y position
// y goes from -1 to 1, so (1 + y) / 2 goes from 0 to 1
let red_intensity = ((1.0 + y) * 127.5) as u8;
let color = SimpleColor::Rgb {
r: red_intensity,
g: 0,
b: 0,
};
// Create bar with size=2 (range is [0, 2])
// begin = 1 - x (left edge of circle)
// end = 1 + x (right edge of circle)
let bar = Bar::new(2.0, 1.0 - x, 1.0 + x)
.with_width(SIZE * 2)
.with_color(color);
// Center-align the bar
let aligned = Align::center(Box::new(bar));
// Print without extra newline (bar already includes one)
console.print(&aligned, None, None, None, false, "")?;
}
Ok(())
}