use ratatui::{
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::Paragraph,
Frame,
};
use crate::ui::theme;
#[derive(Debug, Default, Clone)]
pub struct InputState {
pub value: String,
pub cursor: usize,
pub focused: bool,
}
impl InputState {
pub fn new() -> Self {
Self::default()
}
pub fn with_value(value: impl Into<String>) -> Self {
let value = value.into();
let cursor = value.chars().count();
Self {
value,
cursor,
focused: false,
}
}
pub fn insert(&mut self, c: char) {
let byte_idx = self
.value
.char_indices()
.nth(self.cursor)
.map(|(i, _)| i)
.unwrap_or(self.value.len());
self.value.insert(byte_idx, c);
self.cursor += 1;
}
pub fn delete_back(&mut self) {
if self.cursor > 0 {
self.cursor -= 1;
let byte_idx = self
.value
.char_indices()
.nth(self.cursor)
.map(|(i, _)| i)
.unwrap_or(0);
let next_byte_idx = self
.value
.char_indices()
.nth(self.cursor + 1)
.map(|(i, _)| i)
.unwrap_or(self.value.len());
self.value.replace_range(byte_idx..next_byte_idx, "");
}
}
pub fn delete_forward(&mut self) {
let char_count = self.value.chars().count();
if self.cursor < char_count {
let byte_idx = self
.value
.char_indices()
.nth(self.cursor)
.map(|(i, _)| i)
.unwrap_or(0);
let next_byte_idx = self
.value
.char_indices()
.nth(self.cursor + 1)
.map(|(i, _)| i)
.unwrap_or(self.value.len());
self.value.replace_range(byte_idx..next_byte_idx, "");
}
}
pub fn move_left(&mut self) {
self.cursor = self.cursor.saturating_sub(1);
}
pub fn move_right(&mut self) {
let char_count = self.value.chars().count();
if self.cursor < char_count {
self.cursor += 1;
}
}
pub fn move_start(&mut self) {
self.cursor = 0;
}
pub fn move_end(&mut self) {
self.cursor = self.value.chars().count();
}
pub fn clear(&mut self) {
self.value.clear();
self.cursor = 0;
}
pub fn set_value(&mut self, value: impl Into<String>) {
self.value = value.into();
self.cursor = self.value.chars().count();
}
}
#[derive(Debug, Clone, Default)]
pub struct InputStyle {
pub placeholder: Option<String>,
pub show_cursor: bool,
pub cursor_char: char,
}
impl InputStyle {
pub fn new() -> Self {
Self {
placeholder: None,
show_cursor: true,
cursor_char: '▍',
}
}
pub fn placeholder(mut self, text: impl Into<String>) -> Self {
self.placeholder = Some(text.into());
self
}
pub fn cursor(mut self, show: bool) -> Self {
self.show_cursor = show;
self
}
}
pub struct Input<'a> {
style: InputStyle,
label: Option<&'a str>,
is_active: bool,
}
impl<'a> Input<'a> {
pub fn new() -> Self {
Self {
style: InputStyle::new(),
label: None,
is_active: false,
}
}
pub fn style(mut self, style: InputStyle) -> Self {
self.style = style;
self
}
pub fn label(mut self, label: &'a str) -> Self {
self.label = Some(label);
self
}
pub fn active(mut self, active: bool) -> Self {
self.is_active = active;
self
}
pub fn placeholder(mut self, text: impl Into<String>) -> Self {
self.style.placeholder = Some(text.into());
self
}
pub fn render(self, f: &mut Frame, area: Rect, state: &InputState) {
let text_style = if self.is_active && state.focused {
Style::default().fg(theme::TEXT_PRIMARY())
} else {
Style::default().fg(theme::TEXT_SECONDARY())
};
let content = if state.value.is_empty() {
if let Some(ref placeholder) = self.style.placeholder {
Line::from(Span::styled(
placeholder.clone(),
Style::default()
.fg(theme::TEXT_DIM())
.add_modifier(Modifier::ITALIC),
))
} else if self.style.show_cursor && state.focused {
Line::from(Span::styled(
self.style.cursor_char.to_string(),
Style::default().fg(theme::ACCENT()),
))
} else {
Line::from("")
}
} else if self.style.show_cursor && state.focused {
let mut spans = Vec::new();
let chars: Vec<char> = state.value.chars().collect();
if state.cursor > 0 {
let before: String = chars[..state.cursor].iter().collect();
spans.push(Span::styled(before, text_style));
}
spans.push(Span::styled(
self.style.cursor_char.to_string(),
Style::default()
.fg(theme::ACCENT())
.add_modifier(Modifier::SLOW_BLINK),
));
if state.cursor < chars.len() {
let after: String = chars[state.cursor..].iter().collect();
spans.push(Span::styled(after, text_style));
}
Line::from(spans)
} else {
Line::from(Span::styled(state.value.clone(), text_style))
};
let widget = if let Some(label) = self.label {
let label_span = Span::styled(
format!("{}: ", label),
Style::default()
.fg(theme::TEXT_SECONDARY())
.add_modifier(Modifier::BOLD),
);
let mut spans = vec![label_span];
spans.extend(content.spans);
Paragraph::new(Line::from(spans))
} else {
Paragraph::new(content)
};
f.render_widget(widget, area);
}
}
impl Default for Input<'_> {
fn default() -> Self {
Self::new()
}
}