Skip to content

Commit

Permalink
Add move_{left, right}_command_line
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoheiu committed Jan 27, 2024
1 parent 382a911 commit 722121c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 96 deletions.
136 changes: 40 additions & 96 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 722121c

Please sign in to comment.