use crate::layout::text_width;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style},
};
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnnotationKind {
HLine(f64),
VLine(f64),
XRegion(f64, f64),
YRegion(f64, f64),
Label(f64, f64),
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Annotation {
pub kind: AnnotationKind,
pub style: Style,
pub text: Option<String>,
}
impl Annotation {
fn make(kind: AnnotationKind, text: Option<String>) -> Self {
Self {
kind,
style: Style::default().fg(Color::Yellow),
text,
}
}
pub fn hline(y: f64) -> Self {
Self::make(AnnotationKind::HLine(y), None)
}
pub fn vline(x: f64) -> Self {
Self::make(AnnotationKind::VLine(x), None)
}
pub fn x_region(x0: f64, x1: f64) -> Self {
let mut a = Self::make(AnnotationKind::XRegion(x0, x1), None);
a.style = Style::default().fg(Color::Gray).bg(Color::DarkGray);
a
}
pub fn y_region(y0: f64, y1: f64) -> Self {
let mut a = Self::make(AnnotationKind::YRegion(y0, y1), None);
a.style = Style::default().fg(Color::Gray).bg(Color::DarkGray);
a
}
pub fn label(x: f64, y: f64, text: impl Into<String>) -> Self {
Self::make(AnnotationKind::Label(x, y), Some(text.into()))
}
pub fn color(mut self, color: Color) -> Self {
self.style = self.style.fg(color);
if matches!(
self.kind,
AnnotationKind::XRegion(..) | AnnotationKind::YRegion(..)
) {
self.style = self.style.bg(color);
}
self
}
setter!(
style: Style
);
pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
}
fn put_text(buf: &mut Buffer, plot: Rect, col: i32, row: i32, text: &str, style: Style) {
if row < 0 || row >= plot.height as i32 {
return;
}
let w = text_width(text) as i32;
let start = col.clamp(0, (plot.width as i32 - w).max(0));
let max = plot.width as i32 - start;
if max <= 0 {
return;
}
buf.set_stringn(
plot.x + start as u16,
plot.y + row as u16,
text,
max as usize,
style,
);
}
fn set_glyph(buf: &mut Buffer, x: u16, y: u16, ch: char, style: Style) {
if let Some(c) = buf.cell_mut((x, y)) {
c.set_char(ch).set_style(style);
}
}
pub fn draw_annotations(
buf: &mut Buffer,
plot: Rect,
annotations: &[Annotation],
x_cell: &dyn Fn(f64) -> Option<i32>,
y_cell: &dyn Fn(f64) -> Option<i32>,
) {
if plot.width == 0 || plot.height == 0 {
return;
}
let (w, h) = (plot.width as i32, plot.height as i32);
let mut taken: Vec<(i32, i32, i32)> = Vec::new();
let mut labels: Vec<(i32, i32, &str, Style)> = Vec::new();
for a in annotations {
let text = a.text.as_deref();
match a.kind {
AnnotationKind::HLine(y) => {
let Some(r) = y_cell(y).filter(|r| (0..h).contains(r)) else {
continue;
};
for c in 0..w {
set_glyph(buf, plot.x + c as u16, plot.y + r as u16, '╌', a.style);
}
if let Some(t) = text {
let row = if r > 0 { r - 1 } else { r };
put_text(buf, plot, w - text_width(t) as i32, row, t, a.style);
}
}
AnnotationKind::VLine(x) => {
let Some(c) = x_cell(x).filter(|c| (0..w).contains(c)) else {
continue;
};
for r in 0..h {
set_glyph(buf, plot.x + c as u16, plot.y + r as u16, '╎', a.style);
}
if let Some(t) = text {
let tw = text_width(t) as i32;
let col = if c + 1 + tw <= w { c + 1 } else { c - tw };
let row = (0..h)
.find(|r| {
!taken
.iter()
.any(|&(tr, a0, a1)| tr == *r && col <= a1 && col + tw > a0)
})
.unwrap_or(0);
taken.push((row, col, col + tw - 1));
labels.push((col, row, t, a.style));
}
}
AnnotationKind::XRegion(x0, x1) => {
let (Some(c0), Some(c1)) = (x_cell(x0), x_cell(x1)) else {
continue;
};
let (lo, hi) = (c0.min(c1).max(0), c0.max(c1).min(w - 1));
let bg = region_bg(a.style);
for c in lo..=hi {
for r in 0..h {
shade(buf, plot.x + c as u16, plot.y + r as u16, bg);
}
}
if let (Some(t), true) = (text, lo <= hi) {
put_text(buf, plot, lo, 0, t, a.style);
}
}
AnnotationKind::YRegion(y0, y1) => {
let (Some(r0), Some(r1)) = (y_cell(y0), y_cell(y1)) else {
continue;
};
let (lo, hi) = (r0.min(r1).max(0), r0.max(r1).min(h - 1));
let bg = region_bg(a.style);
for r in lo..=hi {
for c in 0..w {
shade(buf, plot.x + c as u16, plot.y + r as u16, bg);
}
}
if let (Some(t), true) = (text, lo <= hi) {
put_text(buf, plot, 0, lo, t, a.style);
}
}
AnnotationKind::Label(x, y) => {
if let (Some(c), Some(r), Some(t)) = (x_cell(x), y_cell(y), text) {
if (0..w).contains(&c) {
put_text(buf, plot, c, r, t, a.style);
}
}
}
}
}
for (col, row, text, style) in labels {
put_text(buf, plot, col, row, text, style);
}
}
fn region_bg(style: Style) -> Color {
style.bg.or(style.fg).unwrap_or(Color::DarkGray)
}
fn shade(buf: &mut Buffer, x: u16, y: u16, bg: Color) {
if let Some(c) = buf.cell_mut((x, y)) {
c.set_bg(bg);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn map(v: f64) -> Option<i32> {
v.is_finite().then(|| v.round() as i32)
}
#[test]
fn builders() {
let a = Annotation::hline(1.0).color(Color::Red).text("lim");
assert_eq!(a.kind, AnnotationKind::HLine(1.0));
assert_eq!(a.style.fg, Some(Color::Red));
assert_eq!(a.text.as_deref(), Some("lim"));
let r = Annotation::x_region(1.0, 2.0).color(Color::Blue);
assert_eq!(r.style.bg, Some(Color::Blue));
}
#[test]
fn draws_lines_and_region() {
let plot = Rect::new(1, 1, 10, 5);
let mut buf = Buffer::empty(Rect::new(0, 0, 12, 7));
let anns = [
Annotation::hline(2.0),
Annotation::vline(4.0).text("v"),
Annotation::y_region(3.0, 4.0).color(Color::Blue),
];
draw_annotations(&mut buf, plot, &anns, &map, &map);
assert_eq!(buf[(2, 3)].symbol(), "╌");
assert_eq!(buf[(5, 5)].symbol(), "╎");
assert_eq!(buf[(6, 1)].symbol(), "v");
assert_eq!(buf[(8, 4)].bg, Color::Blue);
}
#[test]
fn labels_of_close_vertical_lines_do_not_overlap() {
let plot = Rect::new(0, 0, 30, 4);
let mut buf = Buffer::empty(plot);
let anns = [
Annotation::vline(5.0).text("first"),
Annotation::vline(7.0).text("second"),
Annotation::vline(20.0).text("far"),
];
draw_annotations(&mut buf, plot, &anns, &map, &map);
let row = |y: u16| {
(0..30)
.map(|x| buf[(x, y)].symbol().to_string())
.collect::<String>()
};
assert!(row(0).contains("first"), "{}", row(0));
assert!(row(1).contains("second"), "{}", row(1));
assert!(row(0).contains("far"), "{}", row(0));
}
#[test]
fn out_of_range_and_nan_safe() {
let plot = Rect::new(0, 0, 4, 3);
let mut buf = Buffer::empty(plot);
let anns = [
Annotation::hline(99.0),
Annotation::vline(f64::NAN),
Annotation::x_region(-50.0, 50.0),
Annotation::label(1.0, 1.0, "very long label text"),
Annotation::label(-5.0, 1.0, "x"),
];
draw_annotations(&mut buf, plot, &anns, &map, &map);
draw_annotations(&mut buf, Rect::new(0, 0, 0, 0), &anns, &map, &map);
}
}