fix: alternate colors more nicely

This commit is contained in:
Josh McKinney 2024-09-04 12:21:10 -07:00
parent b5f2c0cef3
commit 2fe946b3e5
No known key found for this signature in database
GPG key ID: 722287396A903BC5

View file

@ -5,6 +5,7 @@ use std::{
use color_eyre::Result; use color_eyre::Result;
use crossterm::event::{self, Event, KeyCode, KeyEventKind}; use crossterm::event::{self, Event, KeyCode, KeyEventKind};
use itertools::Itertools;
use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit}; use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit};
use metrics_util::{ use metrics_util::{
registry::{AtomicStorage, Registry}, registry::{AtomicStorage, Registry},
@ -13,7 +14,7 @@ use metrics_util::{
use ratatui::{ use ratatui::{
buffer::Buffer, buffer::Buffer,
layout::{Constraint, Layout, Rect}, layout::{Constraint, Layout, Rect},
style::Stylize, style::{palette::tailwind::SLATE, Stylize},
widgets::{Row, Table, Widget}, widgets::{Row, Table, Widget},
DefaultTerminal, Frame, DefaultTerminal, Frame,
}; };
@ -166,6 +167,7 @@ impl Widget for &RecorderWidget {
summary.add(data); summary.add(data);
} }
if summary.is_empty() { if summary.is_empty() {
// we omit the empty histograms, but this is how you would render them
// histograms.push((key.clone(), "empty".to_string())); // histograms.push((key.clone(), "empty".to_string()));
} else { } else {
let min = Duration::from_secs_f64(summary.min()); let min = Duration::from_secs_f64(summary.min());
@ -187,17 +189,12 @@ impl Widget for &RecorderWidget {
.iter() .iter()
.chain(gauges.iter()) .chain(gauges.iter())
.chain(histograms.iter()); .chain(histograms.iter());
let row_colors = [SLATE.c950, SLATE.c900];
let rows = lines let rows = lines
.map(|(key, line)| Row::new([key.name(), line])) .map(|(key, line)| Row::new([key.name(), line]))
.enumerate() .zip(row_colors.iter().cycle())
.map(|(i, row)| { .map(|(row, style)| row.bg(*style))
if (i % 2) == 0 { .collect_vec();
row.bg(ratatui::style::palette::tailwind::SLATE.c950)
} else {
row.bg(ratatui::style::palette::tailwind::SLATE.c900)
}
})
.collect::<Vec<_>>();
Table::new(rows, [Constraint::Length(40), Constraint::Fill(1)]).render(area, buf); Table::new(rows, [Constraint::Length(40), Constraint::Fill(1)]).render(area, buf);
} }
} }