|
10 | 10 | //! |
11 | 11 | //! Source: architecture.md#Core Architectural Decisions, epics.md#Story 2.5 |
12 | 12 |
|
| 13 | +/// Centralized style/threshold/format rendering for sub-field render functions. |
| 14 | +/// |
| 15 | +/// Resolves style, thresholds, and format strings using sub-field → parent fallback, |
| 16 | +/// handling `invert_threshold` for decreasing-health indicators (e.g. remaining_percentage). |
| 17 | +/// |
| 18 | +/// # Arguments |
| 19 | +/// - `val_str`: Already-formatted display string (e.g. `"85"`, `"0.0123"`) |
| 20 | +/// - `threshold_val`: Numeric value for threshold comparison; `None` for non-threshold fields |
| 21 | +/// - `sub_cfg`: The sub-field's own `SubfieldConfig` (may be `None`) |
| 22 | +/// - `parent`: Parent config implementing `HasThresholdStyle` for fallback (may be `None`) |
| 23 | +/// |
| 24 | +/// # Returns |
| 25 | +/// `None` when the format path renders empty (conditional group with absent `$value`). |
| 26 | +/// `Some(styled_string)` otherwise. |
| 27 | +pub fn render_styled_value( |
| 28 | + val_str: &str, |
| 29 | + threshold_val: Option<f64>, |
| 30 | + sub_cfg: Option<&crate::config::SubfieldConfig>, |
| 31 | + parent: Option<&dyn crate::config::HasThresholdStyle>, |
| 32 | +) -> Option<String> { |
| 33 | + // Resolve all fields with sub → parent fallback |
| 34 | + let style = sub_cfg |
| 35 | + .and_then(|c| c.style.as_deref()) |
| 36 | + .or_else(|| parent.and_then(|p| p.style())); |
| 37 | + let mut effective_val = threshold_val; |
| 38 | + let mut warn_threshold = sub_cfg |
| 39 | + .and_then(|c| c.warn_threshold) |
| 40 | + .or_else(|| parent.and_then(|p| p.warn_threshold())); |
| 41 | + let mut warn_style = sub_cfg |
| 42 | + .and_then(|c| c.warn_style.as_deref()) |
| 43 | + .or_else(|| parent.and_then(|p| p.warn_style())); |
| 44 | + let mut critical_threshold = sub_cfg |
| 45 | + .and_then(|c| c.critical_threshold) |
| 46 | + .or_else(|| parent.and_then(|p| p.critical_threshold())); |
| 47 | + let mut critical_style = sub_cfg |
| 48 | + .and_then(|c| c.critical_style.as_deref()) |
| 49 | + .or_else(|| parent.and_then(|p| p.critical_style())); |
| 50 | + |
| 51 | + // Inverted thresholds: use sub-only values (negated) and negate the comparison value. |
| 52 | + // Parent thresholds are in the non-inverted domain and must not be inherited. |
| 53 | + if sub_cfg.and_then(|c| c.invert_threshold).unwrap_or(false) { |
| 54 | + effective_val = threshold_val.map(|v| -v); |
| 55 | + warn_threshold = sub_cfg.and_then(|c| c.warn_threshold).map(|t| -t); |
| 56 | + warn_style = sub_cfg.and_then(|c| c.warn_style.as_deref()); |
| 57 | + critical_threshold = sub_cfg.and_then(|c| c.critical_threshold).map(|t| -t); |
| 58 | + critical_style = sub_cfg.and_then(|c| c.critical_style.as_deref()); |
| 59 | + } |
| 60 | + |
| 61 | + // Format path: resolve symbol, threshold style, then apply format |
| 62 | + let fmt = sub_cfg |
| 63 | + .and_then(|c| c.format.as_deref()) |
| 64 | + .or_else(|| parent.and_then(|p| p.format_str())); |
| 65 | + if let Some(fmt) = fmt { |
| 66 | + let symbol = sub_cfg |
| 67 | + .and_then(|c| c.symbol.as_deref()) |
| 68 | + .or_else(|| parent.and_then(|p| p.symbol_str())); |
| 69 | + let effective_style = crate::ansi::resolve_threshold_style( |
| 70 | + effective_val, |
| 71 | + style, |
| 72 | + warn_threshold, |
| 73 | + warn_style, |
| 74 | + critical_threshold, |
| 75 | + critical_style, |
| 76 | + ); |
| 77 | + return apply_module_format(fmt, Some(val_str), symbol, effective_style); |
| 78 | + } |
| 79 | + |
| 80 | + // Default path: apply_style_with_threshold handles no-threshold gracefully |
| 81 | + Some(crate::ansi::apply_style_with_threshold( |
| 82 | + val_str, |
| 83 | + effective_val, |
| 84 | + style, |
| 85 | + warn_threshold, |
| 86 | + warn_style, |
| 87 | + critical_threshold, |
| 88 | + critical_style, |
| 89 | + )) |
| 90 | +} |
| 91 | + |
13 | 92 | /// Apply a per-module format string. |
14 | 93 | /// |
15 | 94 | /// # Arguments |
@@ -150,6 +229,91 @@ fn find_matching_close(s: &str, start: usize, open: char, close: char) -> Option |
150 | 229 | mod tests { |
151 | 230 | use super::*; |
152 | 231 |
|
| 232 | + // --- render_styled_value tests --- |
| 233 | + |
| 234 | + #[test] |
| 235 | + fn test_render_styled_value_with_format_string() { |
| 236 | + let sub = crate::config::SubfieldConfig { |
| 237 | + format: Some("[$value]($style)".to_string()), |
| 238 | + style: Some("bold green".to_string()), |
| 239 | + ..Default::default() |
| 240 | + }; |
| 241 | + let result = render_styled_value("85", Some(85.0), Some(&sub), None); |
| 242 | + let s = result.unwrap(); |
| 243 | + assert!(s.contains("85"), "value present: {s:?}"); |
| 244 | + assert!(s.contains('\x1b'), "ANSI codes present: {s:?}"); |
| 245 | + } |
| 246 | + |
| 247 | + #[test] |
| 248 | + fn test_render_styled_value_no_format_threshold_above_warn() { |
| 249 | + let sub = crate::config::SubfieldConfig { |
| 250 | + warn_threshold: Some(50.0), |
| 251 | + warn_style: Some("yellow".to_string()), |
| 252 | + critical_threshold: Some(90.0), |
| 253 | + critical_style: Some("bold red".to_string()), |
| 254 | + ..Default::default() |
| 255 | + }; |
| 256 | + let result = render_styled_value("75", Some(75.0), Some(&sub), None); |
| 257 | + let s = result.unwrap(); |
| 258 | + assert!(s.contains("75"), "value present: {s:?}"); |
| 259 | + assert!(s.contains('\x1b'), "ANSI codes for warn style: {s:?}"); |
| 260 | + } |
| 261 | + |
| 262 | + #[test] |
| 263 | + fn test_render_styled_value_no_format_no_threshold() { |
| 264 | + let sub = crate::config::SubfieldConfig { |
| 265 | + style: Some("cyan".to_string()), |
| 266 | + ..Default::default() |
| 267 | + }; |
| 268 | + let result = render_styled_value("hello", None, Some(&sub), None); |
| 269 | + let s = result.unwrap(); |
| 270 | + assert!(s.contains("hello"), "value present: {s:?}"); |
| 271 | + assert!(s.contains('\x1b'), "ANSI codes for base style: {s:?}"); |
| 272 | + } |
| 273 | + |
| 274 | + #[test] |
| 275 | + fn test_render_styled_value_invert_threshold() { |
| 276 | + // invert_threshold: value 20 with warn_threshold 30 → inverted: -20 >= -30 → warn fires |
| 277 | + let sub = crate::config::SubfieldConfig { |
| 278 | + invert_threshold: Some(true), |
| 279 | + warn_threshold: Some(30.0), |
| 280 | + warn_style: Some("yellow".to_string()), |
| 281 | + critical_threshold: Some(10.0), |
| 282 | + critical_style: Some("bold red".to_string()), |
| 283 | + ..Default::default() |
| 284 | + }; |
| 285 | + let result = render_styled_value("20", Some(20.0), Some(&sub), None); |
| 286 | + let s = result.unwrap(); |
| 287 | + assert!(s.contains("20"), "value present: {s:?}"); |
| 288 | + assert!(s.contains('\x1b'), "ANSI codes for inverted warn: {s:?}"); |
| 289 | + } |
| 290 | + |
| 291 | + #[test] |
| 292 | + fn test_render_styled_value_parent_fallback_style() { |
| 293 | + let sub = crate::config::SubfieldConfig { |
| 294 | + ..Default::default() |
| 295 | + }; |
| 296 | + let parent = crate::config::ContextWindowConfig { |
| 297 | + style: Some("bold magenta".to_string()), |
| 298 | + ..Default::default() |
| 299 | + }; |
| 300 | + let result = render_styled_value( |
| 301 | + "42", |
| 302 | + None, |
| 303 | + Some(&sub), |
| 304 | + Some(&parent as &dyn crate::config::HasThresholdStyle), |
| 305 | + ); |
| 306 | + let s = result.unwrap(); |
| 307 | + assert!(s.contains("42"), "value present: {s:?}"); |
| 308 | + assert!(s.contains('\x1b'), "ANSI codes from parent style: {s:?}"); |
| 309 | + } |
| 310 | + |
| 311 | + #[test] |
| 312 | + fn test_render_styled_value_no_sub_no_parent() { |
| 313 | + let result = render_styled_value("plain", None, None, None); |
| 314 | + assert_eq!(result, Some("plain".to_string())); |
| 315 | + } |
| 316 | + |
153 | 317 | #[test] |
154 | 318 | fn test_dollar_value_substituted() { |
155 | 319 | let result = apply_module_format("$value", Some("35"), None, None); |
|
0 commit comments