highlight is now working properly well, can keep on going
This commit is contained in:
@@ -9,27 +9,26 @@ use ratatui::{
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use std::cmp::{min, max}; // Import min and max
|
||||
use std::cmp::{min, max};
|
||||
|
||||
pub fn render_canvas(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &impl CanvasState,
|
||||
fields: &[&str],
|
||||
current_field_idx: &usize, // Renamed for clarity
|
||||
current_field_idx: &usize,
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
is_highlight_mode: bool,
|
||||
is_linewise_highlight: bool,
|
||||
highlight_anchor: Option<(usize, usize)>,
|
||||
) -> Option<Rect> {
|
||||
// Split area into columns
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(area);
|
||||
|
||||
// Input container styling
|
||||
let border_style = if form_state.has_unsaved_changes() {
|
||||
Style::default().fg(theme.warning)
|
||||
} else if is_edit_mode {
|
||||
@@ -42,7 +41,6 @@ pub fn render_canvas(
|
||||
.border_style(border_style)
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Input block dimensions
|
||||
let input_block = Rect {
|
||||
x: columns[1].x,
|
||||
y: columns[1].y,
|
||||
@@ -52,7 +50,6 @@ pub fn render_canvas(
|
||||
|
||||
f.render_widget(&input_container, input_block);
|
||||
|
||||
// Input rows layout
|
||||
let input_area = input_container.inner(input_block);
|
||||
let input_rows = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
@@ -82,87 +79,88 @@ pub fn render_canvas(
|
||||
let text = input.as_str();
|
||||
let text_len = text.chars().count();
|
||||
|
||||
let line: Line; // Determine the line content with spans
|
||||
let line: Line;
|
||||
|
||||
if is_highlight_mode && highlight_anchor.is_some() {
|
||||
let (anchor_field, anchor_char) = highlight_anchor.unwrap();
|
||||
|
||||
// Determine the actual start and end fields/chars for rendering
|
||||
let start_field = min(anchor_field, *current_field_idx);
|
||||
let end_field = max(anchor_field, *current_field_idx);
|
||||
|
||||
let (start_char, end_char) = if anchor_field == *current_field_idx {
|
||||
// Single line selection
|
||||
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
|
||||
} else if anchor_field < *current_field_idx {
|
||||
// Anchor is above cursor
|
||||
(anchor_char, current_cursor_pos)
|
||||
} else {
|
||||
// Anchor is below cursor
|
||||
(current_cursor_pos, anchor_char)
|
||||
};
|
||||
|
||||
// Style for highlighted text
|
||||
let highlight_style = Style::default().fg(theme.highlight).bg(theme.highlight_bg).add_modifier(Modifier::BOLD);
|
||||
// Style for normal text within the highlight range (active field color)
|
||||
let highlight_style = Style::default()
|
||||
.fg(theme.highlight)
|
||||
.bg(theme.highlight_bg)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let normal_style_in_highlight = Style::default().fg(theme.highlight);
|
||||
// Style for normal text outside highlight range (inactive field color)
|
||||
let normal_style_outside = Style::default().fg(theme.fg);
|
||||
|
||||
if i >= start_field && i <= end_field {
|
||||
// This line is within the highlight range
|
||||
|
||||
if start_field == end_field { // Case 1: Single Line Highlight
|
||||
let safe_start = start_char.min(text_len);
|
||||
let safe_end = end_char.min(text_len);
|
||||
let before: String = text.chars().take(safe_start).collect();
|
||||
let highlighted: String = text.chars().skip(safe_start).take(safe_end - safe_start).collect();
|
||||
let after: String = text.chars().skip(safe_end).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight),
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight),
|
||||
]);
|
||||
} else if i == start_field { // Case 2: Multi-Line Highlight - Start Line
|
||||
let safe_start = start_char.min(text_len);
|
||||
let before: String = text.chars().take(safe_start).collect();
|
||||
let highlighted: String = text.chars().skip(safe_start).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight), // Use active color before highlight starts
|
||||
Span::styled(highlighted, highlight_style),
|
||||
]);
|
||||
} else if i == end_field { // Case 4: Multi-Line Highlight - End Line
|
||||
let safe_end = end_char.min(text_len);
|
||||
let highlighted: String = text.chars().take(safe_end).collect();
|
||||
let after: String = text.chars().skip(safe_end).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight), // Use active color after highlight ends
|
||||
]);
|
||||
} else { // Case 3: Multi-Line Highlight - Middle Line
|
||||
line = Line::from(Span::styled(text, highlight_style)); // Highlight whole line
|
||||
if is_linewise_highlight {
|
||||
if i >= start_field && i <= end_field {
|
||||
line = Line::from(Span::styled(text, highlight_style));
|
||||
} else {
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
if is_active { normal_style_in_highlight } else { normal_style_outside }
|
||||
));
|
||||
}
|
||||
} else {
|
||||
let (start_char, end_char) = if anchor_field == *current_field_idx {
|
||||
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
|
||||
} else if anchor_field < *current_field_idx {
|
||||
(anchor_char, current_cursor_pos)
|
||||
} else {
|
||||
(current_cursor_pos, anchor_char)
|
||||
};
|
||||
|
||||
if i >= start_field && i <= end_field {
|
||||
if start_field == end_field {
|
||||
let safe_start = start_char.min(text_len);
|
||||
let safe_end = end_char.min(text_len);
|
||||
let before: String = text.chars().take(safe_start).collect();
|
||||
let highlighted: String = text.chars().skip(safe_start).take(safe_end - safe_start).collect();
|
||||
let after: String = text.chars().skip(safe_end).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight),
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight),
|
||||
]);
|
||||
} else if i == start_field {
|
||||
let safe_start = start_char.min(text_len);
|
||||
let before: String = text.chars().take(safe_start).collect();
|
||||
let highlighted: String = text.chars().skip(safe_start).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight),
|
||||
Span::styled(highlighted, highlight_style),
|
||||
]);
|
||||
} else if i == end_field {
|
||||
let safe_end = end_char.min(text_len);
|
||||
let highlighted: String = text.chars().take(safe_end).collect();
|
||||
let after: String = text.chars().skip(safe_end).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight),
|
||||
]);
|
||||
} else {
|
||||
line = Line::from(Span::styled(text, highlight_style));
|
||||
}
|
||||
} else {
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
if is_active { normal_style_in_highlight } else { normal_style_outside }
|
||||
));
|
||||
}
|
||||
} else { // Case 5: Line Outside Highlight Range
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
if is_active { normal_style_in_highlight } else { normal_style_outside }
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Not in highlight mode, render normally
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
if is_active { Style::default().fg(theme.highlight) } else { Style::default().fg(theme.fg) }
|
||||
));
|
||||
};
|
||||
|
||||
let input_display = Paragraph::new(line)
|
||||
.alignment(Alignment::Left);
|
||||
let input_display = Paragraph::new(line).alignment(Alignment::Left);
|
||||
f.render_widget(input_display, input_rows[i]);
|
||||
|
||||
if is_active {
|
||||
active_field_input_rect = Some(input_rows[i]);
|
||||
// Set cursor position (using current_field_idx directly)
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos() as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y));
|
||||
@@ -171,4 +169,3 @@ pub fn render_canvas(
|
||||
|
||||
active_field_input_rect
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user