feat(colors): add parse_color_config function
Swadhin Biswas committed 7 months ago
2d5bd2d
1 parent 0fc9e5f
commit 2d5bd2d
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/colors/mod.rs
+50-0
| @@ -128,3 +128,53 @@ impl Default for ColorScheme { | ||
128 | 128 | /// Blocks are split into two rows: standard (0-7) and extended (8+). |
129 | 129 | pub fn color_blocks(start: u8, end: u8, width: usize) -> Vec<String> { |
130 | 130 | let block = "\u{2588}".repeat(width); // █ repeated |
131 | + let mut row1 = String::new(); // standard colors 0-7 | |
132 | + let mut row2 = String::new(); // extended colors 8+ | |
133 | + | |
134 | + for i in start..=end { | |
135 | + match i { | |
136 | + 0..=7 => { | |
137 | + // Standard ANSI: set both fg and bg to same color | |
138 | + row1.push_str(&format!("\x1b[3{}m\x1b[4{}m{}", i, i, block)); | |
139 | + } | |
140 | + _ => { | |
141 | + // 256-color mode: set both fg and bg | |
142 | + row2.push_str(&format!("\x1b[38;5;{}m\x1b[48;5;{}m{}", i, i, block)); | |
143 | + } | |
144 | + } | |
145 | + } | |
146 | + | |
147 | + let mut lines = Vec::new(); | |
148 | + if !row1.is_empty() { | |
149 | + row1.push_str(RESET); | |
150 | + lines.push(row1); | |
151 | + } | |
152 | + if !row2.is_empty() { | |
153 | + row2.push_str(RESET); | |
154 | + lines.push(row2); | |
155 | + } | |
156 | + lines | |
157 | +} | |
158 | + | |
159 | +/// Generate a progress bar for memory/disk usage. | |
160 | +/// `percent` is 0-100. `width` is the total bar width in chars. | |
161 | +/// Returns something like: `[████████░░░░░░░░] 50%` | |
162 | +pub fn progress_bar(percent: f64, width: usize, bar_color: &str) -> String { | |
163 | + let pct = percent.clamp(0.0, 100.0); | |
164 | + let filled = ((pct / 100.0) * width as f64).round() as usize; | |
165 | + let empty = width.saturating_sub(filled); | |
166 | + | |
167 | + let bar = format!( | |
168 | + "{}{}{}{}{}", | |
169 | + bar_color, | |
170 | + "█".repeat(filled), | |
171 | + RESET, | |
172 | + "░".repeat(empty), | |
173 | + RESET, | |
174 | + ); | |
175 | + format!("[{}] {:.0}%", bar, pct) | |
176 | +} | |
177 | + | |
178 | +/// Apply a gradient effect to text using 256-color (or RGB) ANSI codes. | |
179 | +/// Interpolates between `start_color` (r,g,b) and `end_color` (r,g,b) across the text. | |
180 | +#[allow(dead_code)] | |
131 | 181 | |