diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-10-28 10:12:37 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-10-28 10:12:37 -0400 |
commit | 9b6aea79036c40885f352d26e8a2a4892da15306 (patch) | |
tree | b667fd49d6ec27bd46de54f7b6c40634b8b7f288 /src/commands | |
parent | 6d4a2931a3d03c2b13ad5377b2e4446c96209df4 (diff) |
fix crash cycling through multiple tabs
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/key_command.rs | 5 | ||||
-rw-r--r-- | src/commands/tab_ops.rs | 20 |
2 files changed, 15 insertions, 10 deletions
diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs index 9519e7c..aceb307 100644 --- a/src/commands/key_command.rs +++ b/src/commands/key_command.rs @@ -323,10 +323,7 @@ impl JoshutoRunnable for KeyCommand { Self::SortReverse => sort::toggle_reverse(context), Self::TabSwitch(i) => { - let new_index = (context.tab_context_ref().get_index() as i32 + i) - % context.tab_context_ref().len() as i32; - let new_index = new_index as usize; - tab_ops::tab_switch(new_index, context)?; + tab_ops::tab_switch(*i, context)?; Ok(()) } } diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs index 77db8ee..4bf415e 100644 --- a/src/commands/tab_ops.rs +++ b/src/commands/tab_ops.rs @@ -10,7 +10,7 @@ use crate::HOME_DIR; use super::quit; -pub fn tab_switch(new_index: usize, context: &mut JoshutoContext) -> std::io::Result<()> { +fn _tab_switch(new_index: usize, context: &mut JoshutoContext) -> std::io::Result<()> { context.tab_context_mut().set_index(new_index); let path = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf(); std::env::set_current_dir(path.as_path())?; @@ -24,6 +24,14 @@ pub fn tab_switch(new_index: usize, context: &mut JoshutoContext) -> std::io::Re Ok(()) } +pub fn tab_switch(offset: i32, context: &mut JoshutoContext) -> std::io::Result<()> { + let index = context.tab_context_ref().get_index(); + let num_tabs = context.tab_context_ref().len(); + let new_index = (index as i32 + num_tabs as i32 + offset) as usize % num_tabs; + + _tab_switch(new_index, context) +} + pub fn new_tab(context: &mut JoshutoContext) -> JoshutoResult<()> { /* start the new tab in $HOME or root */ let curr_path = match HOME_DIR.as_ref() { @@ -35,7 +43,7 @@ pub fn new_tab(context: &mut JoshutoContext) -> JoshutoResult<()> { context.tab_context_mut().push_tab(tab); let new_index = context.tab_context_ref().len() - 1; context.tab_context_mut().set_index(new_index); - tab_switch(new_index, context)?; + _tab_switch(new_index, context)?; LoadChild::load_child(context)?; Ok(()) } @@ -47,10 +55,10 @@ pub fn close_tab(context: &mut JoshutoContext) -> JoshutoResult<()> { let mut tab_index = context.tab_context_ref().get_index(); let _ = context.tab_context_mut().pop_tab(tab_index); - if tab_index > 0 { - tab_index -= 1; - context.tab_context_mut().set_index(tab_index); + let num_tabs = context.tab_context_ref().len(); + if tab_index >= num_tabs { + tab_index = num_tabs - 1; } - tab_switch(tab_index, context)?; + _tab_switch(tab_index, context)?; Ok(()) } |