use std::fmt;
use blubat_core::{Device, Direction, Event, Levels, Raised, Thresholds, Trend};
use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::symbols::Marker;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Axis, Block, BorderType, Chart, Dataset, GraphType, Paragraph};
use super::app::App;
use super::render;
use super::theme::{self, Palette};
const SECONDS_PER_HOUR: f64 = 3_600.0;
const CREEPING: f64 = 0.1;
const STATS_WIDTH: u16 = 33;
const EVENTS_HEIGHT: u16 = 7;
const SEPARATOR: &str = " \u{b7} ";
const BAR_SHARE: usize = 4;
const BAR_CEILING: usize = 48;
pub fn render(frame: &mut Frame, app: &App, device: &Device, area: Rect) {
let palette = app.look.palette;
let outer = panel(&format!(" blubat | {} ", device.name), palette);
let inner = outer.inner(area);
frame.render_widget(outer, area);
let [power, middle, events, footer] = Layout::vertical([
Constraint::Length(power_height(device.levels)),
Constraint::Min(0),
Constraint::Length(EVENTS_HEIGHT),
Constraint::Length(1),
])
.areas(inner);
let [chart, stats] =
Layout::horizontal([Constraint::Min(0), Constraint::Length(STATS_WIDTH)]).areas(middle);
render_power(frame, app, device, power);
render_chart(frame, app, device, chart);
render_stats(frame, app, device, stats);
render_events(frame, app, device, events);
frame.render_widget(render::keys_footer(app), footer);
}
fn panel(title: &str, palette: Palette) -> Block<'static> {
Block::bordered()
.border_type(BorderType::Rounded)
.border_style(Style::new().fg(palette.dim))
.title(Span::styled(title.to_string(), palette.accent))
}
fn power_height(levels: Levels) -> u16 {
let parts = if levels.multi_battery() {
levels.present().count()
} else {
0
};
4 + u16::try_from(parts).unwrap_or(0)
}
fn render_power(frame: &mut Frame, app: &App, device: &Device, area: Rect) {
let block = panel(" power ", app.look.palette);
let inner = block.inner(area);
frame.render_widget(block, area);
let lines: Vec<Line<'static>> = std::iter::once(level_line(app, device, inner.width))
.chain(sub_levels(app, device))
.chain([reading_line(app, device)])
.collect();
frame.render_widget(Paragraph::new(lines), inner);
}
fn level_line(app: &App, device: &Device, width: u16) -> Line<'static> {
let palette = app.look.palette;
let level = device.levels.lowest();
let colour = palette.level(level, app.thresholds(device));
let (filled, trough) = theme::bar(level, bar_width(width));
Line::from(vec![
Span::styled(
format!("{:>4} ", theme::percent(level)),
Style::new().fg(colour).add_modifier(Modifier::BOLD),
),
Span::styled(filled, colour),
Span::styled(trough, palette.dim),
Span::styled(format!(" {}", doing(app, device)), palette.text),
])
}
fn sub_levels<'a>(app: &'a App, device: &'a Device) -> impl Iterator<Item = Line<'static>> + 'a {
let palette = app.look.palette;
let thresholds = app.thresholds(device);
let multi = device.levels.multi_battery();
device
.levels
.present()
.filter(move |_| multi)
.map(move |(part, level)| {
let level = Some(level);
let (filled, trough) = theme::bar(level, theme::BAR_WIDTH);
Line::from(vec![
Span::styled(format!(" {:<8}", part.to_string()), palette.dim),
Span::styled(
format!("{:>4} ", theme::percent(level)),
palette.level(level, thresholds),
),
Span::styled(filled, palette.level(level, thresholds)),
Span::styled(trough, palette.dim),
])
})
}
fn reading_line(app: &App, device: &Device) -> Line<'static> {
let palette = app.look.palette;
let link = if device.connected {
"connected"
} else {
"last seen"
};
let said = format!(
"{link}{SEPARATOR}{}{SEPARATOR}last reading {}",
device.source,
theme::age(app.now.unix().saturating_sub(device.read_at.unix()))
);
let stale = if app.is_stale(device) {
Span::styled(format!("{SEPARATOR}stale"), palette.low)
} else {
Span::raw("")
};
Line::from(vec![Span::styled(said, palette.dim), stale])
}
fn doing(app: &App, device: &Device) -> String {
let state = if device.connected {
device.charge.to_string()
} else {
"last seen level".to_string()
};
match estimate(app, device) {
Some(estimate) => format!("{state}, est. {estimate}"),
None => state,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Estimate {
ToFull(i64),
ToEmpty(i64),
}
impl fmt::Display for Estimate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (seconds, end) = match *self {
Estimate::ToFull(seconds) => (seconds, "full"),
Estimate::ToEmpty(seconds) => (seconds, "empty"),
};
write!(f, "{} to {end}", theme::span(seconds))
}
}
impl Estimate {
fn of(level: Option<u8>, trend: Option<Trend>, thresholds: Thresholds) -> Option<Self> {
let level = f64::from(level?);
let trend = trend.filter(|trend| trend.rate.abs() >= CREEPING)?;
let seconds = |points: f64| (points.max(0.0) / trend.rate.abs() * SECONDS_PER_HOUR) as i64;
match trend.direction {
Direction::Rising => Some(Self::ToFull(seconds(f64::from(thresholds.high) - level))),
Direction::Falling => Some(Self::ToEmpty(seconds(level))),
Direction::Flat => None,
}
}
fn left(self) -> String {
let (Self::ToFull(seconds) | Self::ToEmpty(seconds)) = self;
theme::span(seconds)
}
}
fn estimate(app: &App, device: &Device) -> Option<Estimate> {
Estimate::of(
device.active_level(),
app.history.trend(&device.address),
app.thresholds(device),
)
}
fn render_chart(frame: &mut Frame, app: &App, device: &Device, area: Rect) {
let palette = app.look.palette;
let levels = points(app, device);
let Some(oldest) = levels
.first()
.map(|(hours, _)| *hours)
.filter(|hours| *hours < 0.0)
else {
let block = panel(" battery ", palette);
frame.render_widget(
Paragraph::new("no history yet: the chart fills as blubat polls")
.style(palette.dim)
.block(block),
area,
);
return;
};
let low = f64::from(app.thresholds(device).low);
let threshold = [(oldest, low), (0.0, low)];
let axis = Style::new().fg(palette.dim);
let chart = Chart::new(vec![
Dataset::default()
.marker(Marker::Dot)
.graph_type(GraphType::Line)
.style(axis)
.data(&threshold),
Dataset::default()
.marker(Marker::Braille)
.graph_type(GraphType::Line)
.style(Style::new().fg(palette.accent))
.data(&levels),
])
.block(panel(
&format!(
" battery, last {} ",
theme::span((-oldest * SECONDS_PER_HOUR) as i64)
),
palette,
))
.legend_position(None)
.x_axis(Axis::default().style(axis).bounds([oldest, 0.0]).labels([
format!("{} ago", theme::span((-oldest * SECONDS_PER_HOUR) as i64)),
"now".to_string(),
]))
.y_axis(
Axis::default()
.style(axis)
.bounds([0.0, 100.0])
.labels(["0", "50", "100"]),
);
frame.render_widget(chart, area);
}
fn points(app: &App, device: &Device) -> Vec<(f64, f64)> {
app.history
.samples(&device.address)
.map(|sample| {
(
(sample.at.unix() - app.now.unix()) as f64 / SECONDS_PER_HOUR,
f64::from(sample.level),
)
})
.collect()
}
fn render_stats(frame: &mut Frame, app: &App, device: &Device, area: Rect) {
let palette = app.look.palette;
let block = panel(" stats ", palette);
let inner = block.inner(area);
frame.render_widget(block, area);
let width = usize::from(inner.width);
let thresholds = app.thresholds(device);
let trend = app.history.trend(&device.address);
let estimate = estimate(app, device);
let row = |label, value: String, colour| stat(width, label, &value, colour, palette);
let lines = vec![
row(rate_label(trend), rate(trend), palette.text),
row("trend", direction(trend), trend_colour(trend, palette)),
row(
estimate_label(estimate),
estimate.map_or_else(|| theme::UNKNOWN.to_string(), Estimate::left),
palette.text,
),
Line::default(),
row("low", theme::percent(Some(thresholds.low)), palette.low),
row(
"critical",
theme::percent(Some(thresholds.critical)),
palette.critical,
),
row(
"charged at",
theme::percent(Some(thresholds.high)),
palette.ok,
),
Line::default(),
row("address", device.address.as_str().to_string(), palette.text),
row("source", device.source.to_string(), palette.text),
row(
"type",
device.kind.clone().unwrap_or_else(|| theme::UNKNOWN.into()),
palette.text,
),
row(
"samples",
app.history.samples(&device.address).count().to_string(),
palette.text,
),
];
frame.render_widget(Paragraph::new(lines), inner);
}
fn stat(width: usize, label: &str, value: &str, colour: Color, palette: Palette) -> Line<'static> {
let spent = label.chars().count() + value.chars().count();
let gap = width.saturating_sub(spent).max(1);
Line::from(vec![
Span::styled(label.to_string(), palette.dim),
Span::raw(" ".repeat(gap)),
Span::styled(value.to_string(), colour),
])
}
fn rate_label(trend: Option<Trend>) -> &'static str {
match trend.map(|trend| trend.direction) {
Some(Direction::Rising) => "charge rate",
Some(Direction::Falling) => "drain rate",
_ => "rate",
}
}
fn rate(trend: Option<Trend>) -> String {
trend.map_or_else(
|| theme::UNKNOWN.to_string(),
|trend| theme::rate(trend.rate),
)
}
fn direction(trend: Option<Trend>) -> String {
trend.map_or_else(
|| theme::UNKNOWN.to_string(),
|trend| trend.direction.to_string(),
)
}
fn trend_colour(trend: Option<Trend>, palette: Palette) -> Color {
match trend.map(|trend| trend.direction) {
Some(Direction::Rising) => palette.charging,
Some(Direction::Falling) => palette.low,
Some(Direction::Flat) => palette.text,
None => palette.dim,
}
}
fn estimate_label(estimate: Option<Estimate>) -> &'static str {
match estimate {
Some(Estimate::ToFull(_)) => "to full",
Some(Estimate::ToEmpty(_)) => "to empty",
None => "estimate",
}
}
fn render_events(frame: &mut Frame, app: &App, device: &Device, area: Rect) {
let palette = app.look.palette;
let block = panel(" recent events ", palette);
let inner = block.inner(area);
frame.render_widget(block, area);
let mut lines: Vec<Line<'static>> = app
.journal
.recent(&device.address)
.map(|raised| event_line(app, raised))
.collect();
if lines.is_empty() {
lines.push(Line::from(Span::styled(
"nothing raised for this device yet",
palette.dim,
)));
}
frame.render_widget(Paragraph::new(lines), inner);
}
fn event_line(app: &App, raised: &Raised) -> Line<'static> {
let palette = app.look.palette;
let level = theme::percent(raised.level);
let threshold = raised
.threshold
.map(|threshold| format!(", threshold {threshold}%"))
.unwrap_or_default();
Line::from(vec![
Span::styled(
format!(
"{:<10}",
theme::age(app.now.unix().saturating_sub(raised.at.unix()))
),
palette.dim,
),
Span::styled(
format!("{:<18}", raised.event.to_string()),
event_colour(raised.event, palette),
),
Span::styled(format!("at {level}{threshold}"), palette.text),
])
}
fn event_colour(event: Event, palette: Palette) -> Color {
match event {
Event::LowBattery | Event::Stale => palette.low,
Event::CriticalBattery => palette.critical,
Event::Charged | Event::Connected => palette.ok,
Event::Disconnected => palette.dim,
}
}
fn bar_width(width: u16) -> usize {
(usize::from(width) * BAR_SHARE / 10).min(BAR_CEILING)
}
#[cfg(test)]
mod tests {
use blubat_core::Direction;
use super::*;
const HOUR: f64 = 3_600.0;
fn trend(rate: f64) -> Option<Trend> {
let direction = match rate {
rate if rate > 0.0 => Direction::Rising,
rate if rate < 0.0 => Direction::Falling,
_ => Direction::Flat,
};
Some(Trend { rate, direction })
}
fn levels(present: [Option<u8>; 4]) -> Levels {
Levels {
main: present[0],
left: present[1],
right: present[2],
case: present[3],
}
}
fn estimate(level: Option<u8>, rate: f64) -> Option<Estimate> {
Estimate::of(level, trend(rate), Thresholds::BUILT_IN)
}
#[test]
fn a_climbing_level_is_counted_up_to_the_charged_threshold() {
assert_eq!(estimate(Some(23), 10.0), Some(Estimate::ToFull(27_720)));
assert_eq!(
estimate(Some(23), 10.0).map(Estimate::left),
Some("7h 42m".to_string())
);
assert_eq!(
estimate(Some(23), 10.0).map(|estimate| estimate.to_string()),
Some("7h 42m to full".to_string())
);
}
#[test]
fn a_dropping_level_is_counted_down_to_empty() {
assert_eq!(estimate(Some(23), -4.0), Some(Estimate::ToEmpty(20_700)));
assert_eq!(
estimate(Some(23), -4.0).map(|estimate| estimate.to_string()),
Some("5h 45m to empty".to_string())
);
}
#[test]
fn a_level_going_nowhere_has_nothing_to_estimate() {
assert_eq!(estimate(Some(23), 0.0), None, "a flat trend");
assert_eq!(
Estimate::of(Some(23), None, Thresholds::BUILT_IN),
None,
"and one reading, which is no trend at all"
);
assert_eq!(estimate(None, -4.0), None, "as is nothing to count from");
}
#[test]
fn a_level_that_is_barely_moving_is_going_nowhere_too() {
assert_eq!(estimate(Some(100), -0.001), None);
assert_eq!(estimate(Some(100), 0.001), None);
assert_eq!(
estimate(Some(100), -CREEPING),
Some(Estimate::ToEmpty(3_600_000)),
"and the slowest rate that is still worth a figure keeps one"
);
}
#[test]
fn a_level_already_past_the_threshold_it_climbs_to_is_there_now() {
assert_eq!(estimate(Some(100), 10.0), Some(Estimate::ToFull(0)));
assert_eq!(
estimate(Some(100), 10.0).map(Estimate::left),
Some("0m".to_string()),
"rather than a wait that runs backwards"
);
}
#[test]
fn the_charged_threshold_the_device_is_judged_by_is_the_one_counted_to() {
let unplug_at_80 = Thresholds {
high: 80,
..Thresholds::BUILT_IN
};
assert_eq!(
Estimate::of(Some(20), trend(10.0), unplug_at_80),
Some(Estimate::ToFull(6 * HOUR as i64))
);
}
#[test]
fn the_power_panel_grows_a_row_for_every_battery_beyond_the_first() {
let airpods = levels([None, Some(100), Some(97), Some(68)]);
assert_eq!(power_height(levels([Some(42), None, None, None])), 4);
assert_eq!(
power_height(Levels::default()),
4,
"nothing read is one row"
);
assert_eq!(power_height(airpods), 7);
assert_eq!(power_height(levels([Some(42), Some(9), None, None])), 6);
}
#[test]
fn the_bar_takes_a_share_of_the_panel_up_to_a_ceiling() {
assert_eq!(bar_width(0), 0);
assert_eq!(bar_width(30), 12);
assert_eq!(bar_width(96), 38);
assert_eq!(bar_width(400), BAR_CEILING, "a wide terminal stops here");
}
#[test]
fn a_rate_is_labelled_by_the_direction_that_makes_it_news() {
assert_eq!(rate_label(trend(10.0)), "charge rate");
assert_eq!(rate_label(trend(-4.0)), "drain rate");
assert_eq!(rate_label(trend(0.0)), "rate");
assert_eq!(rate_label(None), "rate");
assert_eq!(rate(trend(-4.24)), "4.2%/h");
assert_eq!(rate(None), theme::UNKNOWN);
assert_eq!(direction(trend(10.0)), "rising");
assert_eq!(direction(None), theme::UNKNOWN);
}
#[test]
fn an_estimate_names_the_end_it_is_heading_for() {
assert_eq!(estimate_label(estimate(Some(23), 10.0)), "to full");
assert_eq!(estimate_label(estimate(Some(23), -4.0)), "to empty");
assert_eq!(estimate_label(None), "estimate");
}
}