its now using enum fully for the highlight mode

This commit is contained in:
filipriec
2025-04-16 00:11:41 +02:00
parent af4567aa3d
commit b6c4d3308d
11 changed files with 103 additions and 95 deletions

View File

@@ -12,6 +12,7 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use crate::state::app::highlight::HighlightState;
pub fn render_login(
f: &mut Frame,
@@ -20,9 +21,7 @@ pub fn render_login(
login_state: &LoginState,
app_state: &AppState,
is_edit_mode: bool,
is_highlight_mode: bool,
is_linewise_highlight: bool,
highlight_anchor: Option<(usize, usize)>,
highlight_state: &HighlightState,
) {
// Main container
let block = Block::default()
@@ -59,9 +58,7 @@ pub fn render_login(
&[&login_state.username, &login_state.password],
theme,
is_edit_mode,
is_highlight_mode,
is_linewise_highlight,
highlight_anchor,
highlight_state,
);
// --- ERROR MESSAGE ---

View File

@@ -14,6 +14,7 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use crate::state::app::highlight::HighlightState;
pub fn render_register(
f: &mut Frame,
@@ -22,9 +23,7 @@ pub fn render_register(
state: &RegisterState, // Use RegisterState
app_state: &AppState,
is_edit_mode: bool,
is_highlight_mode: bool,
is_linewise_highlight: bool,
highlight_anchor: Option<(usize, usize)>,
highlight_state: &HighlightState,
) {
let block = Block::default()
.borders(Borders::ALL)
@@ -67,9 +66,7 @@ pub fn render_register(
&state.inputs().iter().map(|s| *s).collect::<Vec<&String>>(), // Pass inputs directly
theme,
is_edit_mode,
is_highlight_mode,
is_linewise_highlight,
highlight_anchor,
highlight_state,
);
// --- HELP TEXT ---

View File

@@ -7,6 +7,7 @@ use ratatui::{
};
use crate::config::colors::themes::Theme;
use crate::state::pages::canvas_state::CanvasState;
use crate::state::app::highlight::HighlightState;
use crate::components::handlers::canvas::render_canvas;
pub fn render_form(
@@ -18,9 +19,7 @@ pub fn render_form(
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
is_highlight_mode: bool,
is_linewise_highlight: bool,
highlight_anchor: Option<(usize, usize)>,
highlight_state: &HighlightState,
total_count: u64,
current_position: u64,
) {
@@ -65,8 +64,6 @@ pub fn render_form(
inputs,
theme,
is_edit_mode,
is_highlight_mode,
is_linewise_highlight,
highlight_anchor,
highlight_state,
);
}

View File

@@ -9,6 +9,7 @@ use ratatui::{
};
use crate::config::colors::themes::Theme;
use crate::state::pages::canvas_state::CanvasState;
use crate::state::app::highlight::HighlightState; // Ensure correct import path
use std::cmp::{min, max};
pub fn render_canvas(
@@ -20,10 +21,9 @@ pub fn render_canvas(
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
is_highlight_mode: bool,
is_linewise_highlight: bool,
highlight_anchor: Option<(usize, usize)>,
highlight_state: &HighlightState, // Using the enum state
) -> Option<Rect> {
// ... (column split, container styling, block dimensions - unchanged) ...
let columns = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
@@ -72,6 +72,7 @@ pub fn render_canvas(
});
}
// Render inputs and cursor
for (i, input) in inputs.iter().enumerate() {
let is_active = i == *current_field_idx;
@@ -81,28 +82,20 @@ pub fn render_canvas(
let line: Line;
if is_highlight_mode && highlight_anchor.is_some() {
let (anchor_field, anchor_char) = highlight_anchor.unwrap();
let start_field = min(anchor_field, *current_field_idx);
let end_field = max(anchor_field, *current_field_idx);
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);
let normal_style_outside = Style::default().fg(theme.fg);
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 {
// --- Use match on the highlight_state enum ---
match highlight_state {
HighlightState::Off => {
// 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) }
));
}
HighlightState::Characterwise { anchor } => {
// --- Character-wise Highlight Logic ---
let (anchor_field, anchor_char) = *anchor; // Dereference the tuple from the enum
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 {
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
} else if anchor_field < *current_field_idx {
@@ -111,8 +104,13 @@ pub fn render_canvas(
(current_cursor_pos, anchor_char)
};
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);
let normal_style_outside = Style::default().fg(theme.fg);
if i >= start_field && i <= end_field {
if start_field == end_field {
// This line is within the character-wise 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();
@@ -123,7 +121,7 @@ pub fn render_canvas(
Span::styled(highlighted, highlight_style),
Span::styled(after, normal_style_in_highlight),
]);
} else if i == start_field {
} 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();
@@ -131,7 +129,7 @@ pub fn render_canvas(
Span::styled(before, normal_style_in_highlight),
Span::styled(highlighted, highlight_style),
]);
} else if i == end_field {
} 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();
@@ -139,22 +137,37 @@ pub fn render_canvas(
Span::styled(highlighted, highlight_style),
Span::styled(after, normal_style_in_highlight),
]);
} else {
line = Line::from(Span::styled(text, highlight_style));
} else { // Case 3: Multi-Line Highlight - Middle Line
line = Line::from(Span::styled(text, highlight_style)); // Highlight whole line
}
} else {
} else { // Case 5: Line Outside Character-wise Highlight Range
line = Line::from(Span::styled(
text,
if is_active { normal_style_in_highlight } else { normal_style_outside }
));
}
}
} else {
line = Line::from(Span::styled(
text,
if is_active { Style::default().fg(theme.highlight) } else { Style::default().fg(theme.fg) }
));
};
HighlightState::Linewise { anchor_line } => {
// --- Linewise Highlight Logic ---
let start_field = min(*anchor_line, *current_field_idx); // Dereference anchor_line
let end_field = max(*anchor_line, *current_field_idx); // Dereference anchor_line
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);
let normal_style_outside = Style::default().fg(theme.fg);
if i >= start_field && i <= end_field {
// Highlight the entire line
line = Line::from(Span::styled(text, highlight_style));
} else {
// Line outside linewise highlight range
line = Line::from(Span::styled(
text,
// Use normal styling (active or inactive)
if is_active { normal_style_in_highlight } else { normal_style_outside }
));
}
}
} // End match highlight_state
let input_display = Paragraph::new(line).alignment(Alignment::Left);
f.render_widget(input_display, input_rows[i]);
@@ -169,3 +182,4 @@ pub fn render_canvas(
active_field_input_rect
}