Refactoring
This commit is contained in:
67
seq/src/widgets/vu_meter.rs
Normal file
67
seq/src/widgets/vu_meter.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::Color;
|
||||
use ratatui::widgets::Widget;
|
||||
|
||||
pub struct VuMeter {
|
||||
left: f32,
|
||||
right: f32,
|
||||
}
|
||||
|
||||
impl VuMeter {
|
||||
pub fn new(left: f32, right: f32) -> Self {
|
||||
Self { left, right }
|
||||
}
|
||||
|
||||
fn level_to_color(level: f32) -> Color {
|
||||
if level > 0.9 {
|
||||
Color::Red
|
||||
} else if level > 0.7 {
|
||||
Color::Yellow
|
||||
} else {
|
||||
Color::Green
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for VuMeter {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
if area.width < 2 || area.height == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let height = area.height as usize;
|
||||
let left_col = area.x;
|
||||
let right_col = area.x + area.width - 1;
|
||||
|
||||
let left_level = (self.left.clamp(0.0, 1.0) * height as f32) as usize;
|
||||
let right_level = (self.right.clamp(0.0, 1.0) * height as f32) as usize;
|
||||
|
||||
for row in 0..height {
|
||||
let y = area.y + area.height - 1 - row as u16;
|
||||
let level_at_row = (row as f32 + 0.5) / height as f32;
|
||||
let color = Self::level_to_color(level_at_row);
|
||||
|
||||
if row < left_level {
|
||||
buf[(left_col, y)].set_char('█').set_fg(color);
|
||||
} else {
|
||||
buf[(left_col, y)].set_char('░').set_fg(Color::DarkGray);
|
||||
}
|
||||
|
||||
if row < right_level {
|
||||
buf[(right_col, y)].set_char('█').set_fg(color);
|
||||
} else {
|
||||
buf[(right_col, y)].set_char('░').set_fg(Color::DarkGray);
|
||||
}
|
||||
}
|
||||
|
||||
if area.width > 2 {
|
||||
for row in 0..height {
|
||||
let y = area.y + row as u16;
|
||||
for x in (area.x + 1)..(area.x + area.width - 1) {
|
||||
buf[(x, y)].set_char(' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user