first textarea implementation

This commit is contained in:
Priec
2025-08-17 11:01:38 +02:00
parent 215be3cf09
commit 60cb45dcca
7 changed files with 1056 additions and 0 deletions

View File

@@ -63,3 +63,11 @@ pub use canvas::gui::render_canvas_default;
#[cfg(all(feature = "gui", feature = "suggestions"))]
pub use suggestions::gui::render_suggestions_dropdown;
// First-class textarea module and exports
#[cfg(feature = "textarea")]
pub mod textarea;
#[cfg(feature = "textarea")]
pub use textarea::{TextArea, TextAreaProvider, TextAreaState, TextAreaEditor};

View File

@@ -0,0 +1,14 @@
// src/textarea/mod.rs
// Module routing and re-exports only. No logic here.
pub mod provider;
pub mod state;
#[cfg(feature = "gui")]
pub mod widget;
pub use provider::TextAreaProvider;
pub use state::{TextAreaEditor, TextAreaState};
#[cfg(feature = "gui")]
pub use widget::TextArea;

View File

@@ -0,0 +1,113 @@
// src/textarea/provider.rs
use crate::DataProvider;
#[derive(Debug, Clone)]
pub struct TextAreaProvider {
lines: Vec<String>,
name: String,
}
impl Default for TextAreaProvider {
fn default() -> Self {
Self {
lines: vec![String::new()],
name: "Text".to_string(),
}
}
}
impl TextAreaProvider {
pub fn from_text<S: Into<String>>(text: S) -> Self {
let text = text.into();
let mut lines: Vec<String> =
text.split('\n').map(|s| s.to_string()).collect();
if lines.is_empty() {
lines.push(String::new());
}
Self {
lines,
name: "Text".to_string(),
}
}
pub fn to_text(&self) -> String {
self.lines.join("\n")
}
pub fn set_text<S: Into<String>>(&mut self, text: S) {
let text = text.into();
self.lines = text.split('\n').map(|s| s.to_string()).collect();
if self.lines.is_empty() {
self.lines.push(String::new());
}
}
pub fn line_count(&self) -> usize {
self.lines.len()
}
#[inline]
fn char_to_byte_index(s: &str, char_idx: usize) -> usize {
s.char_indices()
.nth(char_idx)
.map(|(i, _)| i)
.unwrap_or_else(|| s.len())
}
pub fn split_line_at(&mut self, line_idx: usize, at_char: usize) -> usize {
if line_idx >= self.lines.len() {
return self.lines.len().saturating_sub(1);
}
let line = &mut self.lines[line_idx];
let byte_idx = Self::char_to_byte_index(line, at_char);
let right = line[byte_idx..].to_string();
line.truncate(byte_idx);
let insert_at = line_idx + 1;
self.lines.insert(insert_at, right);
insert_at
}
pub fn join_with_next(&mut self, line_idx: usize) -> Option<usize> {
if line_idx + 1 >= self.lines.len() {
return None;
}
let left_len = self.lines[line_idx].chars().count();
let right = self.lines.remove(line_idx + 1);
self.lines[line_idx].push_str(&right);
Some(left_len)
}
pub fn join_with_prev(
&mut self,
line_idx: usize,
) -> Option<(usize, usize)> {
if line_idx == 0 || line_idx >= self.lines.len() {
return None;
}
let prev_idx = line_idx - 1;
let prev_len = self.lines[prev_idx].chars().count();
let curr = self.lines.remove(line_idx);
self.lines[prev_idx].push_str(&curr);
Some((prev_idx, prev_len))
}
}
impl DataProvider for TextAreaProvider {
fn field_count(&self) -> usize {
self.lines.len()
}
fn field_name(&self, _index: usize) -> &str {
&self.name
}
fn field_value(&self, index: usize) -> &str {
self.lines.get(index).map(|s| s.as_str()).unwrap_or("")
}
fn set_field_value(&mut self, index: usize, value: String) {
if index < self.lines.len() {
self.lines[index] = value;
}
}
}

View File

@@ -0,0 +1,224 @@
// src/textarea/state.rs
use crate::editor::FormEditor;
use crate::textarea::provider::TextAreaProvider;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
#[cfg(feature = "gui")]
use ratatui::{layout::Rect, widgets::Block};
#[cfg(feature = "gui")]
use unicode_width::UnicodeWidthChar;
pub type TextAreaEditor = FormEditor<TextAreaProvider>;
pub struct TextAreaState {
pub(crate) editor: TextAreaEditor,
pub(crate) scroll_y: u16,
pub(crate) wrap: bool,
pub(crate) placeholder: Option<String>,
}
impl Default for TextAreaState {
fn default() -> Self {
Self {
editor: FormEditor::new(TextAreaProvider::default()),
scroll_y: 0,
wrap: false,
placeholder: None,
}
}
}
impl TextAreaState {
pub fn from_text<S: Into<String>>(text: S) -> Self {
let provider = TextAreaProvider::from_text(text);
Self {
editor: FormEditor::new(provider),
scroll_y: 0,
wrap: false,
placeholder: None,
}
}
pub fn text(&self) -> String {
self.editor.data_provider().to_text()
}
pub fn set_text<S: Into<String>>(&mut self, text: S) {
self.editor.data_provider_mut().set_text(text);
// Reset to first line and col 0
self.editor.ui_state.current_field = 0;
self.editor.ui_state.cursor_pos = 0;
self.editor.ui_state.ideal_cursor_column = 0;
}
pub fn set_wrap(&mut self, wrap: bool) {
self.wrap = wrap;
}
pub fn set_placeholder<S: Into<String>>(&mut self, s: S) {
self.placeholder = Some(s.into());
}
// Editing primitives specific to multi-line buffer
pub fn insert_newline(&mut self) {
let line_idx = self.editor.current_field();
let col = self.editor.cursor_position();
let new_idx = self
.editor
.data_provider_mut()
.split_line_at(line_idx, col);
let _ = self.editor.transition_to_field(new_idx);
self.editor.move_line_start();
self.editor.enter_edit_mode();
}
pub fn backspace(&mut self) {
let col = self.editor.cursor_position();
if col > 0 {
let _ = self.editor.delete_backward();
return;
}
let line_idx = self.editor.current_field();
if line_idx == 0 {
return;
}
if let Some((prev_idx, new_col)) = self
.editor
.data_provider_mut()
.join_with_prev(line_idx)
{
let _ = self.editor.transition_to_field(prev_idx);
self.editor.set_cursor_position(new_col);
self.editor.enter_edit_mode();
}
}
pub fn delete_forward_or_join(&mut self) {
let line_idx = self.editor.current_field();
let line_len = self.editor.current_text().chars().count();
let col = self.editor.cursor_position();
if col < line_len {
let _ = self.editor.delete_forward();
return;
}
if let Some(new_col) = self
.editor
.data_provider_mut()
.join_with_next(line_idx)
{
self.editor.set_cursor_position(new_col);
self.editor.enter_edit_mode();
}
}
// Drive the editor from key events
pub fn input(&mut self, key: KeyEvent) {
if key.kind != KeyEventKind::Press {
return;
}
match (key.code, key.modifiers) {
(KeyCode::Enter, _) => self.insert_newline(),
(KeyCode::Backspace, _) => self.backspace(),
(KeyCode::Delete, _) => self.delete_forward_or_join(),
(KeyCode::Left, _) => {
let _ = self.editor.move_left();
}
(KeyCode::Right, _) => {
let _ = self.editor.move_right();
}
(KeyCode::Up, _) => {
let _ = self.editor.move_up();
}
(KeyCode::Down, _) => {
let _ = self.editor.move_down();
}
(KeyCode::Home, _)
| (KeyCode::Char('a'), KeyModifiers::CONTROL) => {
self.editor.move_line_start();
}
(KeyCode::End, _)
| (KeyCode::Char('e'), KeyModifiers::CONTROL) => {
self.editor.move_line_end();
}
// Optional: word motions
(KeyCode::Char('b'), KeyModifiers::ALT) => {
self.editor.move_word_prev();
}
(KeyCode::Char('f'), KeyModifiers::ALT) => {
self.editor.move_word_next();
}
(KeyCode::Char('e'), KeyModifiers::ALT) => {
self.editor.move_word_end();
}
// Insert printable characters
(KeyCode::Char(c), m) if m.is_empty() => {
self.editor.enter_edit_mode();
let _ = self.editor.insert_char(c);
}
// Tab: insert 4 spaces (simple default)
(KeyCode::Tab, _) => {
self.editor.enter_edit_mode();
for _ in 0..4 {
let _ = self.editor.insert_char(' ');
}
}
_ => {}
}
}
// Cursor helpers for GUI
#[cfg(feature = "gui")]
pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
let inner = if let Some(b) = block { b.inner(area) } else { area };
let line_idx = self.editor.current_field() as u16;
let y = inner.y + line_idx.saturating_sub(self.scroll_y);
let current_line = self.editor.current_text();
let col = self.editor.display_cursor_position();
let mut x_off: u16 = 0;
for (i, ch) in current_line.chars().enumerate() {
if i >= col {
break;
}
x_off = x_off.saturating_add(
UnicodeWidthChar::width(ch).unwrap_or(0) as u16,
);
}
let x = inner.x.saturating_add(x_off);
(x, y)
}
#[cfg(feature = "gui")]
pub(crate) fn ensure_visible(
&mut self,
area: Rect,
block: Option<&Block<'_>>,
) {
let inner = if let Some(b) = block { b.inner(area) } else { area };
if inner.height == 0 {
return;
}
let line_idx = self.editor.current_field() as u16;
if line_idx < self.scroll_y {
self.scroll_y = line_idx;
} else if line_idx >= self.scroll_y + inner.height {
self.scroll_y = line_idx.saturating_sub(inner.height - 1);
}
}
}

View File

@@ -0,0 +1,106 @@
// src/textarea/widget.rs
#[cfg(feature = "gui")]
use ratatui::{
buffer::Buffer,
layout::{Alignment, Rect},
style::Style,
text::{Line, Span},
widgets::{
Block, BorderType, Borders, Paragraph, StatefulWidget, Widget, Wrap,
},
};
#[cfg(feature = "gui")]
use crate::data_provider::DataProvider; // bring trait into scope
#[cfg(feature = "gui")]
use crate::textarea::state::TextAreaState;
#[cfg(feature = "gui")]
#[derive(Debug, Clone)]
pub struct TextArea<'a> {
pub(crate) block: Option<Block<'a>>,
pub(crate) style: Style,
pub(crate) border_type: BorderType,
}
#[cfg(feature = "gui")]
impl<'a> Default for TextArea<'a> {
fn default() -> Self {
Self {
block: Some(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded),
),
style: Style::default(),
border_type: BorderType::Rounded,
}
}
}
#[cfg(feature = "gui")]
impl<'a> TextArea<'a> {
pub fn block(mut self, block: Block<'a>) -> Self {
self.block = Some(block);
self
}
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn border_type(mut self, ty: BorderType) -> Self {
self.border_type = ty;
if let Some(b) = &mut self.block {
*b = b.clone().border_type(ty);
}
self
}
}
#[cfg(feature = "gui")]
impl<'a> StatefulWidget for TextArea<'a> {
type State = TextAreaState;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
state.ensure_visible(area, self.block.as_ref());
let inner = if let Some(b) = &self.block {
b.clone().render(area, buf);
b.inner(area)
} else {
area
};
let total = state.editor.data_provider().line_count();
let start = state.scroll_y as usize;
let end = start
.saturating_add(inner.height as usize)
.min(total);
let mut display_lines: Vec<Line> = Vec::with_capacity(end - start);
if start >= end {
if let Some(ph) = &state.placeholder {
display_lines.push(Line::from(Span::raw(ph.clone())));
}
} else {
for i in start..end {
let s = state.editor.data_provider().field_value(i);
display_lines.push(Line::from(Span::raw(s.to_string())));
}
}
let mut p = Paragraph::new(display_lines)
.alignment(Alignment::Left)
.style(self.style);
if state.wrap {
p = p.wrap(Wrap { trim: false });
}
p.render(inner, buf);
}
}