pub fn check_color_support() -> Result<ColorSupport, ColorError>Expand description
Check the level of color support in the current terminal
§Returns
Ok(ColorSupport)indicating the level of color supportErr(ColorError)if the terminal environment cannot be detected
§Examples
use inksac::{check_color_support, ColorSupport};
match check_color_support() {
Ok(support) => match support {
ColorSupport::TrueColor => println!("Terminal supports true color"),
ColorSupport::Color256 => println!("Terminal supports 256 colors"),
ColorSupport::Basic => println!("Terminal supports basic colors"),
ColorSupport::NoColor => println!("Terminal does not support colors"),
},
Err(e) => eprintln!("Failed to detect color support: {}", e),
}Examples found in repository?
examples/print_color.rs (line 7)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6 // First, check color support
7 let support = check_color_support()?;
8 println!("Color Support Level: {}\n", support);
9
10 // Basic colors
11 let basic_colors = [
12 ("Black", Color::Black),
13 ("Red", Color::Red),
14 ("Green", Color::Green),
15 ("Yellow", Color::Yellow),
16 ("Blue", Color::Blue),
17 ("Magenta", Color::Magenta),
18 ("Cyan", Color::Cyan),
19 ("White", Color::White),
20 ];
21
22 println!("Basic Colors:");
23 for (name, color) in basic_colors {
24 let style = Style::builder().foreground(color).bold().build();
25 println!("{:<8}: {}", name, "■■■■".style(style));
26 }
27 println!();
28
29 // RGB Colors (if supported)
30 if matches!(support, ColorSupport::TrueColor) {
31 println!("RGB Colors:");
32 let rgb_colors = [
33 (255, 100, 0),
34 (100, 255, 0),
35 (0, 255, 100),
36 (0, 100, 255),
37 (100, 0, 255),
38 (255, 0, 100),
39 ];
40
41 for (r, g, b) in rgb_colors {
42 let style = Style::builder()
43 .foreground(Color::new_rgb(r, g, b)?)
44 .build();
45 println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46 }
47 println!();
48
49 // Color manipulation
50 let base_color = Color::new_rgb(255, 100, 0)?;
51 println!("Color Manipulation:");
52 println!(
53 "Original: {}",
54 "■■■■".style(Style::builder().foreground(base_color).build())
55 );
56
57 let lighter = base_color.lighten(30)?;
58 println!(
59 "Lighter : {}",
60 "■■■■".style(Style::builder().foreground(lighter).build())
61 );
62
63 let darker = base_color.darken(30)?;
64 println!(
65 "Darker : {}",
66 "■■■■".style(Style::builder().foreground(darker).build())
67 );
68 println!();
69 }
70
71 // Text styles
72 println!("Text Styles:");
73 let text = "Styled Text";
74
75 let bold = Style::builder().bold().build();
76 println!("Bold: {}", text.style(bold));
77
78 let italic = Style::builder().italic().build();
79 println!("Italic: {}", text.style(italic));
80
81 let underline = Style::builder().underline().build();
82 println!("Underline: {}", text.style(underline));
83
84 let dim = Style::builder().dim().build();
85 println!("Dim: {}", text.style(dim));
86
87 // Combined styles
88 let combined = Style::builder()
89 .foreground(Color::Green)
90 .background(Color::Black)
91 .bold()
92 .italic()
93 .build();
94 println!("\nCombined Styles:");
95 println!("{}", "Multiple styles combined!".style(combined));
96
97 Ok(())
98}