diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
commit | 7b4c611ed4d804bd52aeda0a619a54bea9b1e13d (patch) | |
tree | 7ba31fff7d5de171aadfc32c67f81dd64428b3ba /src/commands/cursor_move.rs | |
parent | 7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (diff) |
Change command to use an enum instead of polymorphism
Diffstat (limited to 'src/commands/cursor_move.rs')
-rw-r--r-- | src/commands/cursor_move.rs | 307 |
1 files changed, 79 insertions, 228 deletions
diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index 6a4ec19..bad885f 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -1,10 +1,8 @@ -use std::path::PathBuf; - -use crate::commands::{JoshutoCommand, JoshutoRunnable}; use crate::context::JoshutoContext; use crate::error::JoshutoResult; use crate::history::DirectoryHistory; use crate::ui::TuiBackend; +use std::path::PathBuf; pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) -> JoshutoResult<()> { let mut path: Option<PathBuf> = None; @@ -37,259 +35,112 @@ pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) -> JoshutoRes Ok(()) } -#[derive(Clone, Debug)] -pub struct CursorMoveDown { - movement: usize, -} - -impl CursorMoveDown { - pub fn new(movement: usize) -> Self { - Self { movement } - } - pub const fn command() -> &'static str { - "cursor_move_up" - } -} - -impl JoshutoCommand for CursorMoveDown {} - -impl std::fmt::Display for CursorMoveDown { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{} {}", Self::command(), self.movement) - } -} - -impl JoshutoRunnable for CursorMoveDown { - fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(curr_list) => curr_list.index.map(|idx| idx + self.movement), - None => None, - }; - - if let Some(s) = movement { - cursor_move(s, context)?; - } - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub struct CursorMoveUp { - movement: usize, -} - -impl CursorMoveUp { - pub fn new(movement: usize) -> Self { - Self { movement } - } - pub const fn command() -> &'static str { - "cursor_move_down" - } -} - -impl JoshutoCommand for CursorMoveUp {} - -impl std::fmt::Display for CursorMoveUp { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{} {}", Self::command(), self.movement) - } -} - -impl JoshutoRunnable for CursorMoveUp { - fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(curr_list) => curr_list.index.map(|idx| { - if idx > self.movement { - idx - self.movement - } else { - 0 - } - }), - None => None, - }; +pub fn up(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => curr_list.index.map(|idx| if idx > u { idx - u } else { 0 }), + None => None, + }; - if let Some(s) = movement { - cursor_move(s, context)?; - } - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub struct CursorMovePageUp; - -impl CursorMovePageUp { - pub fn new() -> Self { - Self - } - pub const fn command() -> &'static str { - "cursor_move_page_up" + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } -impl JoshutoCommand for CursorMovePageUp {} - -impl std::fmt::Display for CursorMovePageUp { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", Self::command()) +pub fn down(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => curr_list.index.map(|idx| idx + u), + None => None, + }; + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } -impl JoshutoRunnable for CursorMovePageUp { - fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { - let half_page = { - match backend.terminal.as_ref().unwrap().size() { - Ok(rect) => rect.height as usize - 2, - _ => 10, - } - }; - - let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(curr_list) => { - curr_list - .index - .map(|idx| if idx > half_page { idx - half_page } else { 0 }) +pub fn home(context: &mut JoshutoContext) -> JoshutoResult<()> { + let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => { + let len = curr_list.contents.len(); + if len == 0 { + None + } else { + Some(0) } - None => None, - }; - - if let Some(s) = movement { - cursor_move(s, context)?; } - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub struct CursorMovePageDown; - -impl CursorMovePageDown { - pub fn new() -> Self { - Self - } - pub const fn command() -> &'static str { - "cursor_move_page_down" - } -} - -impl JoshutoCommand for CursorMovePageDown {} + None => None, + }; -impl std::fmt::Display for CursorMovePageDown { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", Self::command()) + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } -impl JoshutoRunnable for CursorMovePageDown { - fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { - let half_page = { - match backend.terminal.as_ref().unwrap().size() { - Ok(rect) => rect.height as usize - 2, - _ => 10, - } - }; - - let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(curr_list) => { - let dir_len = curr_list.contents.len(); - curr_list.index.map(|idx| { - if idx + half_page > dir_len - 1 { - dir_len - 1 - } else { - idx + half_page - } - }) +pub fn end(context: &mut JoshutoContext) -> JoshutoResult<()> { + let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => { + let len = curr_list.contents.len(); + if len == 0 { + None + } else { + Some(len - 1) } - None => None, - }; - - if let Some(s) = movement { - cursor_move(s, context)?; } - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub struct CursorMoveHome; - -impl CursorMoveHome { - pub fn new() -> Self { - Self - } - pub const fn command() -> &'static str { - "cursor_move_home" - } -} - -impl JoshutoCommand for CursorMoveHome {} + None => None, + }; -impl std::fmt::Display for CursorMoveHome { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", Self::command()) + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } -impl JoshutoRunnable for CursorMoveHome { - fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() - { - Some(curr_list) => { - let len = curr_list.contents.len(); - if len == 0 { - None - } else { - Some(0) - } - } - None => None, - }; - - if let Some(s) = movement { - cursor_move(s, context)?; +pub fn page_up(context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { + let half_page = { + match backend.terminal.as_ref().unwrap().size() { + Ok(rect) => rect.height as usize - 2, + _ => 10, } - Ok(()) - } -} + }; -#[derive(Clone, Debug)] -pub struct CursorMoveEnd; + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => curr_list + .index + .map(|idx| if idx > half_page { idx - half_page } else { 0 }), + None => None, + }; -impl CursorMoveEnd { - pub fn new() -> Self { - Self - } - pub const fn command() -> &'static str { - "cursor_move_end" + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } -impl JoshutoCommand for CursorMoveEnd {} - -impl std::fmt::Display for CursorMoveEnd { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", Self::command()) - } -} +pub fn page_down(context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { + let half_page = { + match backend.terminal.as_ref().unwrap().size() { + Ok(rect) => rect.height as usize - 2, + _ => 10, + } + }; -impl JoshutoRunnable for CursorMoveEnd { - fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() - { - Some(curr_list) => { - let len = curr_list.contents.len(); - if len == 0 { - None + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + Some(curr_list) => { + let dir_len = curr_list.contents.len(); + curr_list.index.map(|idx| { + if idx + half_page > dir_len - 1 { + dir_len - 1 } else { - Some(len - 1) + idx + half_page } - } - None => None, - }; - - if let Some(s) = movement { - cursor_move(s, context)?; + }) } - Ok(()) + None => None, + }; + + if let Some(s) = movement { + cursor_move(s, context)?; } + Ok(()) } |