summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyohei Uto <im@kyoheiu.dev>2024-01-28 06:35:43 +0900
committerKyohei Uto <im@kyoheiu.dev>2024-01-28 06:35:43 +0900
commit382a9115530245c6435719d6f7259ab89c9848ac (patch)
treec94e3b2bef41e68586baf56ee0cea77fbf967e1e
parent9abae188be70864d5aba3b4c72c2a56f9583dfe9 (diff)
Add state.escape to go back to normal mode
-rw-r--r--src/run.rs74
-rw-r--r--src/state.rs7
2 files changed, 26 insertions, 55 deletions
diff --git a/src/run.rs b/src/run.rs
index 09500f6..9e71243 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -459,9 +459,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
_ => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
}
}
}
@@ -702,9 +700,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
{
match (code, modifiers) {
(KeyCode::Esc, KeyModifiers::NONE) => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'zoxide;
}
@@ -729,9 +725,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
(KeyCode::Backspace, KeyModifiers::NONE)
| (KeyCode::Char('h'), KeyModifiers::CONTROL) => {
if current_pos == INITIAL_POS_Z + 1 {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'zoxide;
};
command.remove(
@@ -933,9 +927,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
(KeyCode::Esc, KeyModifiers::NONE) => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'insert;
}
@@ -1193,9 +1185,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
}
_ => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
}
}
}
@@ -1238,20 +1228,17 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
None,
false,
);
- go_to_info_line_and_reset();
- hide_cursor();
+ state.escape();
print_info("1 item yanked.", state.layout.y);
}
}
_ => {
- go_to_info_line_and_reset();
- hide_cursor();
+ state.escape();
}
}
}
}
- state.move_cursor(state.layout.y);
}
//put
@@ -1345,9 +1332,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
(KeyCode::Esc, KeyModifiers::NONE) => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break;
}
@@ -1697,9 +1682,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
{
match code {
KeyCode::Esc => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'reg;
}
@@ -1724,9 +1707,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
KeyCode::Backspace => {
if current_pos == INITIAL_POS_COMMAND_LINE {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'reg;
} else {
command.remove(
@@ -1771,13 +1752,11 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
"p" => {
//In read-only directory, put disabled
if state.is_ro {
- go_to_info_line_and_reset();
+ state.escape();
print_warning(
"Cannot put item in this directory.",
state.layout.y,
);
- hide_cursor();
- state.move_cursor(state.layout.y);
break 'reg;
}
if state.v_start.is_some() {
@@ -1856,13 +1835,11 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
state.move_cursor(state.layout.y);
break 'reg;
}
- go_to_info_line_and_reset();
- hide_cursor();
+ state.escape();
print_info(
"1 item yanked.",
state.layout.y,
);
- state.move_cursor(state.layout.y);
break 'reg;
}
//yank (visual mode)
@@ -1920,13 +1897,11 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
//In read-only directory, delete
//disabled
if state.is_ro {
- go_to_info_line_and_reset();
+ state.escape();
print_warning(
"Cannot delete item in this directory.",
state.layout.y,
);
- hide_cursor();
- state.move_cursor(state.layout.y);
break 'reg;
}
if state.v_start.is_some() {
@@ -1971,13 +1946,11 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
//In read-only directory, delete
//disabled
if state.is_ro {
- go_to_info_line_and_reset();
+ state.escape();
print_warning(
"Cannot delete item in this directory.",
state.layout.y,
);
- hide_cursor();
- state.move_cursor(state.layout.y);
break 'reg;
}
if state.v_start.is_none() {
@@ -2128,9 +2101,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
(KeyCode::Esc, KeyModifiers::NONE) => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
break 'command;
}
@@ -2216,8 +2187,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
let commands: Vec<&str> =
commands.split_whitespace().collect();
if commands.is_empty() {
- go_to_info_line_and_reset();
- state.move_cursor(state.layout.y);
+ state.escape();
break;
}
let command = commands[0];
@@ -2263,9 +2233,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
state.layout.show_reg();
state.redraw(state.layout.y);
} else if state.layout.is_reg() {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
} else {
state.layout.show_reg();
let (new_column, new_row) = state
@@ -2276,9 +2244,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
new_row,
state.layout.y,
)?;
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
}
break 'command;
}
@@ -2493,9 +2459,7 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
}
_ => {
- go_to_info_line_and_reset();
- hide_cursor();
- state.move_cursor(state.layout.y);
+ state.escape();
}
}
}
diff --git a/src/state.rs b/src/state.rs
index fa40bed..124f97e 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1076,6 +1076,13 @@ impl State {
}
}
+ /// Escape to normal mode.
+ pub fn escape(&mut self) {
+ go_to_info_line_and_reset();
+ hide_cursor();
+ self.move_cursor(self.layout.y);
+ }
+
/// Print an item in the directory.
fn print_item(&self, item: &ItemInfo) {
let name = if item.file_name.bytes().len() <= self.layout.name_max_len {