use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Widget;
use crate::domain::forms::{Checkbox, RadioGroup, TextInput};
impl Widget for &TextInput {
fn render(self, area: Rect, buf: &mut Buffer) {
if area.width == 0 || area.height == 0 {
return;
}
let value = &self.value;
let cursor = self.cursor;
let max_w = area.width as usize;
let (start, _) = if value.len() > max_w && cursor >= max_w {
(cursor - max_w + 1, cursor)
} else {
(0, value.len().min(max_w))
};
let end = (start + max_w).min(value.len());
let visible = &value[start..end];
let cursor_in_visible = cursor.saturating_sub(start);
let style = Style::default().fg(Color::Reset);
let placeholder_style = Style::default().fg(Color::DarkGray);
let mut spans: Vec<Span> = Vec::with_capacity(visible.len() + 1);
if value.is_empty() {
if let Some(p) = self.placeholder.as_deref() {
spans.push(Span::styled(p, placeholder_style));
}
spans.push(Span::styled(" ", style));
} else {
for c in visible.chars() {
spans.push(Span::styled(c.to_string(), style));
}
}
if value.is_empty() && self.placeholder.is_none() {
spans.push(Span::styled(" ", style));
} else if !value.is_empty() {
spans.push(Span::styled(" ", style));
}
let line = Line::from(spans);
buf.set_line(area.x, area.y, &line, area.width);
let cursor_x = if value.is_empty() {
0
} else {
u16::try_from(cursor_in_visible).unwrap_or(u16::MAX)
};
if area.width > cursor_x {
let cell = &mut buf[(area.x + cursor_x, area.y)];
cell.set_style(Style::default().bg(Color::White).fg(Color::Black));
}
}
}
impl Widget for &Checkbox {
fn render(self, area: Rect, buf: &mut Buffer) {
if area.width == 0 || area.height == 0 {
return;
}
let mark = if self.checked { "[X]" } else { "[ ]" };
let style = if self.checked {
Style::default().fg(Color::Green)
} else {
Style::default().fg(Color::DarkGray)
};
let line = Line::from(vec![
Span::styled(mark, style),
Span::raw(" "),
Span::raw(self.label.clone()),
]);
buf.set_line(area.x, area.y, &line, area.width);
}
}
impl Widget for &RadioGroup {
fn render(self, area: Rect, buf: &mut Buffer) {
if self.options.is_empty() || area.width == 0 || area.height == 0 {
return;
}
for (i, label) in self.options.iter().enumerate() {
let y = area
.y
.saturating_add(u16::try_from(i).unwrap_or(u16::MAX));
if y >= area.y + area.height {
break;
}
let is_selected = i == self.selected;
let mark = if is_selected { "(•)" } else { "( )" };
let style = if is_selected {
Style::default().fg(Color::Cyan)
} else {
Style::default().fg(Color::DarkGray)
};
let line = Line::from(vec![
Span::styled(mark, style),
Span::raw(" "),
Span::raw(label.clone()),
]);
buf.set_line(area.x, y, &line, area.width);
}
}
}