From 722121c18bad39338f904313b662644c6fefd0bf Mon Sep 17 00:00:00 2001 From: Kyohei Uto Date: Sun, 28 Jan 2024 06:52:23 +0900 Subject: [PATCH] Add move_{left, right}_command_line --- src/run.rs | 136 ++++++++++++++++------------------------------------ src/term.rs | 32 +++++++++++++ 2 files changed, 72 insertions(+), 96 deletions(-) diff --git a/src/run.rs b/src/run.rs index 9e71243..7ff0881 100644 --- a/src/run.rs +++ b/src/run.rs @@ -932,33 +932,19 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> { } (KeyCode::Left, KeyModifiers::NONE) => { - if current_char_pos == 0 { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - new_name[current_char_pos - 1], - ) - { - current_char_pos -= 1; - current_pos -= to_be_skipped as u16; - move_left(to_be_skipped as u16); - } + move_left_command_line( + &mut new_name, + &mut current_char_pos, + &mut current_pos, + ) } (KeyCode::Right, KeyModifiers::NONE) => { - if current_char_pos == new_name.len() { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - new_name[current_char_pos], - ) - { - current_char_pos += 1; - current_pos += to_be_skipped as u16; - move_right(to_be_skipped as u16); - } + move_right_command_line( + &mut new_name, + &mut current_char_pos, + &mut current_pos, + ) } (KeyCode::Backspace, KeyModifiers::NONE) @@ -1337,33 +1323,19 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> { } (KeyCode::Left, KeyModifiers::NONE) => { - if current_char_pos == 0 { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - rename[current_char_pos - 1], - ) - { - current_char_pos -= 1; - current_pos -= to_be_skipped as u16; - move_left(to_be_skipped as u16); - } + move_left_command_line( + &mut rename, + &mut current_char_pos, + &mut current_pos, + ) } (KeyCode::Right, KeyModifiers::NONE) => { - if current_char_pos == rename.len() { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - rename[current_char_pos], - ) - { - current_char_pos += 1; - current_pos += to_be_skipped as u16; - move_right(to_be_skipped as u16); - } + move_right_command_line( + &mut rename, + &mut current_char_pos, + &mut current_pos, + ) } (KeyCode::Backspace, KeyModifiers::NONE) @@ -1478,33 +1450,19 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> { } (KeyCode::Left, KeyModifiers::NONE) => { - if current_char_pos == 0 { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - keyword[current_char_pos - 1], - ) - { - current_char_pos -= 1; - current_pos -= to_be_skipped as u16; - move_left(to_be_skipped as u16); - } + move_left_command_line( + &mut keyword, + &mut current_char_pos, + &mut current_pos, + ); } (KeyCode::Right, KeyModifiers::NONE) => { - if current_char_pos == keyword.len() { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - keyword[current_char_pos], - ) - { - current_char_pos += 1; - current_pos += to_be_skipped as u16; - move_right(to_be_skipped as u16); - } + move_right_command_line( + &mut keyword, + &mut current_char_pos, + &mut current_pos, + ); } (KeyCode::Backspace, KeyModifiers::NONE) @@ -2106,33 +2064,19 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> { } (KeyCode::Left, KeyModifiers::NONE) => { - if current_char_pos == 0 { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - command[current_char_pos - 1], - ) - { - current_char_pos -= 1; - current_pos -= to_be_skipped as u16; - move_left(to_be_skipped as u16); - } + move_left_command_line( + &mut command, + &mut current_char_pos, + &mut current_pos, + ); } (KeyCode::Right, KeyModifiers::NONE) => { - if current_char_pos == command.len() { - continue; - }; - if let Some(to_be_skipped) = - unicode_width::UnicodeWidthChar::width( - command[current_char_pos], - ) - { - current_char_pos += 1; - current_pos += to_be_skipped as u16; - move_right(to_be_skipped as u16); - } + move_right_command_line( + &mut command, + &mut current_char_pos, + &mut current_pos, + ); } (KeyCode::Backspace, KeyModifiers::NONE) diff --git a/src/term.rs b/src/term.rs index b2b7f87..c6435fa 100644 --- a/src/term.rs +++ b/src/term.rs @@ -65,10 +65,42 @@ pub fn move_left(x: u16) { print!("{}", MoveLeft(x)); } +pub fn move_left_command_line( + input: &mut [char], + current_char_pos: &mut usize, + current_pos: &mut u16, +) { + if current_char_pos == &0 { + return; + }; + if let Some(to_be_skipped) = + unicode_width::UnicodeWidthChar::width(input[*current_char_pos - 1]) + { + *current_char_pos -= 1; + *current_pos -= to_be_skipped as u16; + move_left(to_be_skipped as u16); + } +} + pub fn move_right(x: u16) { print!("{}", MoveRight(x)); } +pub fn move_right_command_line( + input: &mut [char], + current_char_pos: &mut usize, + current_pos: &mut u16, +) { + if *current_char_pos == input.len() { + return; + }; + if let Some(to_be_skipped) = unicode_width::UnicodeWidthChar::width(input[*current_char_pos]) { + *current_char_pos += 1; + *current_pos += to_be_skipped as u16; + move_right(to_be_skipped as u16); + } +} + pub fn hide_cursor() { print!("{}", Hide); }