refactor: apply review cuts
- replace hand-rolled clamp_cursor_to_char_boundary with str::floor_char_boundary at its four call sites, drop the helper - dedupe the duplicated Layout::vertical constraint arms in render() - drop stale #[allow(dead_code)] on spinner_state (field is read)
This commit is contained in:
@@ -108,10 +108,10 @@ impl MainTui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_command_bar_input(&mut self, key: KeyEvent) {
|
fn handle_command_bar_input(&mut self, key: KeyEvent) {
|
||||||
self.state.command_bar_cursor_position = clamp_cursor_to_char_boundary(
|
self.state.command_bar_cursor_position = self
|
||||||
&self.state.command_bar_input,
|
.state
|
||||||
self.state.command_bar_cursor_position,
|
.command_bar_input
|
||||||
);
|
.floor_char_boundary(self.state.command_bar_cursor_position);
|
||||||
|
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
@@ -437,23 +437,8 @@ impl MainTui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) const fn clamp_cursor_to_char_boundary(
|
|
||||||
input: &str,
|
|
||||||
cursor_position: usize,
|
|
||||||
) -> usize {
|
|
||||||
if cursor_position >= input.len() {
|
|
||||||
return input.len();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut cursor_position = cursor_position;
|
|
||||||
while cursor_position > 0 && !input.is_char_boundary(cursor_position) {
|
|
||||||
cursor_position -= 1;
|
|
||||||
}
|
|
||||||
cursor_position
|
|
||||||
}
|
|
||||||
|
|
||||||
fn previous_char_boundary(input: &str, cursor_position: usize) -> usize {
|
fn previous_char_boundary(input: &str, cursor_position: usize) -> usize {
|
||||||
let cursor_position = clamp_cursor_to_char_boundary(input, cursor_position);
|
let cursor_position = input.floor_char_boundary(cursor_position);
|
||||||
|
|
||||||
input[..cursor_position]
|
input[..cursor_position]
|
||||||
.char_indices()
|
.char_indices()
|
||||||
@@ -462,7 +447,7 @@ fn previous_char_boundary(input: &str, cursor_position: usize) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn next_char_boundary(input: &str, cursor_position: usize) -> usize {
|
fn next_char_boundary(input: &str, cursor_position: usize) -> usize {
|
||||||
let cursor_position = clamp_cursor_to_char_boundary(input, cursor_position);
|
let cursor_position = input.floor_char_boundary(cursor_position);
|
||||||
|
|
||||||
if cursor_position >= input.len() {
|
if cursor_position >= input.len() {
|
||||||
return input.len();
|
return input.len();
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ pub struct MainTui {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
max_concurrent_downloads: usize,
|
max_concurrent_downloads: usize,
|
||||||
search_timeout: Duration,
|
search_timeout: Duration,
|
||||||
#[allow(dead_code)]
|
|
||||||
spinner_state: usize,
|
spinner_state: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,22 +28,15 @@ const fn command_bar_prefix(mode: CommandBarMode) -> &'static str {
|
|||||||
|
|
||||||
impl MainTui {
|
impl MainTui {
|
||||||
pub(super) fn render(&mut self, frame: &mut Frame) {
|
pub(super) fn render(&mut self, frame: &mut Frame) {
|
||||||
let main_chunks = if self.state.command_bar_active {
|
let mut constraints = vec![
|
||||||
Layout::vertical([
|
Constraint::Length(3), // Status bar
|
||||||
Constraint::Length(3), // Status bar
|
Constraint::Fill(1), // Main content
|
||||||
Constraint::Fill(1), // Main content
|
];
|
||||||
Constraint::Length(3), // Command bar
|
if self.state.command_bar_active {
|
||||||
Constraint::Length(3), // Shortcuts
|
constraints.push(Constraint::Length(3)); // Command bar
|
||||||
])
|
}
|
||||||
.split(frame.area())
|
constraints.push(Constraint::Length(3)); // Shortcuts
|
||||||
} else {
|
let main_chunks = Layout::vertical(constraints).split(frame.area());
|
||||||
Layout::vertical([
|
|
||||||
Constraint::Length(3), // Status bar
|
|
||||||
Constraint::Fill(1), // Main content
|
|
||||||
Constraint::Length(3), // Shortcuts
|
|
||||||
])
|
|
||||||
.split(frame.area())
|
|
||||||
};
|
|
||||||
|
|
||||||
render_download_stats(
|
render_download_stats(
|
||||||
frame,
|
frame,
|
||||||
@@ -432,8 +425,7 @@ fn visible_input_at_cursor(
|
|||||||
return (String::new(), 0);
|
return (String::new(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let cursor_position =
|
let cursor_position = input.floor_char_boundary(cursor_position);
|
||||||
super::input::clamp_cursor_to_char_boundary(input, cursor_position);
|
|
||||||
let cursor_character_index = input[..cursor_position].chars().count();
|
let cursor_character_index = input[..cursor_position].chars().count();
|
||||||
let max_cursor_column = usize::from(width.saturating_sub(1));
|
let max_cursor_column = usize::from(width.saturating_sub(1));
|
||||||
let start_character_index =
|
let start_character_index =
|
||||||
|
|||||||
Reference in New Issue
Block a user