diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-06-10 22:20:52 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-06-10 22:20:52 -0400 |
commit | 92bcbb950cb1f2167279ee331f924799c3498c14 (patch) | |
tree | bcdfb22c9ee64a6afdc084903f39b0f9947ed2fc /src | |
parent | 1ae0e3d7b0eb4b08e75c4d31cfe43a4382729562 (diff) |
add support for specifying new tab home page
- inherit, start where preview tab started
- home, start in home dir
- root, start in root dir
- add rudimentary support for preview
- add preview script configuration
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/tab_ops.rs | 21 | ||||
-rw-r--r-- | src/config/default/config.rs | 35 | ||||
-rw-r--r-- | src/config/default/mod.rs | 4 | ||||
-rw-r--r-- | src/config/default/preview.rs | 62 | ||||
-rw-r--r-- | src/config/default/tab.rs | 57 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/preview/mod.rs | 3 | ||||
-rw-r--r-- | src/preview/preview_sh.rs | 15 | ||||
-rw-r--r-- | src/tab.rs | 7 |
9 files changed, 190 insertions, 15 deletions
diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs index d3965ea..f777df4 100644 --- a/src/commands/tab_ops.rs +++ b/src/commands/tab_ops.rs @@ -3,7 +3,7 @@ use std::path; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::history::DirectoryHistory; -use crate::tab::JoshutoTab; +use crate::tab::{JoshutoTab, TabHomePage}; use crate::util::load_child::LoadChild; use crate::HOME_DIR; @@ -51,14 +51,21 @@ pub fn tab_switch(offset: i32, context: &mut AppContext) -> std::io::Result<()> _tab_switch(new_index, context) } +pub fn new_tab_home_path(context: &AppContext) -> path::PathBuf { + match context.config_ref().tab_options_ref().home_page() { + TabHomePage::Home => match HOME_DIR.as_ref() { + Some(s) => s.clone(), + None => path::PathBuf::from("/"), + }, + TabHomePage::Inherit => context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(), + TabHomePage::Root => path::PathBuf::from("/"), + } +} + pub fn new_tab(context: &mut AppContext) -> JoshutoResult<()> { - /* start the new tab in $HOME or root */ - let curr_path = match HOME_DIR.as_ref() { - Some(s) => s.clone(), - None => path::PathBuf::from("/"), - }; + let new_tab_path = new_tab_home_path(context); - let tab = JoshutoTab::new(curr_path, context.config_ref().display_options_ref())?; + let tab = JoshutoTab::new(new_tab_path, context.config_ref().display_options_ref())?; context.tab_context_mut().push_tab(tab); let new_index = context.tab_context_ref().len() - 1; context.tab_context_mut().index = new_index; diff --git a/src/config/default/config.rs b/src/config/default/config.rs index 3dad86d..6559c48 100644 --- a/src/config/default/config.rs +++ b/src/config/default/config.rs @@ -1,6 +1,9 @@ use serde_derive::Deserialize; +use super::preview::{PreviewOption, PreviewRawOption}; +use super::tab::{TabOption, TabRawOption}; use super::DisplayRawOption; + use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; use crate::util::display::DisplayOption; use crate::util::sort; @@ -11,9 +14,6 @@ const fn default_true() -> bool { const fn default_scroll_offset() -> usize { 6 } -const fn default_max_preview_size() -> u64 { - 2 * 1024 * 1024 // 2 MB -} #[derive(Clone, Debug, Deserialize)] pub struct RawAppConfig { @@ -23,31 +23,35 @@ pub struct RawAppConfig { use_trash: bool, #[serde(default)] xdg_open: bool, - #[serde(default = "default_max_preview_size")] - max_preview_size: u64, #[serde(default, rename = "display")] display_options: DisplayRawOption, + #[serde(default, rename = "preview")] + preview_options: PreviewRawOption, + #[serde(default, rename = "tab")] + tab_options: TabRawOption, } impl Flattenable<AppConfig> for RawAppConfig { fn flatten(self) -> AppConfig { AppConfig { - max_preview_size: self.max_preview_size, scroll_offset: self.scroll_offset, use_trash: self.use_trash, xdg_open: self.xdg_open, _display_options: self.display_options.flatten(), + _preview_options: self.preview_options.flatten(), + _tab_options: self.tab_options.flatten(), } } } #[derive(Debug, Clone)] pub struct AppConfig { - pub max_preview_size: u64, pub scroll_offset: usize, pub use_trash: bool, pub xdg_open: bool, _display_options: DisplayOption, + _preview_options: PreviewOption, + _tab_options: TabOption, } impl AppConfig { @@ -58,12 +62,26 @@ impl AppConfig { &mut self._display_options } + pub fn preview_options_ref(&self) -> &PreviewOption { + &self._preview_options + } + pub fn preview_options_mut(&mut self) -> &mut PreviewOption { + &mut self._preview_options + } + pub fn sort_options_ref(&self) -> &sort::SortOption { self.display_options_ref().sort_options_ref() } pub fn sort_options_mut(&mut self) -> &mut sort::SortOption { self.display_options_mut().sort_options_mut() } + + pub fn tab_options_ref(&self) -> &TabOption { + &self._tab_options + } + pub fn tab_options_mut(&mut self) -> &mut TabOption { + &mut self._tab_options + } } impl ConfigStructure for AppConfig { @@ -75,11 +93,12 @@ impl ConfigStructure for AppConfig { impl std::default::Default for AppConfig { fn default() -> Self { Self { - max_preview_size: default_max_preview_size(), scroll_offset: default_scroll_offset(), use_trash: true, xdg_open: false, _display_options: DisplayOption::default(), + _preview_options: PreviewOption::default(), + _tab_options: TabOption::default(), } } } diff --git a/src/config/default/mod.rs b/src/config/default/mod.rs index 0330e63..dc8617c 100644 --- a/src/config/default/mod.rs +++ b/src/config/default/mod.rs @@ -1,7 +1,11 @@ pub mod config; pub mod display; +pub mod preview; pub mod sort; +pub mod tab; pub use self::config::AppConfig; pub use self::display::DisplayRawOption; +pub use self::preview::{PreviewOption, PreviewRawOption}; pub use self::sort::SortRawOption; +pub use self::tab::{TabOption, TabRawOption}; diff --git a/src/config/default/preview.rs b/src/config/default/preview.rs new file mode 100644 index 0000000..2fd3323 --- /dev/null +++ b/src/config/default/preview.rs @@ -0,0 +1,62 @@ +use std::path; + +use serde_derive::Deserialize; + +use crate::config::{search_directories, Flattenable}; +use crate::CONFIG_HIERARCHY; + +const fn default_max_preview_size() -> u64 { + 2 * 1024 * 1024 // 2 MB +} + +#[derive(Clone, Debug, Deserialize)] +pub struct PreviewRawOption { + #[serde(default = "default_max_preview_size")] + max_preview_size: u64, + #[serde(default)] + preview_images: bool, + #[serde(default)] + preview_script: Option<String>, +} + +impl std::default::Default for PreviewRawOption { + fn default() -> Self { + Self { + max_preview_size: default_max_preview_size(), + preview_images: false, + preview_script: None, + } + } +} + +impl Flattenable<PreviewOption> for PreviewRawOption { + fn flatten(self) -> PreviewOption { + let preview_script = match self.preview_script { + Some(s) => Some(path::PathBuf::from(s)), + None => search_directories("preview.sh", &CONFIG_HIERARCHY), + }; + + PreviewOption { + max_preview_size: self.max_preview_size, + preview_images: self.preview_images, + preview_script, + } + } +} + +#[derive(Clone, Debug)] +pub struct PreviewOption { + pub max_preview_size: u64, + pub preview_images: bool, + pub preview_script: Option<path::PathBuf>, +} + +impl std::default::Default for PreviewOption { + fn default() -> Self { + Self { + max_preview_size: default_max_preview_size(), + preview_images: false, + preview_script: None, + } + } +} diff --git a/src/config/default/tab.rs b/src/config/default/tab.rs new file mode 100644 index 0000000..1cd6a88 --- /dev/null +++ b/src/config/default/tab.rs @@ -0,0 +1,57 @@ +use serde_derive::Deserialize; + +use crate::config::Flattenable; +use crate::tab::TabHomePage; + +fn default_home_page() -> String { + "home".to_string() +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TabRawOption { + #[serde(default = "default_home_page")] + home_page: String, +} + +impl std::default::Default for TabRawOption { + fn default() -> Self { + Self { + home_page: default_home_page(), + } + } +} + +impl Flattenable<TabOption> for TabRawOption { + fn flatten(self) -> TabOption { + let home_page = match self.home_page.as_str() { + "inherit" => TabHomePage::Inherit, + "home" => TabHomePage::Home, + "root" => TabHomePage::Root, + _ => TabHomePage::Home, + }; + + TabOption::new(home_page) + } +} + +#[derive(Clone, Debug)] +pub struct TabOption { + _home_page: TabHomePage, +} + +impl TabOption { + pub fn new(_home_page: TabHomePage) -> Self { + Self { _home_page } + } + pub fn home_page(&self) -> TabHomePage { + self._home_page + } +} + +impl std::default::Default for TabOption { + fn default() -> Self { + Self { + _home_page: TabHomePage::Home, + } + } +} diff --git a/src/main.rs b/src/main.rs index a3e0f36..d1a2026 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod error; mod fs; mod history; mod io; +mod preview; mod run; mod tab; mod ui; diff --git a/src/preview/mod.rs b/src/preview/mod.rs new file mode 100644 index 0000000..6a3e7fd --- /dev/null +++ b/src/preview/mod.rs @@ -0,0 +1,3 @@ +pub mod preview_sh; + +pub use self::preview_sh::preview_with_script; diff --git a/src/preview/preview_sh.rs b/src/preview/preview_sh.rs new file mode 100644 index 0000000..9d66ab4 --- /dev/null +++ b/src/preview/preview_sh.rs @@ -0,0 +1,15 @@ +use crate::context::AppContext; +use crate::ui::TuiBackend; + +pub fn preview_with_script(context: &AppContext, backend: &mut TuiBackend) { + let preview_options = context.config_ref().preview_options_ref(); + if let Some(script_path) = preview_options.preview_script.as_ref() { + let file_full_path = 0; + let preview_width = 0; + let preview_height = 0; + let image_cache = 0; + let preview_image = if preview_options.preview_images { 1 } else { 0 }; + + // spawn preview process + } +} @@ -4,6 +4,13 @@ use crate::fs::JoshutoDirList; use crate::history::{DirectoryHistory, JoshutoHistory}; use crate::util::display::DisplayOption; +#[derive(Clone, Copy, Debug)] +pub enum TabHomePage { + Inherit, + Home, + Root, +} + pub struct JoshutoTab { history: JoshutoHistory, _cwd: path::PathBuf, |