From 022eb53457560e54dea5654a7e54d594392c92af Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Wed, 29 Jun 2022 11:18:20 -0400 Subject: rename TuiBackend to AppBackend - fix initial view not correctly offset --- src/commands/bulk_rename.rs | 4 +- src/commands/command_line.rs | 4 +- src/commands/cursor_move.rs | 14 ++-- src/commands/delete_files.rs | 8 +-- src/commands/numbered_command.rs | 4 +- src/commands/open_file.rs | 140 ++++++++++++++++++------------------- src/commands/rename_file.rs | 10 +-- src/commands/search_fzf.rs | 4 +- src/commands/set_mode.rs | 4 +- src/commands/show_help.rs | 4 +- src/commands/show_tasks.rs | 4 +- src/commands/sub_process.rs | 4 +- src/commands/subdir_fzf.rs | 4 +- src/commands/zoxide.rs | 4 +- src/event/process_event.rs | 6 +- src/key_command/impl_appexecute.rs | 4 +- src/key_command/impl_numbered.rs | 4 +- src/key_command/traits.rs | 6 +- src/main.rs | 2 +- src/run.rs | 8 ++- src/ui/backend.rs | 87 +++++++++++++++++++++++ src/ui/mod.rs | 4 +- src/ui/tui_backend.rs | 87 ----------------------- src/ui/views/tui_folder_view.rs | 8 ++- src/ui/views/tui_textfield.rs | 4 +- src/ui/widgets/tui_prompt.rs | 4 +- 26 files changed, 222 insertions(+), 214 deletions(-) create mode 100644 src/ui/backend.rs delete mode 100644 src/ui/tui_backend.rs (limited to 'src') diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index 540036b..3a94f11 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -8,7 +8,7 @@ use rand::Rng; use crate::context::AppContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use crate::util::process::wait_for_enter; use super::reload; @@ -122,7 +122,7 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult { Ok(()) } -pub fn bulk_rename(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn bulk_rename(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { context.remove_external_preview(); backend.terminal_drop(); let res = _bulk_rename(context); diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index 887ea32..9c5aa70 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -5,11 +5,11 @@ use crate::context::AppContext; use crate::error::JoshutoResult; use crate::key_command::{AppExecute, Command}; use crate::ui::views::TuiTextField; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub fn read_and_execute( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, prefix: &str, suffix: &str, diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index 64809c5..ccb8d98 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -1,6 +1,6 @@ use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub fn lazy_load_directory_size(context: &mut AppContext) { let directory_size = match context @@ -120,7 +120,7 @@ pub fn end(context: &mut AppContext) -> JoshutoResult { Ok(()) } -fn get_page_size(context: &AppContext, backend: &TuiBackend) -> Option { +fn get_page_size(context: &AppContext, backend: &AppBackend) -> Option { let config = context.config_ref(); let rect = backend.terminal.as_ref().map(|t| t.size())?.ok()?; @@ -140,7 +140,7 @@ fn get_page_size(context: &AppContext, backend: &TuiBackend) -> Option { pub fn page_up( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, proportion: f64, ) -> JoshutoResult { let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion; @@ -160,7 +160,7 @@ pub fn page_up( pub fn page_down( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, proportion: f64, ) -> JoshutoResult { let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion; @@ -178,7 +178,7 @@ pub fn page_down( Ok(()) } -pub fn page_home(context: &mut AppContext, _: &mut TuiBackend) -> JoshutoResult { +pub fn page_home(context: &mut AppContext, _: &mut AppBackend) -> JoshutoResult { let new_index = context .tab_context_ref() .curr_tab_ref() @@ -190,7 +190,7 @@ pub fn page_home(context: &mut AppContext, _: &mut TuiBackend) -> JoshutoResult Ok(()) } -pub fn page_middle(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn page_middle(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { let movement = get_page_size(context, backend).unwrap_or(10) / 2; let new_index = context @@ -204,7 +204,7 @@ pub fn page_middle(context: &mut AppContext, backend: &mut TuiBackend) -> Joshut Ok(()) } -pub fn page_end(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn page_end(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { let movement = get_page_size(context, backend).unwrap_or(10) - 1; let new_index = context diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 126346f..b702771 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -6,7 +6,7 @@ use termion::event::Key; use crate::context::AppContext; use crate::history::DirectoryHistory; use crate::ui::widgets::TuiPrompt; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use super::reload; @@ -50,7 +50,7 @@ where Ok(()) } -fn delete_files(context: &mut AppContext, backend: &mut TuiBackend) -> std::io::Result<()> { +fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io::Result<()> { let delete_func = if context.config_ref().use_trash { trash_files } else { @@ -79,7 +79,7 @@ fn delete_files(context: &mut AppContext, backend: &mut TuiBackend) -> std::io:: }; match ch { - Key::Char('y') | Key::Char('\n') => { + Key::Char('Y') | Key::Char('y') | Key::Char('\n') => { let confirm_delete = if paths_len > 1 { // prompt user again for deleting multiple files let ch = { @@ -112,7 +112,7 @@ fn delete_files(context: &mut AppContext, backend: &mut TuiBackend) -> std::io:: pub fn delete_selected_files( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, ) -> std::io::Result<()> { let _ = delete_files(context, backend)?; diff --git a/src/commands/numbered_command.rs b/src/commands/numbered_command.rs index c35205b..c1331da 100644 --- a/src/commands/numbered_command.rs +++ b/src/commands/numbered_command.rs @@ -8,12 +8,12 @@ use crate::event::process_event; use crate::event::AppEvent; use crate::key_command::{CommandKeybind, NumberedExecute}; use crate::ui::views::TuiView; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub fn numbered_command( first_char: char, context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap: &AppKeyMapping, ) -> JoshutoResult { context.flush_event(); diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index a2b157e..ad90a91 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -6,14 +6,14 @@ use crate::config::AppMimetypeEntry; use crate::context::AppContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; use crate::ui::views::TuiTextField; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use crate::util::process::{execute_and_wait, fork_execute}; use super::change_directory; use crate::MIMETYPE_T; -pub fn get_options<'a>(path: &path::Path) -> Vec<&'a AppMimetypeEntry> { +fn _get_options<'a>(path: &path::Path) -> Vec<&'a AppMimetypeEntry> { let mut options: Vec<&AppMimetypeEntry> = Vec::new(); if let Some(file_ext) = path.extension() { if let Some(file_ext) = file_ext.to_str() { @@ -24,47 +24,9 @@ pub fn get_options<'a>(path: &path::Path) -> Vec<&'a AppMimetypeEntry> { options } -pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { - let curr_list = context.tab_context_ref().curr_tab_ref().curr_list_ref(); - let entry = curr_list.and_then(|s| s.curr_entry_ref().cloned()); - - match entry { - None => (), - Some(entry) if entry.file_path().is_dir() => { - let path = entry.file_path().to_path_buf(); - change_directory::cd(path.as_path(), context)?; - reload::soft_reload(context.tab_context_ref().index, context)?; - } - Some(entry) => { - let paths = curr_list.map_or_else(Vec::new, |s| s.iter_selected().cloned().collect()); - let (path, files) = if paths.is_empty() { - (entry.file_path(), vec![entry.file_name()]) - } else { - ( - paths.get(0).unwrap().file_path(), - paths.iter().map(|e| e.file_name()).collect(), - ) - }; - let options = get_options(path); - let option = options.iter().find(|option| option.program_exists()); - - let config = context.config_ref(); - - if let Some(option) = option { - open_with_entry(context, backend, option, &files)?; - } else if config.xdg_open { - open_with_xdg(context, backend, path)?; - } else { - open_with_helper(context, backend, options, &files)?; - } - } - } - Ok(()) -} - -pub fn open_with_entry( +fn _open_with_entry( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, option: &AppMimetypeEntry, files: &[S], ) -> std::io::Result<()> @@ -82,9 +44,9 @@ where Ok(()) } -pub fn open_with_xdg( +fn _open_with_xdg( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, path: &path::Path, ) -> std::io::Result<()> { let config = context.config_ref(); @@ -100,9 +62,9 @@ pub fn open_with_xdg( Ok(()) } -pub fn open_with_helper( +fn _open_with_helper( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, options: Vec<&AppMimetypeEntry>, files: &[S], ) -> std::io::Result<()> @@ -139,7 +101,7 @@ where } Ok(n) => { let option = &options[n]; - open_with_entry(context, backend, option, files)?; + _open_with_entry(context, backend, option, files)?; } Err(_) => { let mut args_iter = user_input.split_whitespace(); @@ -159,34 +121,47 @@ where Ok(()) } -pub fn open_with_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { - let mut paths = context - .tab_context_ref() - .curr_tab_ref() - .curr_list_ref() - .map_or(vec![], |s| s.iter_selected().cloned().collect()); +pub fn open(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { + let curr_list = context.tab_context_ref().curr_tab_ref().curr_list_ref(); + let entry = curr_list.and_then(|s| s.curr_entry_ref().cloned()); - if paths.is_empty() { - paths.push( - context - .tab_context_ref() - .curr_tab_ref() - .curr_list_ref() - .and_then(|s| s.curr_entry_ref()) - .unwrap() - .clone(), - ); - } - let files: Vec<&str> = paths.iter().map(|e| e.file_name()).collect(); - let options = get_options(paths[0].file_path()); + match entry { + None => (), + Some(entry) if entry.file_path().is_dir() => { + let path = entry.file_path().to_path_buf(); + change_directory::cd(path.as_path(), context)?; + reload::soft_reload(context.tab_context_ref().index, context)?; + } + Some(entry) => { + let paths = curr_list.map_or_else(Vec::new, |s| s.iter_selected().cloned().collect()); + let (path, files) = if paths.is_empty() { + (entry.file_path(), vec![entry.file_name()]) + } else { + ( + paths.get(0).unwrap().file_path(), + paths.iter().map(|e| e.file_name()).collect(), + ) + }; + let options = _get_options(path); + let option = options.iter().find(|option| option.program_exists()); + + let config = context.config_ref(); - open_with_helper(context, backend, options, &files)?; + if let Some(option) = option { + _open_with_entry(context, backend, option, &files)?; + } else if config.xdg_open { + _open_with_xdg(context, backend, path)?; + } else { + _open_with_helper(context, backend, options, &files)?; + } + } + } Ok(()) } pub fn open_with_index( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, index: usize, ) -> JoshutoResult { let paths = context @@ -202,7 +177,7 @@ pub fn open_with_index( )); } let files: Vec<&str> = paths.iter().map(|e| e.file_name()).collect(); - let options = get_options(paths[0].file_path()); + let options = _get_options(paths[0].file_path()); if index >= options.len() { return Err(JoshutoError::new( @@ -212,6 +187,31 @@ pub fn open_with_index( } let option = &options[index]; - open_with_entry(context, backend, option, &files)?; + _open_with_entry(context, backend, option, &files)?; + Ok(()) +} + +pub fn open_with_interactive(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { + let mut paths = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map_or(vec![], |s| s.iter_selected().cloned().collect()); + + if paths.is_empty() { + paths.push( + context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .and_then(|s| s.curr_entry_ref()) + .unwrap() + .clone(), + ); + } + let files: Vec<&str> = paths.iter().map(|e| e.file_name()).collect(); + let options = _get_options(paths[0].file_path()); + + _open_with_helper(context, backend, options, &files)?; Ok(()) } diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index fa4a23d..cce7ab1 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -4,7 +4,7 @@ use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::history::create_dirlist_with_history; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use super::command_line; @@ -52,7 +52,7 @@ pub fn rename_file(context: &mut AppContext, dest: &path::Path) -> JoshutoResult pub fn _rename_file_append( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, file_name: &str, ) -> JoshutoResult { @@ -68,7 +68,7 @@ pub fn _rename_file_append( pub fn rename_file_append( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { let mut file_name: Option = None; @@ -87,7 +87,7 @@ pub fn rename_file_append( pub fn _rename_file_prepend( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, file_name: String, ) -> JoshutoResult { @@ -98,7 +98,7 @@ pub fn _rename_file_prepend( pub fn rename_file_prepend( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { let file_name = context diff --git a/src/commands/search_fzf.rs b/src/commands/search_fzf.rs index 0ae9fae..056fd10 100644 --- a/src/commands/search_fzf.rs +++ b/src/commands/search_fzf.rs @@ -5,9 +5,9 @@ use std::process::{Command, Stdio}; use crate::commands::cursor_move; use crate::context::AppContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; -pub fn search_fzf(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn search_fzf(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { let items = context .tab_context_ref() .curr_tab_ref() diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index 2691483..d62d8a4 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -3,7 +3,7 @@ use std::fs; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::ui::views::TuiTextField; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use crate::util::unix; use super::cursor_move; @@ -35,7 +35,7 @@ pub fn str_to_mode(s: &str) -> u32 { mode } -pub fn set_mode(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn set_mode(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { #[cfg(unix)] use std::os::unix::fs::PermissionsExt; diff --git a/src/commands/show_help.rs b/src/commands/show_help.rs index 739c63b..a8a7a4b 100644 --- a/src/commands/show_help.rs +++ b/src/commands/show_help.rs @@ -10,11 +10,11 @@ use crate::event::AppEvent; use crate::key_command::{Command, CommandKeybind}; use crate::ui::widgets; use crate::ui::widgets::TuiHelp; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub fn help_loop( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { context.flush_event(); diff --git a/src/commands/show_tasks.rs b/src/commands/show_tasks.rs index 4b22860..64312a1 100644 --- a/src/commands/show_tasks.rs +++ b/src/commands/show_tasks.rs @@ -5,12 +5,12 @@ use crate::event::process_event; use crate::event::AppEvent; use crate::key_command::{Command, CommandKeybind}; use crate::ui::views::TuiWorkerView; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use crate::util::to_string::ToString; pub fn show_tasks( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { context.flush_event(); diff --git a/src/commands/sub_process.rs b/src/commands/sub_process.rs index e3e2e5e..e2a33dc 100644 --- a/src/commands/sub_process.rs +++ b/src/commands/sub_process.rs @@ -1,6 +1,6 @@ use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use std::process::{Command, Stdio}; use super::reload; @@ -49,7 +49,7 @@ fn execute_sub_process( /// Handler for Joshuto's `shell` and `spawn` commands. pub fn sub_process( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, words: &[String], spawn: bool, ) -> JoshutoResult { diff --git a/src/commands/subdir_fzf.rs b/src/commands/subdir_fzf.rs index d1adc07..0944915 100644 --- a/src/commands/subdir_fzf.rs +++ b/src/commands/subdir_fzf.rs @@ -5,11 +5,11 @@ use std::process::{Command, Stdio}; use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use super::change_directory::change_directory; -pub fn subdir_fzf(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn subdir_fzf(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { let fzf_default_command = std::env::var("FZF_DEFAULT_COMMAND")?; backend.terminal_drop(); diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs index 9ee1bf6..0725618 100644 --- a/src/commands/zoxide.rs +++ b/src/commands/zoxide.rs @@ -5,7 +5,7 @@ use std::process::{Command, Stdio}; use crate::commands::change_directory; use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub fn zoxide_query(context: &mut AppContext, args: &str) -> JoshutoResult { let cwd = std::env::current_dir()?; @@ -39,7 +39,7 @@ pub fn zoxide_query(context: &mut AppContext, args: &str) -> JoshutoResult { pub fn zoxide_query_interactive( context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, ) -> JoshutoResult { backend.terminal_drop(); diff --git a/src/event/process_event.rs b/src/event/process_event.rs index aa4cb24..bf587a3 100644 --- a/src/event/process_event.rs +++ b/src/event/process_event.rs @@ -19,7 +19,7 @@ use crate::ui::views::TuiCommandMenu; use crate::util::format; pub fn get_input_while_composite<'a>( - backend: &mut ui::TuiBackend, + backend: &mut ui::AppBackend, context: &mut AppContext, keymap: &'a KeyMapping, ) -> Option<&'a Command> { @@ -151,7 +151,7 @@ pub fn process_file_preview( pub fn process_unsupported( context: &mut AppContext, - backend: &mut ui::TuiBackend, + backend: &mut ui::AppBackend, keymap_t: &AppKeyMapping, event: Vec, ) { @@ -175,7 +175,7 @@ pub fn process_unsupported( pub fn process_mouse( event: MouseEvent, context: &mut AppContext, - backend: &mut ui::TuiBackend, + backend: &mut ui::AppBackend, keymap_t: &AppKeyMapping, ) { let f_size = backend.terminal.as_ref().unwrap().size().unwrap(); diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index 3f88f4b..4febf1e 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -2,7 +2,7 @@ use crate::commands::*; use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use super::{AppExecute, Command}; @@ -10,7 +10,7 @@ impl AppExecute for Command { fn execute( &self, context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { match &*self { diff --git a/src/key_command/impl_numbered.rs b/src/key_command/impl_numbered.rs index 2a0007c..d1f05f0 100644 --- a/src/key_command/impl_numbered.rs +++ b/src/key_command/impl_numbered.rs @@ -2,7 +2,7 @@ use crate::commands::*; use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; use super::{Command, NumberedExecute}; @@ -15,7 +15,7 @@ impl NumberedExecute for Command { &self, number_prefix: usize, context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult { match self { diff --git a/src/key_command/traits.rs b/src/key_command/traits.rs index 08f6c0d..647ad3c 100644 --- a/src/key_command/traits.rs +++ b/src/key_command/traits.rs @@ -1,13 +1,13 @@ use crate::config::AppKeyMapping; use crate::context::AppContext; use crate::error::JoshutoResult; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub trait AppExecute { fn execute( &self, context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult; } @@ -17,7 +17,7 @@ pub trait NumberedExecute { &self, number_prefix: usize, context: &mut AppContext, - backend: &mut TuiBackend, + backend: &mut AppBackend, keymap_t: &AppKeyMapping, ) -> JoshutoResult; } diff --git a/src/main.rs b/src/main.rs index d809b58..f7795a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ fn run_joshuto(args: Args) -> Result { let mut context = AppContext::new(config, args.clone()); { - let mut backend: ui::TuiBackend = ui::TuiBackend::new()?; + let mut backend: ui::AppBackend = ui::AppBackend::new()?; run(&mut backend, &mut context, keymap)?; } run_quit(&args, &context)?; diff --git a/src/run.rs b/src/run.rs index 145e6bc..7ecd694 100644 --- a/src/run.rs +++ b/src/run.rs @@ -16,11 +16,17 @@ use termion::event::{Event, Key}; use tui::layout::Rect; pub fn run( - backend: &mut ui::TuiBackend, + backend: &mut ui::AppBackend, context: &mut AppContext, keymap_t: AppKeyMapping, ) -> std::io::Result<()> { let curr_path = std::env::current_dir()?; + + if let Ok(area) = backend.terminal_ref().size() { + // pre-calculate some ui attributes + calculate_ui_context(context, area); + } + { // Initialize an initial tab let tab = JoshutoTab::new( diff --git a/src/ui/backend.rs b/src/ui/backend.rs new file mode 100644 index 0000000..8180072 --- /dev/null +++ b/src/ui/backend.rs @@ -0,0 +1,87 @@ +use std::io::{self, stdout, Write}; + +use termion::raw::{IntoRawMode, RawTerminal}; +use termion::screen::AlternateScreen; +use tui::backend::TermionBackend; +use tui::widgets::Widget; + +#[cfg(feature = "mouse")] +use termion::input::MouseTerminal; + +trait New { + fn new() -> std::io::Result + where + Self: Sized; +} + +#[cfg(feature = "mouse")] +type Screen = MouseTerminal>>; +#[cfg(feature = "mouse")] +impl New for Screen { + fn new() -> std::io::Result { + let stdout = std::io::stdout().into_raw_mode()?; + let alt_screen = MouseTerminal::from(AlternateScreen::from(stdout)); + return Ok(alt_screen); + } +} +#[cfg(not(feature = "mouse"))] +type Screen = AlternateScreen>; +#[cfg(not(feature = "mouse"))] +impl New for Screen { + fn new() -> io::Result { + let stdout = std::io::stdout().into_raw_mode()?; + let alt_screen = AlternateScreen::from(stdout); + Ok(alt_screen) + } +} + +// pub type TuiBackend = TermionBackend; +pub type TuiTerminal = tui::Terminal>; + +pub struct AppBackend { + pub terminal: Option, +} + +impl AppBackend { + pub fn new() -> io::Result { + let mut alt_screen = Screen::new()?; + // clears the screen of artifacts + write!(alt_screen, "{}", termion::clear::All)?; + + let backend = TermionBackend::new(alt_screen); + let mut terminal = tui::Terminal::new(backend)?; + terminal.hide_cursor()?; + Ok(Self { + terminal: Some(terminal), + }) + } + + pub fn render(&mut self, widget: W) + where + W: Widget, + { + let _ = self.terminal_mut().draw(|frame| { + let rect = frame.size(); + frame.render_widget(widget, rect); + }); + } + + pub fn terminal_ref(&self) -> &TuiTerminal { + self.terminal.as_ref().unwrap() + } + + pub fn terminal_mut(&mut self) -> &mut TuiTerminal { + self.terminal.as_mut().unwrap() + } + + pub fn terminal_drop(&mut self) { + let _ = self.terminal.take(); + let _ = stdout().flush(); + } + + pub fn terminal_restore(&mut self) -> io::Result<()> { + let mut new_backend = Self::new()?; + std::mem::swap(&mut self.terminal, &mut new_backend.terminal); + Ok(()) + } +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 2b19584..709e9a4 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,10 +1,10 @@ +mod backend; mod preview_area; mod rect; -mod tui_backend; pub mod views; pub mod widgets; +pub use backend::*; pub use preview_area::*; pub use rect::*; -pub use tui_backend::*; diff --git a/src/ui/tui_backend.rs b/src/ui/tui_backend.rs deleted file mode 100644 index c88590b..0000000 --- a/src/ui/tui_backend.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::io::stdout; -use std::io::Write; - -use termion::raw::{IntoRawMode, RawTerminal}; -use termion::screen::AlternateScreen; -use tui::backend::TermionBackend; -use tui::widgets::Widget; - -#[cfg(feature = "mouse")] -use termion::input::MouseTerminal; - -trait New { - fn new() -> std::io::Result - where - Self: Sized; -} - -#[cfg(feature = "mouse")] -type Screen = MouseTerminal>>; -#[cfg(feature = "mouse")] -impl New for Screen { - fn new() -> std::io::Result { - let stdout = std::io::stdout().into_raw_mode()?; - let alt_screen = MouseTerminal::from(AlternateScreen::from(stdout)); - return Ok(alt_screen); - } -} -#[cfg(not(feature = "mouse"))] -type Screen = AlternateScreen>; -#[cfg(not(feature = "mouse"))] -impl New for Screen { - fn new() -> std::io::Result { - let stdout = std::io::stdout().into_raw_mode()?; - let alt_screen = AlternateScreen::from(stdout); - Ok(alt_screen) - } -} - -pub type JoshutoTerminal = tui::Terminal>; - -pub struct TuiBackend { - pub terminal: Option, -} - -impl TuiBackend { - pub fn new() -> std::io::Result { - let mut alt_screen = Screen::new()?; - // clears the screen of artifacts - write!(alt_screen, "{}", termion::clear::All)?; - - let backend = TermionBackend::new(alt_screen); - let mut terminal = tui::Terminal::new(backend)?; - terminal.hide_cursor()?; - Ok(Self { - terminal: Some(terminal), - }) - } - - pub fn render(&mut self, widget: W) - where - W: Widget, - { - let _ = self.terminal_mut().draw(|frame| { - let rect = frame.size(); - frame.render_widget(widget, rect); - }); - } - - pub fn terminal_ref(&self) -> &JoshutoTerminal { - self.terminal.as_ref().unwrap() - } - - pub fn terminal_mut(&mut self) -> &mut JoshutoTerminal { - self.terminal.as_mut().unwrap() - } - - pub fn terminal_drop(&mut self) { - let _ = self.terminal.take(); - let _ = stdout().flush(); - } - - pub fn terminal_restore(&mut self) -> std::io::Result<()> { - let mut new_backend = TuiBackend::new()?; - std::mem::swap(&mut self.terminal, &mut new_backend.terminal); - Ok(()) - } -} diff --git a/src/ui/views/tui_folder_view.rs b/src/ui/views/tui_folder_view.rs index 986612f..d84dde1 100644 --- a/src/ui/views/tui_folder_view.rs +++ b/src/ui/views/tui_folder_view.rs @@ -217,6 +217,11 @@ impl Intersections { } pub fn get_constraints(context: &AppContext) -> &[Constraint; 3] { + let display_options = context.config_ref().display_options_ref(); + if context.tab_context_ref().len() == 0 { + return &display_options.default_layout + } + let preview_context = context.preview_context_ref(); let curr_tab = context.tab_context_ref().curr_tab_ref(); @@ -225,9 +230,6 @@ pub fn get_constraints(context: &AppContext) -> &[Constraint; 3] { let child_list = curr_tab.child_list_ref(); - let config = context.config_ref(); - let display_options = config.display_options_ref(); - if !display_options.collapse_preview() { &display_options.default_layout } else { diff --git a/src/ui/views/tui_textfield.rs b/src/ui/views/tui_textfield.rs index 045def9..15a8fcf 100644 --- a/src/ui/views/tui_textfield.rs +++ b/src/ui/views/tui_textfield.rs @@ -17,7 +17,7 @@ use crate::event::AppEvent; use crate::key_command::{complete_command, Command, InteractiveExecute}; use crate::ui::views::TuiView; use crate::ui::widgets::{TuiMenu, TuiMultilineText}; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; struct CompletionTracker { pub index: usize, @@ -76,7 +76,7 @@ impl<'a> TuiTextField<'a> { pub fn get_input( &mut self, - backend: &mut TuiBackend, + backend: &mut AppBackend, context: &mut AppContext, ) -> Option { let mut line_buffer = line_buffer::LineBuffer::with_capacity(255); diff --git a/src/ui/widgets/tui_prompt.rs b/src/ui/widgets/tui_prompt.rs index 2a3c8f9..9ee2573 100644 --- a/src/ui/widgets/tui_prompt.rs +++ b/src/ui/widgets/tui_prompt.rs @@ -8,7 +8,7 @@ use crate::context::AppContext; use crate::event::process_event; use crate::event::AppEvent; use crate::ui::views::TuiView; -use crate::ui::TuiBackend; +use crate::ui::AppBackend; pub struct TuiPrompt<'a> { prompt: &'a str, @@ -19,7 +19,7 @@ impl<'a> TuiPrompt<'a> { Self { prompt } } - pub fn get_key(&mut self, backend: &mut TuiBackend, context: &mut AppContext) -> Key { + pub fn get_key(&mut self, backend: &mut AppBackend, context: &mut AppContext) -> Key { let terminal = backend.terminal_mut(); context.flush_event(); -- cgit v1.2.3