Skip to main content

color_usage/
color_usage.rs

1//! Color system demonstration showing different ways to style terminal text.
2//!
3//! This example shows:
4//! - Named colors vs RGB vs ANSI colors
5//! - Foreground and background styling
6//! - Predefined color pairs for common use cases
7//! - Practical color usage patterns
8
9use minui::prelude::*;
10
11fn main() -> minui::Result<()> {
12    let mut app = App::new(())?;
13
14    app.run(
15        |_state, event| {
16            // Return false to exit.
17            //
18            // The keyboard handler may emit:
19            // - `Event::KeyWithModifiers(KeyKind::Char('q'))` (modifier-aware), or
20            // - `Event::Character('q')` (legacy fallback).
21            match event {
22                Event::KeyWithModifiers(k) if matches!(k.key, KeyKind::Char('q')) => false,
23                Event::Character('q') => false,
24                _ => true,
25            }
26        },
27        |_state, window| {
28            // Display title at the top
29            window.write_str(0, 0, "Color Demo (press 'q' to quit)")?;
30
31            // Demonstrate all available foreground colors on black background
32            let colors = [
33                Color::Red,
34                Color::Green,
35                Color::Yellow,
36                Color::Blue,
37                Color::Magenta,
38                Color::Cyan,
39                Color::White,
40            ];
41
42            // Display each color name in its corresponding color
43            for (i, &color) in colors.iter().enumerate() {
44                let color_pair = ColorPair::new(color, Color::Black);
45                window.write_str_colored(
46                    2,               // Row 2
47                    (i as u16) * 10, // Column 0, 10, 20, etc.
48                    &format!("{:?}", color),
49                    color_pair,
50                )?;
51            }
52
53            // Show practical examples of color usage for different types of messages
54            window.write_str_colored(
55                4,
56                0,
57                "Error: Something went wrong!",
58                ColorPair::new(Color::Red, Color::Black),
59            )?;
60
61            window.write_str_colored(
62                5,
63                0,
64                "Success: Operation completed!",
65                ColorPair::new(Color::Green, Color::Black),
66            )?;
67
68            window.write_str_colored(
69                6,
70                0,
71                "Warning: Disk space low",
72                ColorPair::new(Color::Yellow, Color::Black),
73            )?;
74
75            // Example of using background color for emphasis
76            window.write_str_colored(
77                7,
78                0,
79                "URGENT: System failure! jk :)",
80                ColorPair::new(Color::Black, Color::Red),
81            )?;
82
83            window.flush()?;
84            Ok(())
85        },
86    )?;
87
88    Ok(())
89}