use ratatui::{
layout::Rect,
style::{Color, Style},
widgets::Gauge,
Frame,
};
pub fn context_bar_color(ratio: f64) -> Color {
if ratio < 0.5 {
Color::Green
} else if ratio < 0.8 {
Color::Yellow
} else {
Color::Red
}
}
pub fn render_context_bar(frame: &mut Frame, area: Rect, ratio: f64) {
let ratio = ratio.clamp(0.0, 1.0);
let color = context_bar_color(ratio);
let percent = (ratio * 100.0) as u16;
let gauge = Gauge::default()
.gauge_style(Style::default().fg(color))
.ratio(ratio)
.label(format!("{}%", percent));
frame.render_widget(gauge, area);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_bar_color_green() {
assert_eq!(context_bar_color(0.0), Color::Green);
assert_eq!(context_bar_color(0.25), Color::Green);
assert_eq!(context_bar_color(0.49), Color::Green);
}
#[test]
fn test_context_bar_color_yellow() {
assert_eq!(context_bar_color(0.5), Color::Yellow);
assert_eq!(context_bar_color(0.65), Color::Yellow);
assert_eq!(context_bar_color(0.79), Color::Yellow);
}
#[test]
fn test_context_bar_color_red() {
assert_eq!(context_bar_color(0.8), Color::Red);
assert_eq!(context_bar_color(0.9), Color::Red);
assert_eq!(context_bar_color(1.0), Color::Red);
}
}