Skip to content

Commit 203a82d

Browse files
committed
Add support for gradients
Signed-off-by: Joe Grund <[email protected]>
1 parent 12281c1 commit 203a82d

2 files changed

Lines changed: 642 additions & 20 deletions

File tree

examples/gradient.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use console::{style, Color, Style};
2+
3+
fn main() {
4+
// Basic foreground gradient using the builder API
5+
println!(
6+
"Foreground gradient: {}",
7+
style("Hello, gradient world!")
8+
.fg(Color::TrueColor(255, 0, 0))
9+
.fg_end(Color::TrueColor(0, 0, 255))
10+
);
11+
12+
// Background gradient
13+
println!(
14+
"Background gradient: {}",
15+
style(" Rainbow background ")
16+
.bg(Color::TrueColor(255, 0, 0))
17+
.bg_end(Color::TrueColor(0, 255, 0))
18+
);
19+
20+
// Both foreground and background gradients
21+
println!(
22+
"Both gradients: {}",
23+
style("Fancy text!")
24+
.fg(Color::TrueColor(255, 255, 0))
25+
.fg_end(Color::TrueColor(255, 0, 255))
26+
.bg(Color::TrueColor(0, 0, 128))
27+
.bg_end(Color::TrueColor(0, 128, 0))
28+
);
29+
30+
// Using from_dotted_str for gradient specification
31+
let gradient_style = Style::from_dotted_str("#ff0000->#00ff00.bold");
32+
println!(
33+
"From dotted string: {}",
34+
gradient_style.apply_to("Styled from_dotted_str")
35+
);
36+
37+
// Gradient with named colors (they get converted to RGB for interpolation)
38+
println!(
39+
"Named color gradient: {}",
40+
style("Red to Blue").fg(Color::Red).fg_end(Color::Blue)
41+
);
42+
43+
// Progress bar simulation
44+
let width = 40;
45+
let progress = 0.7; // 70% complete
46+
let filled = (width as f32 * progress) as usize;
47+
let bar: String = "█".repeat(filled) + &"░".repeat(width - filled);
48+
println!(
49+
"Progress: [{}] {}%",
50+
style(&bar)
51+
.fg(Color::TrueColor(0, 255, 0))
52+
.fg_end(Color::TrueColor(0, 128, 255)),
53+
(progress * 100.0) as u32
54+
);
55+
}

0 commit comments

Comments
 (0)