Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b2f021509 | ||
|
|
5f1bdfefca | ||
|
|
3273a43e20 | ||
|
|
61e439a1d4 | ||
|
|
03808a8b3b | ||
|
|
57aa0ed8e3 | ||
|
|
5efee3f044 | ||
|
|
6588f310f2 |
16
canvas/aider.md
Normal file
16
canvas/aider.md
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# Aider Instructions
|
||||||
|
|
||||||
|
## General Rules
|
||||||
|
- Only modify files that I explicitly add with `/add`.
|
||||||
|
- If a prompt mentions multiple files, **ignore all files except the ones I have added**.
|
||||||
|
- Do not create, edit, or delete any files unless they are explicitly added.
|
||||||
|
- Keep all other files exactly as they are, even if the prompt suggests changes.
|
||||||
|
- Never move logic into or out of files that are not explicitly added.
|
||||||
|
- If a prompt suggests changes to multiple files, apply **only the subset of changes** that belong to the added file(s).
|
||||||
|
- If a change requires touching other files, ignore them, if they were not manually added.
|
||||||
|
|
||||||
|
## Coding Style
|
||||||
|
- Follow Rust 2021 edition idioms.
|
||||||
|
- No logic in `mod.rs` files (only exports/routing).
|
||||||
|
- Always update or create tests **only if the test file is explicitly added**.
|
||||||
|
- Do not think, only apply changes from the prompt
|
||||||
@@ -243,6 +243,26 @@ fn handle_key_press(
|
|||||||
(KeyCode::Delete, _) => editor.delete_char_forward(),
|
(KeyCode::Delete, _) => editor.delete_char_forward(),
|
||||||
(KeyCode::Backspace, _) => editor.delete_char_backward(),
|
(KeyCode::Backspace, _) => editor.delete_char_backward(),
|
||||||
|
|
||||||
|
(KeyCode::F(1), _) => {
|
||||||
|
// Switch to indicator mode
|
||||||
|
editor.textarea.use_overflow_indicator('$');
|
||||||
|
editor.set_debug_message("Overflow: indicator '$' (wrap OFF)".to_string());
|
||||||
|
}
|
||||||
|
(KeyCode::F(2), _) => {
|
||||||
|
// Switch to wrap mode
|
||||||
|
editor.textarea.use_wrap();
|
||||||
|
editor.set_debug_message("Overflow: wrap ON".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
(KeyCode::F(3), _) => {
|
||||||
|
editor.textarea.set_wrap_indent_cols(3);
|
||||||
|
editor.set_debug_message("Wrap indent: 3 columns".to_string());
|
||||||
|
}
|
||||||
|
(KeyCode::F(4), _) => {
|
||||||
|
editor.textarea.set_wrap_indent_cols(0);
|
||||||
|
editor.set_debug_message("Wrap indent: 0 columns".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
// Debug/info
|
// Debug/info
|
||||||
(KeyCode::Char('?'), _) => {
|
(KeyCode::Char('?'), _) => {
|
||||||
editor.set_debug_message(format!(
|
editor.set_debug_message(format!(
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ Press ? for help, F1/F2 for manual cursor control demo.";
|
|||||||
|
|
||||||
let mut textarea = TextAreaState::from_text(initial_text);
|
let mut textarea = TextAreaState::from_text(initial_text);
|
||||||
textarea.set_placeholder("Start typing...");
|
textarea.set_placeholder("Start typing...");
|
||||||
|
textarea.use_wrap();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
textarea,
|
textarea,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// src/canvas/actions/types.rs
|
// src/canvas/actions/types.rs
|
||||||
|
|
||||||
/// All available canvas actions
|
/// All available canvas actions
|
||||||
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum CanvasAction {
|
pub enum CanvasAction {
|
||||||
// Movement actions
|
// Movement actions
|
||||||
@@ -42,6 +43,7 @@ pub enum CanvasAction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Result type for canvas actions
|
/// Result type for canvas actions
|
||||||
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ActionResult {
|
pub enum ActionResult {
|
||||||
Success,
|
Success,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use ratatui::{
|
|||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{Block, Borders, BorderType, Paragraph},
|
widgets::{Block, Borders, BorderType, Paragraph, Wrap},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -20,8 +20,177 @@ use unicode_width::UnicodeWidthChar;
|
|||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
/// Render ONLY the canvas form fields - no suggestions rendering here
|
#[cfg(feature = "gui")]
|
||||||
/// Updated to work with FormEditor instead of CanvasState trait
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum OverflowMode {
|
||||||
|
Indicator(char), // default '$'
|
||||||
|
Wrap,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct CanvasDisplayOptions {
|
||||||
|
pub overflow: OverflowMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
impl Default for CanvasDisplayOptions {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
overflow: OverflowMode::Indicator('$'),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Utility: measure display width of a string
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn display_width(s: &str) -> u16 {
|
||||||
|
s.chars()
|
||||||
|
.map(|c| UnicodeWidthChar::width(c).unwrap_or(0) as u16)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Utility: clip a string to fit width, append indicator if overflow
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn clip_with_indicator_line<'a>(s: &'a str, width: u16, indicator: char) -> Line<'a> {
|
||||||
|
if width == 0 {
|
||||||
|
return Line::from("");
|
||||||
|
}
|
||||||
|
if display_width(s) <= width {
|
||||||
|
return Line::from(Span::raw(s));
|
||||||
|
}
|
||||||
|
let budget = width.saturating_sub(1);
|
||||||
|
let mut out = String::new();
|
||||||
|
let mut used: u16 = 0;
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
if used + w > budget {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
out.push(ch);
|
||||||
|
used = used.saturating_add(w);
|
||||||
|
}
|
||||||
|
Line::from(vec![Span::raw(out), Span::raw(indicator.to_string())])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
const RIGHT_PAD: u16 = 3;
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
||||||
|
if max_cols == 0 {
|
||||||
|
return String::new();
|
||||||
|
}
|
||||||
|
let mut cols: u16 = 0;
|
||||||
|
let mut out = String::new();
|
||||||
|
let mut taken: u16 = 0;
|
||||||
|
let mut started = false;
|
||||||
|
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
let next = cols.saturating_add(w);
|
||||||
|
|
||||||
|
if !started {
|
||||||
|
if next <= start_cols {
|
||||||
|
cols = next;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
started = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if taken.saturating_add(w) > max_cols {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
out.push(ch);
|
||||||
|
taken = taken.saturating_add(w);
|
||||||
|
cols = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn compute_h_scroll_with_padding(cursor_cols: u16, width: u16) -> (u16, u16) {
|
||||||
|
let mut h = 0u16;
|
||||||
|
for _ in 0..2 {
|
||||||
|
let left_cols = if h > 0 { 1 } else { 0 };
|
||||||
|
let max_x_visible = width.saturating_sub(1 + RIGHT_PAD + left_cols);
|
||||||
|
let needed = cursor_cols.saturating_sub(max_x_visible);
|
||||||
|
if needed <= h {
|
||||||
|
return (h, left_cols);
|
||||||
|
}
|
||||||
|
h = needed;
|
||||||
|
}
|
||||||
|
let left_cols = if h > 0 { 1 } else { 0 };
|
||||||
|
(h, left_cols)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn render_active_line_with_indicator<T: CanvasTheme>(
|
||||||
|
typed_text: &str,
|
||||||
|
completion: Option<&str>,
|
||||||
|
width: u16,
|
||||||
|
indicator: char,
|
||||||
|
cursor_chars: usize,
|
||||||
|
theme: &T,
|
||||||
|
) -> (Line<'static>, u16, u16) {
|
||||||
|
if width == 0 {
|
||||||
|
return (Line::from(""), 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cursor display column
|
||||||
|
let mut cursor_cols: u16 = 0;
|
||||||
|
for (i, ch) in typed_text.chars().enumerate() {
|
||||||
|
if i >= cursor_chars {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cursor_cols = cursor_cols
|
||||||
|
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (h_scroll, left_cols) = compute_h_scroll_with_padding(cursor_cols, width);
|
||||||
|
|
||||||
|
let total_cols = display_width(typed_text);
|
||||||
|
let content_budget = width.saturating_sub(left_cols);
|
||||||
|
let show_right = total_cols.saturating_sub(h_scroll) > content_budget;
|
||||||
|
let right_cols: u16 = if show_right { 1 } else { 0 };
|
||||||
|
|
||||||
|
let visible_cols = width.saturating_sub(left_cols + right_cols);
|
||||||
|
let visible_typed = slice_by_display_cols(typed_text, h_scroll, visible_cols);
|
||||||
|
|
||||||
|
let used_typed_cols = display_width(&visible_typed);
|
||||||
|
let mut remaining_cols = visible_cols.saturating_sub(used_typed_cols);
|
||||||
|
let mut visible_completion = String::new();
|
||||||
|
|
||||||
|
if let Some(comp) = completion {
|
||||||
|
if !comp.is_empty() && remaining_cols > 0 {
|
||||||
|
visible_completion = slice_by_display_cols(comp, 0, remaining_cols);
|
||||||
|
remaining_cols = remaining_cols.saturating_sub(display_width(&visible_completion));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut spans: Vec<Span> = Vec::with_capacity(3);
|
||||||
|
if left_cols == 1 {
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
spans.push(Span::styled(
|
||||||
|
visible_typed,
|
||||||
|
Style::default().fg(theme.fg()),
|
||||||
|
));
|
||||||
|
if !visible_completion.is_empty() {
|
||||||
|
spans.push(Span::styled(
|
||||||
|
visible_completion,
|
||||||
|
Style::default().fg(theme.suggestion_gray()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if show_right {
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
(Line::from(spans), h_scroll, left_cols)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
@@ -29,32 +198,63 @@ pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
|||||||
editor: &FormEditor<D>,
|
editor: &FormEditor<D>,
|
||||||
theme: &T,
|
theme: &T,
|
||||||
) -> Option<Rect> {
|
) -> Option<Rect> {
|
||||||
// Convert SelectionState to HighlightState
|
let opts = CanvasDisplayOptions::default();
|
||||||
let highlight_state = convert_selection_to_highlight(editor.ui_state().selection_state());
|
render_canvas_with_options(f, area, editor, theme, opts)
|
||||||
render_canvas_with_highlight(f, area, editor, theme, &highlight_state)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render canvas with explicit highlight state (for advanced use)
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub fn render_canvas_with_highlight<T: CanvasTheme, D: DataProvider>(
|
pub fn render_canvas_with_options<T: CanvasTheme, D: DataProvider>(
|
||||||
|
f: &mut Frame,
|
||||||
|
area: Rect,
|
||||||
|
editor: &FormEditor<D>,
|
||||||
|
theme: &T,
|
||||||
|
opts: CanvasDisplayOptions,
|
||||||
|
) -> Option<Rect> {
|
||||||
|
let highlight_state =
|
||||||
|
convert_selection_to_highlight(editor.ui_state().selection_state());
|
||||||
|
|
||||||
|
#[cfg(feature = "suggestions")]
|
||||||
|
let active_completion = if editor.ui_state().is_suggestions_active()
|
||||||
|
&& editor.ui_state().suggestions.active_field
|
||||||
|
== Some(editor.ui_state().current_field())
|
||||||
|
{
|
||||||
|
editor.ui_state().suggestions.completion_text.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
#[cfg(not(feature = "suggestions"))]
|
||||||
|
let active_completion: Option<String> = None;
|
||||||
|
|
||||||
|
render_canvas_with_highlight_and_options(
|
||||||
|
f,
|
||||||
|
area,
|
||||||
|
editor,
|
||||||
|
theme,
|
||||||
|
&highlight_state,
|
||||||
|
active_completion,
|
||||||
|
opts,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
editor: &FormEditor<D>,
|
editor: &FormEditor<D>,
|
||||||
theme: &T,
|
theme: &T,
|
||||||
highlight_state: &HighlightState,
|
highlight_state: &HighlightState,
|
||||||
|
active_completion: Option<String>,
|
||||||
|
opts: CanvasDisplayOptions,
|
||||||
) -> Option<Rect> {
|
) -> Option<Rect> {
|
||||||
let ui_state = editor.ui_state();
|
let ui_state = editor.ui_state();
|
||||||
let data_provider = editor.data_provider();
|
let data_provider = editor.data_provider();
|
||||||
|
|
||||||
// Build field information
|
|
||||||
let field_count = data_provider.field_count();
|
let field_count = data_provider.field_count();
|
||||||
let mut fields: Vec<&str> = Vec::with_capacity(field_count);
|
let mut fields: Vec<&str> = Vec::with_capacity(field_count);
|
||||||
let mut inputs: Vec<String> = Vec::with_capacity(field_count);
|
let mut inputs: Vec<String> = Vec::with_capacity(field_count);
|
||||||
|
|
||||||
for i in 0..field_count {
|
for i in 0..field_count {
|
||||||
fields.push(data_provider.field_name(i));
|
fields.push(data_provider.field_name(i));
|
||||||
|
|
||||||
// Use editor-provided effective display text per field (Feature 4/mask aware)
|
|
||||||
#[cfg(feature = "validation")]
|
#[cfg(feature = "validation")]
|
||||||
{
|
{
|
||||||
inputs.push(editor.display_text_for_field(i));
|
inputs.push(editor.display_text_for_field(i));
|
||||||
@@ -68,20 +268,7 @@ pub fn render_canvas_with_highlight<T: CanvasTheme, D: DataProvider>(
|
|||||||
let current_field_idx = ui_state.current_field();
|
let current_field_idx = ui_state.current_field();
|
||||||
let is_edit_mode = matches!(ui_state.mode(), crate::canvas::modes::AppMode::Edit);
|
let is_edit_mode = matches!(ui_state.mode(), crate::canvas::modes::AppMode::Edit);
|
||||||
|
|
||||||
// Precompute completion for active field
|
render_canvas_fields_with_options(
|
||||||
#[cfg(feature = "suggestions")]
|
|
||||||
let active_completion = if ui_state.is_suggestions_active()
|
|
||||||
&& ui_state.suggestions.active_field == Some(current_field_idx)
|
|
||||||
{
|
|
||||||
ui_state.suggestions.completion_text.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(not(feature = "suggestions"))]
|
|
||||||
let active_completion: Option<String> = None;
|
|
||||||
|
|
||||||
render_canvas_fields(
|
|
||||||
f,
|
f,
|
||||||
area,
|
area,
|
||||||
&fields,
|
&fields,
|
||||||
@@ -90,52 +277,50 @@ pub fn render_canvas_with_highlight<T: CanvasTheme, D: DataProvider>(
|
|||||||
theme,
|
theme,
|
||||||
is_edit_mode,
|
is_edit_mode,
|
||||||
highlight_state,
|
highlight_state,
|
||||||
editor.display_cursor_position(), // Use display cursor position for masks
|
editor.display_cursor_position(),
|
||||||
false, // TODO: track unsaved changes in editor
|
false,
|
||||||
// Closures for getting display values and overrides
|
|
||||||
#[cfg(feature = "validation")]
|
#[cfg(feature = "validation")]
|
||||||
|field_idx| editor.display_text_for_field(field_idx),
|
|field_idx| editor.display_text_for_field(field_idx),
|
||||||
#[cfg(not(feature = "validation"))]
|
#[cfg(not(feature = "validation"))]
|
||||||
|field_idx| data_provider.field_value(field_idx).to_string(),
|
|field_idx| data_provider.field_value(field_idx).to_string(),
|
||||||
// Closure for checking display overrides
|
|
||||||
#[cfg(feature = "validation")]
|
#[cfg(feature = "validation")]
|
||||||
|field_idx| {
|
|field_idx| {
|
||||||
editor.ui_state().validation_state().get_field_config(field_idx)
|
editor
|
||||||
.map(|cfg| {
|
.ui_state()
|
||||||
let has_formatter = cfg.custom_formatter.is_some();
|
.validation_state()
|
||||||
let has_mask = cfg.display_mask.is_some();
|
.get_field_config(field_idx)
|
||||||
has_formatter || has_mask
|
.map(|cfg| cfg.custom_formatter.is_some() || cfg.display_mask.is_some())
|
||||||
})
|
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
},
|
},
|
||||||
#[cfg(not(feature = "validation"))]
|
#[cfg(not(feature = "validation"))]
|
||||||
|_field_idx| false,
|
|_field_idx| false,
|
||||||
// Closure for providing completion
|
active_completion,
|
||||||
|field_idx| {
|
opts,
|
||||||
if field_idx == current_field_idx {
|
|
||||||
active_completion.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert SelectionState to HighlightState for rendering
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn convert_selection_to_highlight(selection: &crate::canvas::state::SelectionState) -> HighlightState {
|
fn convert_selection_to_highlight(
|
||||||
|
selection: &crate::canvas::state::SelectionState,
|
||||||
|
) -> HighlightState {
|
||||||
use crate::canvas::state::SelectionState;
|
use crate::canvas::state::SelectionState;
|
||||||
|
|
||||||
match selection {
|
match selection {
|
||||||
SelectionState::None => HighlightState::Off,
|
SelectionState::None => HighlightState::Off,
|
||||||
SelectionState::Characterwise { anchor } => HighlightState::Characterwise { anchor: *anchor },
|
SelectionState::Characterwise { anchor } => {
|
||||||
SelectionState::Linewise { anchor_field } => HighlightState::Linewise { anchor_line: *anchor_field },
|
HighlightState::Characterwise { anchor: *anchor }
|
||||||
|
}
|
||||||
|
SelectionState::Linewise { anchor_field } => {
|
||||||
|
HighlightState::Linewise {
|
||||||
|
anchor_line: *anchor_field,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Core canvas field rendering
|
/// Core canvas field rendering with options
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn render_canvas_fields<T: CanvasTheme, F1, F2, F3>(
|
fn render_canvas_fields_with_options<T: CanvasTheme, F1, F2>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
fields: &[&str],
|
fields: &[&str],
|
||||||
@@ -148,20 +333,18 @@ fn render_canvas_fields<T: CanvasTheme, F1, F2, F3>(
|
|||||||
has_unsaved_changes: bool,
|
has_unsaved_changes: bool,
|
||||||
get_display_value: F1,
|
get_display_value: F1,
|
||||||
has_display_override: F2,
|
has_display_override: F2,
|
||||||
get_completion: F3,
|
active_completion: Option<String>,
|
||||||
|
opts: CanvasDisplayOptions,
|
||||||
) -> Option<Rect>
|
) -> Option<Rect>
|
||||||
where
|
where
|
||||||
F1: Fn(usize) -> String,
|
F1: Fn(usize) -> String,
|
||||||
F2: Fn(usize) -> bool,
|
F2: Fn(usize) -> bool,
|
||||||
F3: Fn(usize) -> Option<String>,
|
|
||||||
{
|
{
|
||||||
// Create layout
|
|
||||||
let columns = Layout::default()
|
let columns = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||||
.split(area);
|
.split(area);
|
||||||
|
|
||||||
// Border style based on state
|
|
||||||
let border_style = if has_unsaved_changes {
|
let border_style = if has_unsaved_changes {
|
||||||
Style::default().fg(theme.warning())
|
Style::default().fg(theme.warning())
|
||||||
} else if is_edit_mode {
|
} else if is_edit_mode {
|
||||||
@@ -170,7 +353,6 @@ where
|
|||||||
Style::default().fg(theme.secondary())
|
Style::default().fg(theme.secondary())
|
||||||
};
|
};
|
||||||
|
|
||||||
// Input container
|
|
||||||
let input_container = Block::default()
|
let input_container = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_type(BorderType::Rounded)
|
.border_type(BorderType::Rounded)
|
||||||
@@ -186,29 +368,111 @@ where
|
|||||||
|
|
||||||
f.render_widget(&input_container, input_block);
|
f.render_widget(&input_container, input_block);
|
||||||
|
|
||||||
// Input area layout
|
|
||||||
let input_area = input_container.inner(input_block);
|
let input_area = input_container.inner(input_block);
|
||||||
|
|
||||||
let input_rows = Layout::default()
|
let input_rows = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints(vec![Constraint::Length(1); fields.len()])
|
.constraints(vec![Constraint::Length(1); fields.len()])
|
||||||
.split(input_area);
|
.split(input_area);
|
||||||
|
|
||||||
// Render field labels
|
|
||||||
render_field_labels(f, columns[0], input_block, fields, theme);
|
render_field_labels(f, columns[0], input_block, fields, theme);
|
||||||
|
|
||||||
// Render field values and return active field rect
|
let mut active_field_input_rect = None;
|
||||||
render_field_values(
|
|
||||||
f,
|
for i in 0..inputs.len() {
|
||||||
input_rows.to_vec(),
|
let is_active = i == *current_field_idx;
|
||||||
inputs,
|
let typed_text = get_display_value(i);
|
||||||
|
let inner_width = input_rows[i].width;
|
||||||
|
|
||||||
|
// ---- BEGIN MODIFIED SECTION ----
|
||||||
|
let mut h_scroll_for_cursor: u16 = 0;
|
||||||
|
let mut left_offset_for_cursor: u16 = 0;
|
||||||
|
|
||||||
|
let line = match highlight_state {
|
||||||
|
// Selection highlighting active: always use highlighting, even for the active field
|
||||||
|
HighlightState::Characterwise { .. } | HighlightState::Linewise { .. } => {
|
||||||
|
apply_highlighting(
|
||||||
|
&typed_text,
|
||||||
|
i,
|
||||||
current_field_idx,
|
current_field_idx,
|
||||||
theme,
|
|
||||||
highlight_state,
|
|
||||||
current_cursor_pos,
|
current_cursor_pos,
|
||||||
get_display_value,
|
highlight_state,
|
||||||
has_display_override,
|
theme,
|
||||||
get_completion,
|
is_active,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// No selection highlighting
|
||||||
|
HighlightState::Off => match opts.overflow {
|
||||||
|
// Indicator mode: special-case the active field to preserve h-scroll + indicators
|
||||||
|
OverflowMode::Indicator(ind) => {
|
||||||
|
if is_active {
|
||||||
|
let (l, hs, left_cols) = render_active_line_with_indicator(
|
||||||
|
&typed_text,
|
||||||
|
active_completion.as_deref(),
|
||||||
|
inner_width,
|
||||||
|
ind,
|
||||||
|
current_cursor_pos,
|
||||||
|
theme,
|
||||||
|
);
|
||||||
|
h_scroll_for_cursor = hs;
|
||||||
|
left_offset_for_cursor = left_cols;
|
||||||
|
l
|
||||||
|
} else if display_width(&typed_text) <= inner_width {
|
||||||
|
Line::from(Span::raw(typed_text.clone()))
|
||||||
|
} else {
|
||||||
|
clip_with_indicator_line(&typed_text, inner_width, ind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap mode: keep active completion for active line
|
||||||
|
OverflowMode::Wrap => {
|
||||||
|
if is_active {
|
||||||
|
let mut spans: Vec<Span> = Vec::new();
|
||||||
|
spans.push(Span::styled(
|
||||||
|
typed_text.clone(),
|
||||||
|
Style::default().fg(theme.fg()),
|
||||||
|
));
|
||||||
|
if let Some(completion) = &active_completion {
|
||||||
|
if !completion.is_empty() {
|
||||||
|
spans.push(Span::styled(
|
||||||
|
completion.clone(),
|
||||||
|
Style::default().fg(theme.suggestion_gray()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Line::from(spans)
|
||||||
|
} else {
|
||||||
|
Line::from(Span::raw(typed_text.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// ---- END MODIFIED SECTION ----
|
||||||
|
|
||||||
|
let mut p = Paragraph::new(line).alignment(Alignment::Left);
|
||||||
|
|
||||||
|
if matches!(opts.overflow, OverflowMode::Wrap) {
|
||||||
|
p = p.wrap(Wrap { trim: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
f.render_widget(p, input_rows[i]);
|
||||||
|
|
||||||
|
if is_active {
|
||||||
|
active_field_input_rect = Some(input_rows[i]);
|
||||||
|
set_cursor_position_scrolled(
|
||||||
|
f,
|
||||||
|
input_rows[i],
|
||||||
|
&typed_text,
|
||||||
|
current_cursor_pos,
|
||||||
|
has_display_override(i),
|
||||||
|
h_scroll_for_cursor,
|
||||||
|
left_offset_for_cursor,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
active_field_input_rect
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render field labels
|
/// Render field labels
|
||||||
@@ -237,73 +501,6 @@ fn render_field_labels<T: CanvasTheme>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render field values with highlighting
|
|
||||||
#[cfg(feature = "gui")]
|
|
||||||
fn render_field_values<T: CanvasTheme, F1, F2, F3>(
|
|
||||||
f: &mut Frame,
|
|
||||||
input_rows: Vec<Rect>,
|
|
||||||
inputs: &[String],
|
|
||||||
current_field_idx: &usize,
|
|
||||||
theme: &T,
|
|
||||||
highlight_state: &HighlightState,
|
|
||||||
current_cursor_pos: usize,
|
|
||||||
get_display_value: F1,
|
|
||||||
has_display_override: F2,
|
|
||||||
get_completion: F3,
|
|
||||||
) -> Option<Rect>
|
|
||||||
where
|
|
||||||
F1: Fn(usize) -> String,
|
|
||||||
F2: Fn(usize) -> bool,
|
|
||||||
F3: Fn(usize) -> Option<String>,
|
|
||||||
{
|
|
||||||
let mut active_field_input_rect = None;
|
|
||||||
|
|
||||||
// FIX: Iterate over indices only since we never use the input values directly
|
|
||||||
for i in 0..inputs.len() {
|
|
||||||
let is_active = i == *current_field_idx;
|
|
||||||
let typed_text = get_display_value(i);
|
|
||||||
|
|
||||||
let line = if is_active {
|
|
||||||
// Compose typed + gray completion for the active field
|
|
||||||
let normal_style = Style::default().fg(theme.fg());
|
|
||||||
let gray_style = Style::default().fg(theme.suggestion_gray());
|
|
||||||
|
|
||||||
let mut spans: Vec<Span> = Vec::new();
|
|
||||||
spans.push(Span::styled(typed_text.clone(), normal_style));
|
|
||||||
|
|
||||||
if let Some(completion) = get_completion(i) {
|
|
||||||
if !completion.is_empty() {
|
|
||||||
spans.push(Span::styled(completion, gray_style));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Line::from(spans)
|
|
||||||
} else {
|
|
||||||
// Non-active fields: keep existing highlighting logic
|
|
||||||
apply_highlighting(
|
|
||||||
&typed_text,
|
|
||||||
i,
|
|
||||||
current_field_idx,
|
|
||||||
current_cursor_pos,
|
|
||||||
highlight_state,
|
|
||||||
theme,
|
|
||||||
is_active,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let input_display = Paragraph::new(line).alignment(Alignment::Left);
|
|
||||||
f.render_widget(input_display, input_rows[i]);
|
|
||||||
|
|
||||||
// Set cursor for active field at end of typed text (not after completion)
|
|
||||||
if is_active {
|
|
||||||
active_field_input_rect = Some(input_rows[i]);
|
|
||||||
set_cursor_position(f, input_rows[i], &typed_text, current_cursor_pos, has_display_override(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
active_field_input_rect
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply highlighting based on highlight state
|
/// Apply highlighting based on highlight state
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn apply_highlighting<'a, T: CanvasTheme>(
|
fn apply_highlighting<'a, T: CanvasTheme>(
|
||||||
@@ -319,21 +516,34 @@ fn apply_highlighting<'a, T: CanvasTheme>(
|
|||||||
|
|
||||||
match highlight_state {
|
match highlight_state {
|
||||||
HighlightState::Off => {
|
HighlightState::Off => {
|
||||||
Line::from(Span::styled(
|
Line::from(Span::styled(text, Style::default().fg(theme.fg())))
|
||||||
text,
|
|
||||||
Style::default().fg(theme.fg())
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
HighlightState::Characterwise { anchor } => {
|
HighlightState::Characterwise { anchor } => {
|
||||||
apply_characterwise_highlighting(text, text_len, field_index, current_field_idx, current_cursor_pos, anchor, theme, is_active)
|
apply_characterwise_highlighting(
|
||||||
|
text,
|
||||||
|
text_len,
|
||||||
|
field_index,
|
||||||
|
current_field_idx,
|
||||||
|
current_cursor_pos,
|
||||||
|
anchor,
|
||||||
|
theme,
|
||||||
|
is_active,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
HighlightState::Linewise { anchor_line } => {
|
HighlightState::Linewise { anchor_line } => {
|
||||||
apply_linewise_highlighting(text, field_index, current_field_idx, anchor_line, theme, is_active)
|
apply_linewise_highlighting(
|
||||||
|
text,
|
||||||
|
field_index,
|
||||||
|
current_field_idx,
|
||||||
|
anchor_line,
|
||||||
|
theme,
|
||||||
|
is_active,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply characterwise highlighting - PROPER VIM-LIKE VERSION
|
/// Apply characterwise highlighting (unchanged)
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
@@ -349,21 +559,20 @@ fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
|||||||
let start_field = min(anchor_field, *current_field_idx);
|
let start_field = min(anchor_field, *current_field_idx);
|
||||||
let end_field = max(anchor_field, *current_field_idx);
|
let end_field = max(anchor_field, *current_field_idx);
|
||||||
|
|
||||||
// Vim-like styling:
|
|
||||||
// - Selected text: contrasting color + background (like vim visual selection)
|
|
||||||
// - All other text: normal color (no special colors for active fields, etc.)
|
|
||||||
let highlight_style = Style::default()
|
let highlight_style = Style::default()
|
||||||
.fg(theme.highlight()) // ✅ Contrasting text color for selected text
|
.fg(theme.highlight())
|
||||||
.bg(theme.highlight_bg()) // ✅ Background for selected text
|
.bg(theme.highlight_bg())
|
||||||
.add_modifier(Modifier::BOLD);
|
.add_modifier(Modifier::BOLD);
|
||||||
|
|
||||||
let normal_style = Style::default().fg(theme.fg()); // ✅ Normal text color everywhere else
|
let normal_style = Style::default().fg(theme.fg());
|
||||||
|
|
||||||
if field_index >= start_field && field_index <= end_field {
|
if field_index >= start_field && field_index <= end_field {
|
||||||
if start_field == end_field {
|
if start_field == end_field {
|
||||||
// Single field selection
|
|
||||||
let (start_char, end_char) = if anchor_field == *current_field_idx {
|
let (start_char, end_char) = if anchor_field == *current_field_idx {
|
||||||
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
|
(
|
||||||
|
min(anchor_char, current_cursor_pos),
|
||||||
|
max(anchor_char, current_cursor_pos),
|
||||||
|
)
|
||||||
} else if anchor_field < *current_field_idx {
|
} else if anchor_field < *current_field_idx {
|
||||||
(anchor_char, current_cursor_pos)
|
(anchor_char, current_cursor_pos)
|
||||||
} else {
|
} else {
|
||||||
@@ -374,19 +583,19 @@ fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
|||||||
let clamped_end = end_char.min(text_len);
|
let clamped_end = end_char.min(text_len);
|
||||||
|
|
||||||
let before: String = text.chars().take(clamped_start).collect();
|
let before: String = text.chars().take(clamped_start).collect();
|
||||||
let highlighted: String = text.chars()
|
let highlighted: String = text
|
||||||
|
.chars()
|
||||||
.skip(clamped_start)
|
.skip(clamped_start)
|
||||||
.take(clamped_end.saturating_sub(clamped_start) + 1)
|
.take(clamped_end.saturating_sub(clamped_start) + 1)
|
||||||
.collect();
|
.collect();
|
||||||
let after: String = text.chars().skip(clamped_end + 1).collect();
|
let after: String = text.chars().skip(clamped_end + 1).collect();
|
||||||
|
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
Span::styled(before, normal_style), // Normal text color
|
Span::styled(before, normal_style),
|
||||||
Span::styled(highlighted, highlight_style), // Contrasting color + background
|
Span::styled(highlighted, highlight_style),
|
||||||
Span::styled(after, normal_style), // Normal text color
|
Span::styled(after, normal_style),
|
||||||
])
|
])
|
||||||
} else {
|
} else {
|
||||||
// Multi-field selection
|
|
||||||
if field_index == anchor_field {
|
if field_index == anchor_field {
|
||||||
if anchor_field < *current_field_idx {
|
if anchor_field < *current_field_idx {
|
||||||
let clamped_start = anchor_char.min(text_len);
|
let clamped_start = anchor_char.min(text_len);
|
||||||
@@ -428,17 +637,15 @@ fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Middle field: highlight entire field
|
|
||||||
Line::from(Span::styled(text, highlight_style))
|
Line::from(Span::styled(text, highlight_style))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Outside selection: always normal text color (no special active field color)
|
|
||||||
Line::from(Span::styled(text, normal_style))
|
Line::from(Span::styled(text, normal_style))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply linewise highlighting - PROPER VIM-LIKE VERSION
|
/// Apply linewise highlighting (unchanged)
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn apply_linewise_highlighting<'a, T: CanvasTheme>(
|
fn apply_linewise_highlighting<'a, T: CanvasTheme>(
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
@@ -451,35 +658,31 @@ fn apply_linewise_highlighting<'a, T: CanvasTheme>(
|
|||||||
let start_field = min(*anchor_line, *current_field_idx);
|
let start_field = min(*anchor_line, *current_field_idx);
|
||||||
let end_field = max(*anchor_line, *current_field_idx);
|
let end_field = max(*anchor_line, *current_field_idx);
|
||||||
|
|
||||||
// Vim-like styling:
|
|
||||||
// - Selected lines: contrasting text color + background
|
|
||||||
// - All other lines: normal text color (no special active field color)
|
|
||||||
let highlight_style = Style::default()
|
let highlight_style = Style::default()
|
||||||
.fg(theme.highlight()) // ✅ Contrasting text color for selected text
|
.fg(theme.highlight())
|
||||||
.bg(theme.highlight_bg()) // ✅ Background for selected text
|
.bg(theme.highlight_bg())
|
||||||
.add_modifier(Modifier::BOLD);
|
.add_modifier(Modifier::BOLD);
|
||||||
|
|
||||||
let normal_style = Style::default().fg(theme.fg()); // ✅ Normal text color everywhere else
|
let normal_style = Style::default().fg(theme.fg());
|
||||||
|
|
||||||
if field_index >= start_field && field_index <= end_field {
|
if field_index >= start_field && field_index <= end_field {
|
||||||
// Selected line: contrasting text color + background
|
|
||||||
Line::from(Span::styled(text, highlight_style))
|
Line::from(Span::styled(text, highlight_style))
|
||||||
} else {
|
} else {
|
||||||
// Normal line: normal text color (no special active field color)
|
|
||||||
Line::from(Span::styled(text, normal_style))
|
Line::from(Span::styled(text, normal_style))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set cursor position
|
/// Set cursor position (x clamp only; no Y offset with wrap in this version)
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn set_cursor_position(
|
fn set_cursor_position_scrolled(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
field_rect: Rect,
|
field_rect: Rect,
|
||||||
text: &str,
|
text: &str,
|
||||||
current_cursor_pos: usize,
|
current_cursor_pos: usize,
|
||||||
_has_display_override: bool,
|
_has_display_override: bool,
|
||||||
|
h_scroll: u16,
|
||||||
|
left_offset: u16,
|
||||||
) {
|
) {
|
||||||
// Sum display widths of the first current_cursor_pos characters
|
|
||||||
let mut cols: u16 = 0;
|
let mut cols: u16 = 0;
|
||||||
for (i, ch) in text.chars().enumerate() {
|
for (i, ch) in text.chars().enumerate() {
|
||||||
if i >= current_cursor_pos {
|
if i >= current_cursor_pos {
|
||||||
@@ -488,17 +691,19 @@ fn set_cursor_position(
|
|||||||
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
let cursor_x = field_rect.x.saturating_add(cols);
|
let mut visible_x = cols.saturating_sub(h_scroll).saturating_add(left_offset);
|
||||||
|
|
||||||
|
let limit = field_rect.width.saturating_sub(1 + RIGHT_PAD);
|
||||||
|
if visible_x > limit {
|
||||||
|
visible_x = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cursor_x = field_rect.x.saturating_add(visible_x);
|
||||||
let cursor_y = field_rect.y;
|
let cursor_y = field_rect.y;
|
||||||
|
f.set_cursor_position((cursor_x, cursor_y));
|
||||||
// Clamp to field bounds
|
|
||||||
let max_cursor_x = field_rect.x + field_rect.width.saturating_sub(1);
|
|
||||||
let safe_cursor_x = cursor_x.min(max_cursor_x);
|
|
||||||
|
|
||||||
f.set_cursor_position((safe_cursor_x, cursor_y));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set default theme if custom not specified
|
/// Default theme
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub fn render_canvas_default<D: DataProvider>(
|
pub fn render_canvas_default<D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ pub mod suggestions;
|
|||||||
#[cfg(feature = "validation")]
|
#[cfg(feature = "validation")]
|
||||||
pub mod validation;
|
pub mod validation;
|
||||||
|
|
||||||
|
// First-class textarea module and exports
|
||||||
|
#[cfg(feature = "textarea")]
|
||||||
|
pub mod textarea;
|
||||||
|
|
||||||
// Only include computed module if feature is enabled
|
// Only include computed module if feature is enabled
|
||||||
#[cfg(feature = "computed")]
|
#[cfg(feature = "computed")]
|
||||||
pub mod computed;
|
pub mod computed;
|
||||||
@@ -56,18 +60,17 @@ pub use computed::{ComputedProvider, ComputedContext, ComputedState};
|
|||||||
pub use canvas::theme::{CanvasTheme, DefaultCanvasTheme};
|
pub use canvas::theme::{CanvasTheme, DefaultCanvasTheme};
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub use canvas::gui::render_canvas;
|
pub use canvas::gui::{render_canvas, render_canvas_default};
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub use canvas::gui::render_canvas_default;
|
pub use canvas::gui::render_canvas_with_options;
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub use canvas::gui::{CanvasDisplayOptions, OverflowMode};
|
||||||
|
|
||||||
#[cfg(all(feature = "gui", feature = "suggestions"))]
|
#[cfg(all(feature = "gui", feature = "suggestions"))]
|
||||||
pub use suggestions::gui::render_suggestions_dropdown;
|
pub use suggestions::gui::render_suggestions_dropdown;
|
||||||
|
|
||||||
|
|
||||||
// First-class textarea module and exports
|
|
||||||
#[cfg(feature = "textarea")]
|
|
||||||
pub mod textarea;
|
|
||||||
|
|
||||||
#[cfg(feature = "textarea")]
|
#[cfg(feature = "textarea")]
|
||||||
pub use textarea::{TextArea, TextAreaProvider, TextAreaState, TextAreaEditor};
|
pub use textarea::{TextArea, TextAreaProvider, TextAreaState, TextAreaEditor};
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
// src/textarea/mod.rs
|
// src/textarea/mod.rs
|
||||||
// Module routing and re-exports only. No logic here.
|
|
||||||
|
|
||||||
pub mod provider;
|
pub mod provider;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|
||||||
@@ -8,7 +6,7 @@ pub mod state;
|
|||||||
pub mod widget;
|
pub mod widget;
|
||||||
|
|
||||||
pub use provider::TextAreaProvider;
|
pub use provider::TextAreaProvider;
|
||||||
pub use state::{TextAreaEditor, TextAreaState};
|
pub use state::{TextAreaEditor, TextAreaState, TextOverflowMode};
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub use widget::TextArea;
|
pub use widget::TextArea;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
// src/textarea/state.rs
|
// src/textarea/state.rs
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
|
|
||||||
use crate::editor::FormEditor;
|
use crate::editor::FormEditor;
|
||||||
use crate::textarea::provider::TextAreaProvider;
|
use crate::textarea::provider::TextAreaProvider;
|
||||||
|
use crate::data_provider::DataProvider;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use ratatui::{layout::Rect, widgets::Block};
|
use ratatui::{layout::Rect, widgets::Block};
|
||||||
@@ -13,13 +13,117 @@ use ratatui::{layout::Rect, widgets::Block};
|
|||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use unicode_width::UnicodeWidthChar;
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) const RIGHT_PAD: u16 = 3;
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) fn compute_h_scroll_with_padding(
|
||||||
|
cursor_cols: u16,
|
||||||
|
width: u16,
|
||||||
|
) -> (u16, u16) {
|
||||||
|
let mut h = 0u16;
|
||||||
|
for _ in 0..2 {
|
||||||
|
let left_cols = if h > 0 { 1 } else { 0 };
|
||||||
|
let max_x_visible = width.saturating_sub(1 + RIGHT_PAD + left_cols);
|
||||||
|
let needed = cursor_cols.saturating_sub(max_x_visible);
|
||||||
|
if needed <= h {
|
||||||
|
return (h, left_cols);
|
||||||
|
}
|
||||||
|
h = needed;
|
||||||
|
}
|
||||||
|
let left_cols = if h > 0 { 1 } else { 0 };
|
||||||
|
(h, left_cols)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn normalize_indent(width: u16, indent: u16) -> u16 {
|
||||||
|
indent.min(width.saturating_sub(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) fn count_wrapped_rows_indented(
|
||||||
|
s: &str,
|
||||||
|
width: u16,
|
||||||
|
indent: u16,
|
||||||
|
) -> u16 {
|
||||||
|
if width == 0 {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
let indent = normalize_indent(width, indent);
|
||||||
|
let cont_cap = width.saturating_sub(indent);
|
||||||
|
|
||||||
|
let mut rows: u16 = 1;
|
||||||
|
let mut used: u16 = 0;
|
||||||
|
let mut first = true;
|
||||||
|
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
let cap = if first { width } else { cont_cap };
|
||||||
|
|
||||||
|
if used > 0 && used.saturating_add(w) >= cap {
|
||||||
|
rows = rows.saturating_add(1);
|
||||||
|
first = false;
|
||||||
|
used = indent;
|
||||||
|
}
|
||||||
|
used = used.saturating_add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
rows
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn wrapped_rows_to_cursor_indented(
|
||||||
|
s: &str,
|
||||||
|
width: u16,
|
||||||
|
indent: u16,
|
||||||
|
cursor_chars: usize,
|
||||||
|
) -> (u16, u16) {
|
||||||
|
if width == 0 {
|
||||||
|
return (0, 0);
|
||||||
|
}
|
||||||
|
let indent = normalize_indent(width, indent);
|
||||||
|
let cont_cap = width.saturating_sub(indent);
|
||||||
|
|
||||||
|
let mut row: u16 = 0;
|
||||||
|
let mut used: u16 = 0;
|
||||||
|
let mut first = true;
|
||||||
|
|
||||||
|
for (i, ch) in s.chars().enumerate() {
|
||||||
|
if i >= cursor_chars {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
let cap = if first { width } else { cont_cap };
|
||||||
|
|
||||||
|
if used > 0 && used.saturating_add(w) >= cap {
|
||||||
|
row = row.saturating_add(1);
|
||||||
|
first = false;
|
||||||
|
used = indent;
|
||||||
|
}
|
||||||
|
used = used.saturating_add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
(row, used.min(width.saturating_sub(1)))
|
||||||
|
}
|
||||||
|
|
||||||
pub type TextAreaEditor = FormEditor<TextAreaProvider>;
|
pub type TextAreaEditor = FormEditor<TextAreaProvider>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum TextOverflowMode {
|
||||||
|
Indicator { ch: char },
|
||||||
|
Wrap,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct TextAreaState {
|
pub struct TextAreaState {
|
||||||
pub(crate) editor: TextAreaEditor,
|
pub(crate) editor: TextAreaEditor,
|
||||||
pub(crate) scroll_y: u16,
|
pub(crate) scroll_y: u16,
|
||||||
pub(crate) wrap: bool,
|
|
||||||
pub(crate) placeholder: Option<String>,
|
pub(crate) placeholder: Option<String>,
|
||||||
|
pub(crate) overflow_mode: TextOverflowMode,
|
||||||
|
pub(crate) h_scroll: u16,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) wrap_indent_cols: u16,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) edited_this_frame: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TextAreaState {
|
impl Default for TextAreaState {
|
||||||
@@ -27,13 +131,17 @@ impl Default for TextAreaState {
|
|||||||
Self {
|
Self {
|
||||||
editor: FormEditor::new(TextAreaProvider::default()),
|
editor: FormEditor::new(TextAreaProvider::default()),
|
||||||
scroll_y: 0,
|
scroll_y: 0,
|
||||||
wrap: false,
|
|
||||||
placeholder: None,
|
placeholder: None,
|
||||||
|
overflow_mode: TextOverflowMode::Indicator { ch: '$' },
|
||||||
|
h_scroll: 0,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
wrap_indent_cols: 0,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
edited_this_frame: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose the entire FormEditor API directly on TextAreaState
|
|
||||||
impl Deref for TextAreaState {
|
impl Deref for TextAreaState {
|
||||||
type Target = TextAreaEditor;
|
type Target = TextAreaEditor;
|
||||||
|
|
||||||
@@ -54,8 +162,13 @@ impl TextAreaState {
|
|||||||
Self {
|
Self {
|
||||||
editor: FormEditor::new(provider),
|
editor: FormEditor::new(provider),
|
||||||
scroll_y: 0,
|
scroll_y: 0,
|
||||||
wrap: false,
|
|
||||||
placeholder: None,
|
placeholder: None,
|
||||||
|
overflow_mode: TextOverflowMode::Indicator { ch: '$' },
|
||||||
|
h_scroll: 0,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
wrap_indent_cols: 0,
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
edited_this_frame: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,16 +183,30 @@ impl TextAreaState {
|
|||||||
self.editor.ui_state.ideal_cursor_column = 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) {
|
pub fn set_placeholder<S: Into<String>>(&mut self, s: S) {
|
||||||
self.placeholder = Some(s.into());
|
self.placeholder = Some(s.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Textarea-specific primitive: split at cursor
|
pub fn use_overflow_indicator(&mut self, ch: char) {
|
||||||
|
self.overflow_mode = TextOverflowMode::Indicator { ch };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_wrap(&mut self) {
|
||||||
|
self.overflow_mode = TextOverflowMode::Wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_wrap_indent_cols(&mut self, cols: u16) {
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.wrap_indent_cols = cols;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn insert_newline(&mut self) {
|
pub fn insert_newline(&mut self) {
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
let line_idx = self.current_field();
|
let line_idx = self.current_field();
|
||||||
let col = self.cursor_position();
|
let col = self.cursor_position();
|
||||||
|
|
||||||
@@ -93,10 +220,13 @@ impl TextAreaState {
|
|||||||
self.enter_edit_mode();
|
self.enter_edit_mode();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Textarea-specific primitive: backspace with line join at start-of-line
|
|
||||||
pub fn backspace(&mut self) {
|
pub fn backspace(&mut self) {
|
||||||
let col = self.cursor_position();
|
let col = self.cursor_position();
|
||||||
if col > 0 {
|
if col > 0 {
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
let _ = self.delete_backward();
|
let _ = self.delete_backward();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -106,67 +236,45 @@ impl TextAreaState {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((prev_idx, new_col)) = self
|
if let Some((prev_idx, new_col)) =
|
||||||
.editor
|
self.editor.data_provider_mut().join_with_prev(line_idx)
|
||||||
.data_provider_mut()
|
|
||||||
.join_with_prev(line_idx)
|
|
||||||
{
|
{
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
let _ = self.transition_to_field(prev_idx);
|
let _ = self.transition_to_field(prev_idx);
|
||||||
self.set_cursor_position(new_col);
|
self.set_cursor_position(new_col);
|
||||||
self.enter_edit_mode();
|
self.enter_edit_mode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Textarea-specific primitive: delete or join with next line at EOL
|
|
||||||
pub fn delete_forward_or_join(&mut self) {
|
pub fn delete_forward_or_join(&mut self) {
|
||||||
let line_idx = self.current_field();
|
let line_idx = self.current_field();
|
||||||
let line_len = self.current_text().chars().count();
|
let line_len = self.current_text().chars().count();
|
||||||
let col = self.cursor_position();
|
let col = self.cursor_position();
|
||||||
|
|
||||||
if col < line_len {
|
if col < line_len {
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
let _ = self.delete_forward();
|
let _ = self.delete_forward();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(new_col) = self
|
if let Some(new_col) =
|
||||||
.editor
|
self.editor.data_provider_mut().join_with_next(line_idx)
|
||||||
.data_provider_mut()
|
|
||||||
.join_with_next(line_idx)
|
|
||||||
{
|
{
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
self.set_cursor_position(new_col);
|
self.set_cursor_position(new_col);
|
||||||
self.enter_edit_mode();
|
self.enter_edit_mode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override for multiline: insert new blank line below and enter insert mode.
|
|
||||||
pub fn open_line_below(&mut self) -> Result<()> {
|
|
||||||
let line_idx = self.current_field();
|
|
||||||
let new_idx = self
|
|
||||||
.editor
|
|
||||||
.data_provider_mut()
|
|
||||||
.insert_blank_line_after(line_idx);
|
|
||||||
|
|
||||||
self.transition_to_field(new_idx)?;
|
|
||||||
self.move_line_start();
|
|
||||||
self.enter_edit_mode();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override for multiline: insert new blank line above and enter insert mode.
|
|
||||||
pub fn open_line_above(&mut self) -> Result<()> {
|
|
||||||
let line_idx = self.current_field();
|
|
||||||
let new_idx = self
|
|
||||||
.editor
|
|
||||||
.data_provider_mut()
|
|
||||||
.insert_blank_line_before(line_idx);
|
|
||||||
|
|
||||||
self.transition_to_field(new_idx)?;
|
|
||||||
self.move_line_start();
|
|
||||||
self.enter_edit_mode();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drive from KeyEvent; you can still call all FormEditor methods directly
|
|
||||||
pub fn input(&mut self, key: KeyEvent) {
|
pub fn input(&mut self, key: KeyEvent) {
|
||||||
if key.kind != KeyEventKind::Press {
|
if key.kind != KeyEventKind::Press {
|
||||||
return;
|
return;
|
||||||
@@ -199,20 +307,25 @@ impl TextAreaState {
|
|||||||
self.move_line_end();
|
self.move_line_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional: word motions
|
|
||||||
(KeyCode::Char('b'), KeyModifiers::ALT) => self.move_word_prev(),
|
(KeyCode::Char('b'), KeyModifiers::ALT) => self.move_word_prev(),
|
||||||
(KeyCode::Char('f'), KeyModifiers::ALT) => self.move_word_next(),
|
(KeyCode::Char('f'), KeyModifiers::ALT) => self.move_word_next(),
|
||||||
(KeyCode::Char('e'), KeyModifiers::ALT) => self.move_word_end(),
|
(KeyCode::Char('e'), KeyModifiers::ALT) => self.move_word_end(),
|
||||||
|
|
||||||
// Printable characters
|
|
||||||
(KeyCode::Char(c), m) if m.is_empty() => {
|
(KeyCode::Char(c), m) if m.is_empty() => {
|
||||||
self.enter_edit_mode();
|
self.enter_edit_mode();
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
let _ = self.insert_char(c);
|
let _ = self.insert_char(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple Tab policy
|
|
||||||
(KeyCode::Tab, _) => {
|
(KeyCode::Tab, _) => {
|
||||||
self.enter_edit_mode();
|
self.enter_edit_mode();
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
{
|
||||||
|
self.edited_this_frame = true;
|
||||||
|
}
|
||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
let _ = self.insert_char(' ');
|
let _ = self.insert_char(' ');
|
||||||
}
|
}
|
||||||
@@ -222,43 +335,184 @@ impl TextAreaState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cursor helpers for GUI
|
#[cfg(feature = "gui")]
|
||||||
|
fn visual_rows_before_line_and_intra_indented(
|
||||||
|
&self,
|
||||||
|
width: u16,
|
||||||
|
line_idx: usize,
|
||||||
|
) -> u16 {
|
||||||
|
let provider = self.editor.data_provider();
|
||||||
|
let mut acc: u16 = 0;
|
||||||
|
let indent = self.wrap_indent_cols;
|
||||||
|
|
||||||
|
for i in 0..line_idx {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
acc = acc.saturating_add(count_wrapped_rows_indented(s, width, indent));
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
|
pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
|
||||||
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
||||||
let line_idx = self.current_field() as u16;
|
let line_idx = self.current_field() as usize;
|
||||||
let y = inner.y + line_idx.saturating_sub(self.scroll_y);
|
|
||||||
|
|
||||||
|
match self.overflow_mode {
|
||||||
|
TextOverflowMode::Wrap => {
|
||||||
|
let width = inner.width;
|
||||||
|
let y_top = inner.y;
|
||||||
|
let indent = self.wrap_indent_cols;
|
||||||
|
|
||||||
|
if width == 0 {
|
||||||
|
let prefix = self.visual_rows_before_line_and_intra_indented(1, line_idx);
|
||||||
|
let y = y_top.saturating_add(prefix.saturating_sub(self.scroll_y));
|
||||||
|
return (inner.x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
let prefix_rows =
|
||||||
|
self.visual_rows_before_line_and_intra_indented(width, line_idx);
|
||||||
|
let current_line = self.current_text();
|
||||||
|
let col_chars = self.display_cursor_position();
|
||||||
|
|
||||||
|
let (subrow, x_cols) = wrapped_rows_to_cursor_indented(
|
||||||
|
¤t_line,
|
||||||
|
width,
|
||||||
|
indent,
|
||||||
|
col_chars,
|
||||||
|
);
|
||||||
|
|
||||||
|
let caret_vis_row = prefix_rows.saturating_add(subrow);
|
||||||
|
let y = y_top.saturating_add(caret_vis_row.saturating_sub(self.scroll_y));
|
||||||
|
let x = inner.x.saturating_add(x_cols);
|
||||||
|
(x, y)
|
||||||
|
}
|
||||||
|
TextOverflowMode::Indicator { .. } => {
|
||||||
|
let y = inner.y + (line_idx as u16).saturating_sub(self.scroll_y);
|
||||||
let current_line = self.current_text();
|
let current_line = self.current_text();
|
||||||
let col = self.display_cursor_position();
|
let col = self.display_cursor_position();
|
||||||
|
|
||||||
let mut x_off: u16 = 0;
|
let mut x_cols: u16 = 0;
|
||||||
|
let mut total_cols: u16 = 0;
|
||||||
for (i, ch) in current_line.chars().enumerate() {
|
for (i, ch) in current_line.chars().enumerate() {
|
||||||
if i >= col {
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
break;
|
if i < col {
|
||||||
|
x_cols = x_cols.saturating_add(w);
|
||||||
}
|
}
|
||||||
x_off = x_off
|
total_cols = total_cols.saturating_add(w);
|
||||||
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
|
||||||
}
|
}
|
||||||
let x = inner.x.saturating_add(x_off);
|
|
||||||
|
let left_cols = if self.h_scroll > 0 { 1 } else { 0 };
|
||||||
|
|
||||||
|
let mut x_off_visible = x_cols
|
||||||
|
.saturating_sub(self.h_scroll)
|
||||||
|
.saturating_add(left_cols);
|
||||||
|
|
||||||
|
let limit = inner.width.saturating_sub(1 + RIGHT_PAD);
|
||||||
|
|
||||||
|
if x_off_visible > limit {
|
||||||
|
x_off_visible = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = inner.x.saturating_add(x_off_visible);
|
||||||
(x, y)
|
(x, y)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub(crate) fn ensure_visible(
|
pub(crate) fn ensure_visible(&mut self, area: Rect, block: Option<&Block<'_>>) {
|
||||||
&mut self,
|
|
||||||
area: Rect,
|
|
||||||
block: Option<&Block<'_>>,
|
|
||||||
) {
|
|
||||||
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
||||||
if inner.height == 0 {
|
if inner.height == 0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let line_idx = self.current_field() as u16;
|
|
||||||
if line_idx < self.scroll_y {
|
match self.overflow_mode {
|
||||||
self.scroll_y = line_idx;
|
TextOverflowMode::Indicator { .. } => {
|
||||||
} else if line_idx >= self.scroll_y + inner.height {
|
let line_idx_u16 = self.current_field() as u16;
|
||||||
self.scroll_y = line_idx.saturating_sub(inner.height - 1);
|
if line_idx_u16 < self.scroll_y {
|
||||||
|
self.scroll_y = line_idx_u16;
|
||||||
|
} else if line_idx_u16 >= self.scroll_y + inner.height {
|
||||||
|
self.scroll_y = line_idx_u16.saturating_sub(inner.height - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let width = inner.width;
|
||||||
|
if width == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let current_line = self.current_text();
|
||||||
|
let mut total_cols: u16 = 0;
|
||||||
|
for ch in current_line.chars() {
|
||||||
|
total_cols = total_cols
|
||||||
|
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
|
}
|
||||||
|
if total_cols <= width {
|
||||||
|
self.h_scroll = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let col = self.display_cursor_position();
|
||||||
|
let mut cursor_cols: u16 = 0;
|
||||||
|
for (i, ch) in current_line.chars().enumerate() {
|
||||||
|
if i >= col {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cursor_cols = cursor_cols
|
||||||
|
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (target_h, _left_cols) =
|
||||||
|
compute_h_scroll_with_padding(cursor_cols, width);
|
||||||
|
|
||||||
|
if target_h > self.h_scroll {
|
||||||
|
self.h_scroll = target_h;
|
||||||
|
} else if cursor_cols < self.h_scroll {
|
||||||
|
self.h_scroll = cursor_cols;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TextOverflowMode::Wrap => {
|
||||||
|
let width = inner.width;
|
||||||
|
if width == 0 {
|
||||||
|
self.h_scroll = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let indent = self.wrap_indent_cols;
|
||||||
|
let line_idx = self.current_field() as usize;
|
||||||
|
|
||||||
|
let prefix_rows =
|
||||||
|
self.visual_rows_before_line_and_intra_indented(width, line_idx);
|
||||||
|
|
||||||
|
let current_line = self.current_text();
|
||||||
|
let col = self.display_cursor_position();
|
||||||
|
|
||||||
|
let (subrow, _x_cols) =
|
||||||
|
wrapped_rows_to_cursor_indented(¤t_line, width, indent, col);
|
||||||
|
|
||||||
|
let caret_vis_row = prefix_rows.saturating_add(subrow);
|
||||||
|
|
||||||
|
let top = self.scroll_y;
|
||||||
|
let height = inner.height;
|
||||||
|
|
||||||
|
if caret_vis_row < top {
|
||||||
|
self.scroll_y = caret_vis_row;
|
||||||
|
} else {
|
||||||
|
let bottom = top.saturating_add(height.saturating_sub(1));
|
||||||
|
if caret_vis_row > bottom {
|
||||||
|
let shift = caret_vis_row.saturating_sub(bottom);
|
||||||
|
self.scroll_y = top.saturating_add(shift);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.h_scroll = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
pub(crate) fn take_edited_flag(&mut self) -> bool {
|
||||||
|
let v = self.edited_this_frame;
|
||||||
|
self.edited_this_frame = false;
|
||||||
|
v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,23 @@ use ratatui::{
|
|||||||
style::Style,
|
style::Style,
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{
|
widgets::{
|
||||||
Block, BorderType, Borders, Paragraph, StatefulWidget, Widget, Wrap,
|
Block, BorderType, Borders, Paragraph, StatefulWidget, Widget,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use crate::data_provider::DataProvider; // bring trait into scope
|
use crate::data_provider::DataProvider;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use crate::textarea::state::TextAreaState;
|
use crate::textarea::state::{
|
||||||
|
compute_h_scroll_with_padding,
|
||||||
|
count_wrapped_rows_indented,
|
||||||
|
TextAreaState,
|
||||||
|
TextOverflowMode,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -60,6 +68,197 @@ impl<'a> TextArea<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn display_width(s: &str) -> u16 {
|
||||||
|
s.chars()
|
||||||
|
.map(|c| UnicodeWidthChar::width(c).unwrap_or(0) as u16)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn display_cols_up_to(s: &str, char_count: usize) -> u16 {
|
||||||
|
let mut cols: u16 = 0;
|
||||||
|
for (i, ch) in s.chars().enumerate() {
|
||||||
|
if i >= char_count {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
|
}
|
||||||
|
cols
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
||||||
|
if max_cols == 0 {
|
||||||
|
return String::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut current_cols: u16 = 0;
|
||||||
|
let mut output = String::new();
|
||||||
|
let mut taken: u16 = 0;
|
||||||
|
let mut started = false;
|
||||||
|
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
|
||||||
|
if !started {
|
||||||
|
if current_cols.saturating_add(w) <= start_cols {
|
||||||
|
current_cols = current_cols.saturating_add(w);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
started = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if taken.saturating_add(w) > max_cols {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push(ch);
|
||||||
|
taken = taken.saturating_add(w);
|
||||||
|
current_cols = current_cols.saturating_add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn clip_window_with_indicator_padded(
|
||||||
|
text: &str,
|
||||||
|
view_width: u16,
|
||||||
|
indicator: char,
|
||||||
|
start_cols: u16,
|
||||||
|
) -> Line<'static> {
|
||||||
|
if view_width == 0 {
|
||||||
|
return Line::from("");
|
||||||
|
}
|
||||||
|
|
||||||
|
let total = display_width(text);
|
||||||
|
|
||||||
|
// Left indicator if we scrolled
|
||||||
|
let show_left = start_cols > 0;
|
||||||
|
let left_cols: u16 = if show_left { 1 } else { 0 };
|
||||||
|
|
||||||
|
// Capacity for text if we also need a right indicator
|
||||||
|
let cap_with_right = view_width.saturating_sub(left_cols + 1);
|
||||||
|
|
||||||
|
// Do we still have content beyond this window?
|
||||||
|
let remaining = total.saturating_sub(start_cols);
|
||||||
|
let show_right = remaining > cap_with_right;
|
||||||
|
|
||||||
|
// Final capacity for visible text
|
||||||
|
let max_visible = if show_right {
|
||||||
|
cap_with_right
|
||||||
|
} else {
|
||||||
|
view_width.saturating_sub(left_cols)
|
||||||
|
};
|
||||||
|
|
||||||
|
let visible = slice_by_display_cols(text, start_cols, max_visible);
|
||||||
|
|
||||||
|
let mut spans: Vec<Span> = Vec::new();
|
||||||
|
if show_left {
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Visible text
|
||||||
|
spans.push(Span::raw(visible.clone()));
|
||||||
|
|
||||||
|
// Place $ flush-right
|
||||||
|
if show_right {
|
||||||
|
let used_cols = left_cols + display_width(&visible);
|
||||||
|
let right_pos = view_width.saturating_sub(1);
|
||||||
|
let filler = right_pos.saturating_sub(used_cols);
|
||||||
|
if filler > 0 {
|
||||||
|
spans.push(Span::raw(" ".repeat(filler as usize)));
|
||||||
|
}
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Line::from(spans)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn wrap_segments_with_indent(
|
||||||
|
s: &str,
|
||||||
|
width: u16,
|
||||||
|
indent: u16,
|
||||||
|
) -> Vec<String> {
|
||||||
|
let mut segments: Vec<String> = Vec::new();
|
||||||
|
if width == 0 {
|
||||||
|
segments.push(String::new());
|
||||||
|
return segments;
|
||||||
|
}
|
||||||
|
|
||||||
|
let indent = indent.min(width.saturating_sub(1));
|
||||||
|
let cont_cap = width.saturating_sub(indent);
|
||||||
|
let indent_str = " ".repeat(indent as usize);
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
let mut used: u16 = 0;
|
||||||
|
let mut first = true;
|
||||||
|
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
let cap = if first { width } else { cont_cap };
|
||||||
|
|
||||||
|
// Early-wrap: wrap before filling the last cell (and avoid empty segment)
|
||||||
|
if used > 0 && used.saturating_add(w) >= cap {
|
||||||
|
segments.push(buf);
|
||||||
|
buf = String::new();
|
||||||
|
used = 0;
|
||||||
|
first = false;
|
||||||
|
if indent > 0 {
|
||||||
|
buf.push_str(&indent_str);
|
||||||
|
used = indent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.push(ch);
|
||||||
|
used = used.saturating_add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
segments.push(buf);
|
||||||
|
segments
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map visual row offset to (logical line, intra segment)
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
fn resolve_start_line_and_intra_indented(
|
||||||
|
state: &TextAreaState,
|
||||||
|
inner: Rect,
|
||||||
|
) -> (usize, u16) {
|
||||||
|
let provider = state.editor.data_provider();
|
||||||
|
let total = provider.line_count();
|
||||||
|
|
||||||
|
if total == 0 {
|
||||||
|
return (0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let wrap = matches!(state.overflow_mode, TextOverflowMode::Wrap);
|
||||||
|
let width = inner.width;
|
||||||
|
let target_vis = state.scroll_y;
|
||||||
|
|
||||||
|
if !wrap {
|
||||||
|
let start = (target_vis as usize).min(total);
|
||||||
|
return (start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let indent = state.wrap_indent_cols;
|
||||||
|
|
||||||
|
let mut acc: u16 = 0;
|
||||||
|
for i in 0..total {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
let rows = count_wrapped_rows_indented(s, width, indent);
|
||||||
|
if acc.saturating_add(rows) > target_vis {
|
||||||
|
let intra = target_vis.saturating_sub(acc);
|
||||||
|
return (i, intra);
|
||||||
|
}
|
||||||
|
acc = acc.saturating_add(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
(total.saturating_sub(1), 0)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
impl<'a> StatefulWidget for TextArea<'a> {
|
impl<'a> StatefulWidget for TextArea<'a> {
|
||||||
type State = TextAreaState;
|
type State = TextAreaState;
|
||||||
@@ -74,33 +273,80 @@ impl<'a> StatefulWidget for TextArea<'a> {
|
|||||||
area
|
area
|
||||||
};
|
};
|
||||||
|
|
||||||
let total = state.editor.data_provider().line_count();
|
let edited_now = state.take_edited_flag();
|
||||||
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);
|
let wrap_mode = matches!(state.overflow_mode, TextOverflowMode::Wrap);
|
||||||
|
let provider = state.editor.data_provider();
|
||||||
|
let total = provider.line_count();
|
||||||
|
|
||||||
if start >= end {
|
let (start, intra) = resolve_start_line_and_intra_indented(state, inner);
|
||||||
|
|
||||||
|
let mut display_lines: Vec<Line> = Vec::new();
|
||||||
|
|
||||||
|
if total == 0 || start >= total {
|
||||||
if let Some(ph) = &state.placeholder {
|
if let Some(ph) = &state.placeholder {
|
||||||
display_lines.push(Line::from(Span::raw(ph.clone())));
|
display_lines.push(Line::from(Span::raw(ph.clone())));
|
||||||
}
|
}
|
||||||
|
} else if wrap_mode {
|
||||||
|
// manual pre-wrap path (unchanged)
|
||||||
|
let mut rows_left = inner.height;
|
||||||
|
let indent = state.wrap_indent_cols;
|
||||||
|
let mut i = start;
|
||||||
|
while i < total && rows_left > 0 {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
let segments = wrap_segments_with_indent(s, inner.width, indent);
|
||||||
|
let skip = if i == start { intra as usize } else { 0 };
|
||||||
|
for seg in segments.into_iter().skip(skip) {
|
||||||
|
display_lines.push(Line::from(Span::raw(seg)));
|
||||||
|
rows_left = rows_left.saturating_sub(1);
|
||||||
|
if rows_left == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Indicator mode: full inner width; RIGHT_PAD only affects cursor clamp and h-scroll
|
||||||
|
let end = (start.saturating_add(inner.height as usize)).min(total);
|
||||||
|
|
||||||
for i in start..end {
|
for i in start..end {
|
||||||
let s = state.editor.data_provider().field_value(i);
|
let s = provider.field_value(i);
|
||||||
display_lines.push(Line::from(Span::raw(s.to_string())));
|
match state.overflow_mode {
|
||||||
|
TextOverflowMode::Wrap => unreachable!(),
|
||||||
|
TextOverflowMode::Indicator { ch } => {
|
||||||
|
let fits = display_width(&s) <= inner.width;
|
||||||
|
|
||||||
|
let start_cols = if i == state.current_field() {
|
||||||
|
let col_idx = state.display_cursor_position();
|
||||||
|
let cursor_cols = display_cols_up_to(&s, col_idx);
|
||||||
|
let (target_h, _left_cols) =
|
||||||
|
compute_h_scroll_with_padding(cursor_cols, inner.width);
|
||||||
|
|
||||||
|
if fits {
|
||||||
|
if edited_now { target_h } else { 0 }
|
||||||
|
} else {
|
||||||
|
target_h.max(state.h_scroll)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
display_lines.push(clip_window_with_indicator_padded(
|
||||||
|
&s,
|
||||||
|
inner.width,
|
||||||
|
ch,
|
||||||
|
start_cols,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut p = Paragraph::new(display_lines)
|
let p = Paragraph::new(display_lines)
|
||||||
.alignment(Alignment::Left)
|
.alignment(Alignment::Left)
|
||||||
.style(self.style);
|
.style(self.style);
|
||||||
|
|
||||||
if state.wrap {
|
// No Paragraph::wrap/scroll in wrap mode — we pre-wrap.
|
||||||
p = p.wrap(Wrap { trim: false });
|
|
||||||
}
|
|
||||||
|
|
||||||
p.render(inner, buf);
|
p.render(inner, buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user