Skip to content

Commit cba9042

Browse files
committed
Add support for gradients
Signed-off-by: Joe Grund <grundjoseph@gmail.com>
1 parent 12281c1 commit cba9042

3 files changed

Lines changed: 648 additions & 23 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(" Gradient background ")
16+
.bg(Color::TrueColor(255, 0, 0))
17+
.bg_end(Color::TrueColor(54, 36, 79))
18+
);
19+
20+
// Both foreground and background gradients
21+
println!(
22+
"Foreground and background 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+
}

src/term.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Term {
228228
TermTarget::Stderr => Style::new().for_stderr(),
229229
TermTarget::Stdout => Style::new().for_stdout(),
230230
#[cfg(unix)]
231-
TermTarget::ReadWritePair(ReadWritePair { ref style, .. }) => style.clone(),
231+
TermTarget::ReadWritePair(ReadWritePair { ref style, .. }) => *style,
232232
}
233233
}
234234

0 commit comments

Comments
 (0)