65 lines
2.2 KiB
Rust
65 lines
2.2 KiB
Rust
use crate::theme;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::style::Color;
|
|
use ratatui::widgets::Widget;
|
|
|
|
const BLOCKS: [char; 8] = ['\u{2581}', '\u{2582}', '\u{2583}', '\u{2584}', '\u{2585}', '\u{2586}', '\u{2587}', '\u{2588}'];
|
|
|
|
pub struct Spectrum<'a> {
|
|
data: &'a [f32; 32],
|
|
}
|
|
|
|
impl<'a> Spectrum<'a> {
|
|
pub fn new(data: &'a [f32; 32]) -> Self {
|
|
Self { data }
|
|
}
|
|
}
|
|
|
|
impl Widget for Spectrum<'_> {
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
|
if area.width == 0 || area.height == 0 {
|
|
return;
|
|
}
|
|
|
|
let colors = theme::get();
|
|
let height = area.height as f32;
|
|
let band_width = area.width as usize / 32;
|
|
if band_width == 0 {
|
|
return;
|
|
}
|
|
|
|
for (band, &mag) in self.data.iter().enumerate() {
|
|
let bar_height = mag * height;
|
|
let full_cells = bar_height as usize;
|
|
let frac = bar_height - full_cells as f32;
|
|
let frac_idx = (frac * 8.0) as usize;
|
|
|
|
let x_start = area.x + (band * band_width) as u16;
|
|
|
|
for row in 0..area.height as usize {
|
|
let y = area.y + area.height - 1 - row as u16;
|
|
let ratio = row as f32 / area.height as f32;
|
|
let color = if ratio < 0.33 {
|
|
Color::Rgb(colors.meter.low_rgb.0, colors.meter.low_rgb.1, colors.meter.low_rgb.2)
|
|
} else if ratio < 0.66 {
|
|
Color::Rgb(colors.meter.mid_rgb.0, colors.meter.mid_rgb.1, colors.meter.mid_rgb.2)
|
|
} else {
|
|
Color::Rgb(colors.meter.high_rgb.0, colors.meter.high_rgb.1, colors.meter.high_rgb.2)
|
|
};
|
|
for dx in 0..band_width as u16 {
|
|
let x = x_start + dx;
|
|
if x >= area.x + area.width {
|
|
break;
|
|
}
|
|
if row < full_cells {
|
|
buf[(x, y)].set_char(BLOCKS[7]).set_fg(color);
|
|
} else if row == full_cells && frac_idx > 0 {
|
|
buf[(x, y)].set_char(BLOCKS[frac_idx - 1]).set_fg(color);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|