diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-20 15:52:41 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-20 15:52:41 -0400 |
commit | 6e2164aeaaf02bf61099e773a0341a38652c5da4 (patch) | |
tree | 69c90c589119b1058097993f3a0221aba6af4100 | |
parent | d151d64809f918c59b4cd5c18ce71d05b165cd84 (diff) |
rework config structure
34 files changed, 453 insertions, 458 deletions
diff --git a/config/joshuto.toml b/config/joshuto.toml index a3b797c..4b8616a 100644 --- a/config/joshuto.toml +++ b/config/joshuto.toml @@ -1,6 +1,3 @@ -# currently does not work -scroll_offset = 6 - xdg_open = false use_trash = true @@ -9,6 +6,7 @@ automatically_count_files = false collapse_preview = true # ratios for parent view, current view and preview column_ratio = [1, 4, 4] +scroll_offset = 6 show_borders = true show_hidden = false show_icons = true diff --git a/src/commands/sort.rs b/src/commands/sort.rs index 7102fc3..ba9f103 100644 --- a/src/commands/sort.rs +++ b/src/commands/sort.rs @@ -1,7 +1,7 @@ +use crate::config::option::SortType; use crate::context::AppContext; use crate::error::JoshutoResult; use crate::history::DirectoryHistory; -use crate::util::sort_type::SortType; use super::reload; diff --git a/src/config/general/app.rs b/src/config/general/app.rs new file mode 100644 index 0000000..5c3c3cf --- /dev/null +++ b/src/config/general/app.rs @@ -0,0 +1,52 @@ +use serde_derive::Deserialize; + +use crate::config::option::{DisplayOption, PreviewOption, SortOption, TabOption}; +use crate::config::{parse_to_config_file, TomlConfigFile}; + +#[derive(Debug, Clone)] +pub struct AppConfig { + pub use_trash: bool, + pub xdg_open: bool, + pub _display_options: DisplayOption, + pub _preview_options: PreviewOption, + pub _tab_options: TabOption, +} + +impl AppConfig { + pub fn display_options_ref(&self) -> &DisplayOption { + &self._display_options + } + pub fn display_options_mut(&mut self) -> &mut DisplayOption { + &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) -> &SortOption { + self.display_options_ref().sort_options_ref() + } + pub fn sort_options_mut(&mut self) -> &mut SortOption { + self.display_options_mut().sort_options_mut() + } + + pub fn tab_options_ref(&self) -> &TabOption { + &self._tab_options + } +} + +impl std::default::Default for AppConfig { + fn default() -> Self { + Self { + use_trash: true, + xdg_open: false, + _display_options: DisplayOption::default(), + _preview_options: PreviewOption::default(), + _tab_options: TabOption::default(), + } + } +} diff --git a/src/config/general/app_crude.rs b/src/config/general/app_crude.rs new file mode 100644 index 0000000..d24eff9 --- /dev/null +++ b/src/config/general/app_crude.rs @@ -0,0 +1,51 @@ +use std::convert::From; + +use serde_derive::Deserialize; + +use crate::config::option::{DisplayOption, PreviewOption, TabOption}; +use crate::config::{parse_to_config_file, AppConfig, TomlConfigFile}; + +use super::display_crude::DisplayOptionCrude; +use super::preview_crude::PreviewOptionCrude; +use super::tab_crude::TabOptionCrude; + +const fn default_true() -> bool { + true +} +const fn default_scroll_offset() -> usize { + 6 +} + +#[derive(Clone, Debug, Deserialize)] +pub struct AppConfigCrude { + #[serde(default = "default_scroll_offset")] + pub scroll_offset: usize, + #[serde(default = "default_true")] + pub use_trash: bool, + #[serde(default)] + pub xdg_open: bool, + #[serde(default, rename = "display")] + pub display_options: DisplayOptionCrude, + #[serde(default, rename = "preview")] + pub preview_options: PreviewOptionCrude, + #[serde(default, rename = "tab")] + pub tab_options: TabOptionCrude, +} + +impl From<AppConfigCrude> for AppConfig { + fn from(crude: AppConfigCrude) -> Self { + Self { + use_trash: crude.use_trash, + xdg_open: crude.xdg_open, + _display_options: DisplayOption::from(crude.display_options), + _preview_options: PreviewOption::from(crude.preview_options), + _tab_options: TabOption::from(crude.tab_options), + } + } +} + +impl TomlConfigFile for AppConfig { + fn get_config(file_name: &str) -> Self { + parse_to_config_file::<AppConfigCrude, AppConfig>(file_name).unwrap_or_else(Self::default) + } +} diff --git a/src/config/general/config.rs b/src/config/general/config.rs deleted file mode 100644 index 3196017..0000000 --- a/src/config/general/config.rs +++ /dev/null @@ -1,101 +0,0 @@ -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_option::DisplayOption; -use crate::util::sort_option::SortOption; - -const fn default_true() -> bool { - true -} -const fn default_scroll_offset() -> usize { - 6 -} - -#[derive(Clone, Debug, Deserialize)] -pub struct RawAppConfig { - #[serde(default = "default_scroll_offset")] - scroll_offset: usize, - #[serde(default = "default_true")] - use_trash: bool, - #[serde(default)] - xdg_open: bool, - #[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 { - 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 scroll_offset: usize, - pub use_trash: bool, - pub xdg_open: bool, - _display_options: DisplayOption, - _preview_options: PreviewOption, - _tab_options: TabOption, -} - -impl AppConfig { - pub fn display_options_ref(&self) -> &DisplayOption { - &self._display_options - } - pub fn display_options_mut(&mut self) -> &mut DisplayOption { - &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) -> &SortOption { - self.display_options_ref().sort_options_ref() - } - pub fn sort_options_mut(&mut self) -> &mut SortOption { - self.display_options_mut().sort_options_mut() - } - - pub fn tab_options_ref(&self) -> &TabOption { - &self._tab_options - } -} - -impl ConfigStructure for AppConfig { - fn get_config(file_name: &str) -> Self { - parse_to_config_file::<RawAppConfig, AppConfig>(file_name).unwrap_or_else(Self::default) - } -} - -impl std::default::Default for AppConfig { - fn default() -> Self { - Self { - 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/general/display.rs b/src/config/general/display_crude.rs index 7ca6456..ba3c307 100644 --- a/src/config/general/display.rs +++ b/src/config/general/display_crude.rs @@ -1,48 +1,77 @@ +use std::convert::From; + use serde_derive::Deserialize; use tui::layout::Constraint; -use crate::config::Flattenable; -use crate::util::display_option::{default_column_ratio, DisplayOption}; +use crate::config::option::DisplayOption; + +use super::sort_crude::SortOptionCrude; -use super::SortRawOption; +pub const fn default_column_ratio() -> (usize, usize, usize) { + (1, 3, 4) +} const fn default_true() -> bool { true } +const fn default_scroll_offset() -> usize { + 4 +} + #[derive(Clone, Debug, Deserialize)] -pub struct DisplayRawOption { +pub struct DisplayOptionCrude { #[serde(default)] - automatically_count_files: bool, + pub automatically_count_files: bool, #[serde(default = "default_true")] - collapse_preview: bool, + pub collapse_preview: bool, #[serde(default)] - column_ratio: Option<[usize; 3]>, + pub column_ratio: Option<[usize; 3]>, + + #[serde(default = "default_scroll_offset")] + pub scroll_offset: usize, #[serde(default = "default_true")] - show_borders: bool, + pub show_borders: bool, #[serde(default)] - show_hidden: bool, + pub show_hidden: bool, #[serde(default)] - show_icons: bool, + pub show_icons: bool, #[serde(default = "default_true")] - show_preview: bool, + pub show_preview: bool, #[serde(default = "default_true")] - tilde_in_titlebar: bool, + pub tilde_in_titlebar: bool, #[serde(default, rename = "sort")] - sort_options: SortRawOption, + pub sort_options: SortOptionCrude, +} + +impl std::default::Default for DisplayOptionCrude { + fn default() -> Self { + Self { + automatically_count_files: false, + collapse_preview: true, + column_ratio: None, + scroll_offset: 4, + show_borders: true, + show_hidden: false, + show_icons: false, + show_preview: true, + sort_options: SortOptionCrude::default(), + tilde_in_titlebar: true, + } + } } -impl Flattenable<DisplayOption> for DisplayRawOption { - fn flatten(self) -> DisplayOption { - let column_ratio = match self.column_ratio { +impl From<DisplayOptionCrude> for DisplayOption { + fn from(crude: DisplayOptionCrude) -> Self { + let column_ratio = match crude.column_ratio { Some(s) => (s[0], s[1], s[2]), _ => default_column_ratio(), }; @@ -60,34 +89,20 @@ impl Flattenable<DisplayOption> for DisplayRawOption { Constraint::Ratio(0, total), ]; - DisplayOption { - _automatically_count_files: self.automatically_count_files, - _collapse_preview: self.collapse_preview, + Self { + _automatically_count_files: crude.automatically_count_files, + _collapse_preview: crude.collapse_preview, + _scroll_offset: crude.scroll_offset, + _show_borders: crude.show_borders, + _show_hidden: crude.show_hidden, + _show_icons: crude.show_icons, + _show_preview: crude.show_preview, + _sort_options: crude.sort_options.into(), + _tilde_in_titlebar: crude.tilde_in_titlebar, + column_ratio, - _show_borders: self.show_borders, - _show_hidden: self.show_hidden, - _show_icons: self.show_icons, - _show_preview: self.show_preview, - _sort_options: self.sort_options.into(), - _tilde_in_titlebar: self.tilde_in_titlebar, default_layout, no_preview_layout, } } } - -impl std::default::Default for DisplayRawOption { - fn default() -> Self { - Self { - automatically_count_files: false, - collapse_preview: true, - column_ratio: None, - show_borders: true, - show_hidden: false, - show_icons: false, - show_preview: true, - sort_options: SortRawOption::default(), - tilde_in_titlebar: true, - } - } -} diff --git a/src/config/general/mod.rs b/src/config/general/mod.rs index dc8617c..ff42b69 100644 --- a/src/config/general/mod.rs +++ b/src/config/general/mod.rs @@ -1,11 +1,9 @@ -pub mod config; -pub mod display; -pub mod preview; -pub mod sort; -pub mod tab; +pub mod app; -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}; +mod app_crude; +mod display_crude; +mod preview_crude; +mod sort_crude; +mod tab_crude; + +pub use self::app::AppConfig; diff --git a/src/config/general/preview.rs b/src/config/general/preview_crude.rs index 5d9436f..0d9c670 100644 --- a/src/config/general/preview.rs +++ b/src/config/general/preview_crude.rs @@ -1,25 +1,27 @@ +use std::convert::From; use std::path; use serde_derive::Deserialize; -use crate::config::{search_directories, Flattenable}; +use crate::config::option::PreviewOption; +use crate::config::search_directories; use crate::CONFIG_HIERARCHY; -const fn default_max_preview_size() -> u64 { +pub const fn default_max_preview_size() -> u64 { 2 * 1024 * 1024 // 2 MB } #[derive(Clone, Debug, Deserialize)] -pub struct PreviewRawOption { +pub struct PreviewOptionCrude { #[serde(default = "default_max_preview_size")] - max_preview_size: u64, + pub max_preview_size: u64, #[serde(default)] - preview_images: bool, + pub preview_images: bool, #[serde(default)] - preview_script: Option<String>, + pub preview_script: Option<String>, } -impl std::default::Default for PreviewRawOption { +impl std::default::Default for PreviewOptionCrude { fn default() -> Self { Self { max_preview_size: default_max_preview_size(), @@ -29,9 +31,9 @@ impl std::default::Default for PreviewRawOption { } } -impl Flattenable<PreviewOption> for PreviewRawOption { - fn flatten(self) -> PreviewOption { - let preview_script = match self.preview_script { +impl From<PreviewOptionCrude> for PreviewOption { + fn from(crude: PreviewOptionCrude) -> Self { + let preview_script = match crude.preview_script { Some(s) => { let tilde_cow = shellexpand::tilde_with_context(s.as_str(), dirs_next::home_dir); let tilde_path = path::PathBuf::from(tilde_cow.as_ref()); @@ -40,27 +42,10 @@ impl Flattenable<PreviewOption> for PreviewRawOption { 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, + max_preview_size: crude.max_preview_size, + preview_images: crude.preview_images, + preview_script, } } } diff --git a/src/config/general/sort.rs b/src/config/general/sort_crude.rs index fea3db2..f7a1836 100644 --- a/src/config/general/sort.rs +++ b/src/config/general/sort_crude.rs @@ -1,14 +1,15 @@ +use std::convert::From; + use serde_derive::Deserialize; -use crate::util::sort_option::SortOption; -use crate::util::sort_type::{SortType, SortTypes}; +use crate::config::option::{SortOption, SortType, SortTypes}; const fn default_true() -> bool { true } #[derive(Clone, Debug, Deserialize)] -pub struct SortRawOption { +pub struct SortOptionCrude { #[serde(default = "default_true")] pub directories_first: bool, #[serde(default)] @@ -19,9 +20,20 @@ pub struct SortRawOption { pub sort_method: Option<String>, } -impl SortRawOption { - pub fn into(self) -> SortOption { - let sort_method = match self.sort_method.as_ref() { +impl std::default::Default for SortOptionCrude { + fn default() -> Self { + Self { + directories_first: default_true(), + case_sensitive: bool::default(), + reverse: bool::default(), + sort_method: None, + } + } +} + +impl From<SortOptionCrude> for SortOption { + fn from(crude: SortOptionCrude) -> Self { + let sort_method = match crude.sort_method.as_ref() { Some(s) => SortType::parse(s).unwrap_or(SortType::Natural), None => SortType::Natural, }; @@ -29,22 +41,11 @@ impl SortRawOption { let mut sort_methods = SortTypes::default(); sort_methods.reorganize(sort_method); - SortOption { - directories_first: self.directories_first, - case_sensitive: self.case_sensitive, - reverse: self.reverse, - sort_methods, - } - } -} - -impl std::default::Default for SortRawOption { - fn default() -> Self { Self { - directories_first: default_true(), - case_sensitive: bool::default(), - reverse: bool::default(), - sort_method: None, + directories_first: crude.directories_first, + case_sensitive: crude.case_sensitive, + reverse: crude.reverse, + sort_methods, } } } diff --git a/src/config/general/tab.rs b/src/config/general/tab.rs deleted file mode 100644 index 1cd6a88..0000000 --- a/src/config/general/tab.rs +++ /dev/null @@ -1,57 +0,0 @@ -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/config/general/tab_crude.rs b/src/config/general/tab_crude.rs new file mode 100644 index 0000000..2a8f8f3 --- /dev/null +++ b/src/config/general/tab_crude.rs @@ -0,0 +1,37 @@ +use std::convert::From; + +use serde_derive::Deserialize; + +use crate::config::option::TabOption; +use crate::tab::TabHomePage; + +fn default_home_page() -> String { + "home".to_string() +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TabOptionCrude { + #[serde(default = "default_home_page")] + pub home_page: String, +} + +impl std::default::Default for TabOptionCrude { + fn default() -> Self { + Self { + home_page: default_home_page(), + } + } +} + +impl From<TabOptionCrude> for TabOption { + fn from(crude: TabOptionCrude) -> Self { + let home_page = match crude.home_page.as_str() { + "inherit" => TabHomePage::Inherit, + "home" => TabHomePage::Home, + "root" => TabHomePage::Root, + _ => TabHomePage::Home, + }; + + Self::new(home_page) + } +} diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index 0f42e94..3c3f68c 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -1,13 +1,14 @@ use serde_derive::Deserialize; use std::collections::{hash_map::Entry, HashMap}; +use std::convert::{AsMut, AsRef, From}; use std::str::FromStr; #[cfg(feature = "mouse")] use termion::event::MouseEvent; use termion::event::{Event, Key}; -use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; +use crate::config::{parse_to_config_file, TomlConfigFile}; use crate::error::JoshutoResult; use crate::io::IoWorkerOptions; use crate::key_command::{Command, CommandKeybind}; @@ -22,15 +23,46 @@ struct CommandKeymap { } #[derive(Debug, Deserialize)] -struct RawAppKeyMapping { +struct AppKeyMappingCrude { #[serde(default)] - mapcommand: Vec<CommandKeymap>, + pub mapcommand: Vec<CommandKeymap>, } -impl Flattenable<AppKeyMapping> for RawAppKeyMapping { - fn flatten(self) -> AppKeyMapping { - let mut keymaps = AppKeyMapping::new(); - for m in self.mapcommand { +#[derive(Debug)] +pub struct AppKeyMapping { + map: HashMap<Event, CommandKeybind>, +} + +impl AppKeyMapping { + pub fn new() -> Self { + Self { + map: HashMap::new(), + } + } + + pub fn default_res() -> JoshutoResult<Self> { + let crude: AppKeyMappingCrude = toml::from_str(DEFAULT_KEYMAP)?; + let keymapping: Self = Self::from(crude); + Ok(keymapping) + } +} + +impl AsRef<HashMap<Event, CommandKeybind>> for AppKeyMapping { + fn as_ref(&self) -> &HashMap<Event, CommandKeybind> { + &self.map + } +} + +impl AsMut<HashMap<Event, CommandKeybind>> for AppKeyMapping { + fn as_mut(&mut self) -> &mut HashMap<Event, CommandKeybind> { + &mut self.map + } +} + +impl From<AppKeyMappingCrude> for AppKeyMapping { + fn from(crude: AppKeyMappingCrude) -> Self { + let mut keymaps = Self::new(); + for m in crude.mapcommand { match Command::from_str(m.command.as_str()) { Ok(command) => { let events: Vec<Event> = m @@ -57,34 +89,10 @@ impl Flattenable<AppKeyMapping> for RawAppKeyMapping { } } -#[derive(Debug)] -pub struct AppKeyMapping { - map: HashMap<Event, CommandKeybind>, -} - -impl std::convert::AsRef<HashMap<Event, CommandKeybind>> for AppKeyMapping { - fn as_ref(&self) -> &HashMap<Event, CommandKeybind> { - &self.map - } -} - -impl std::convert::AsMut<HashMap<Event, CommandKeybind>> for AppKeyMapping { - fn as_mut(&mut self) -> &mut HashMap<Event, CommandKeybind> { - &mut self.map - } -} - -impl AppKeyMapping { - pub fn new() -> Self { - Self { - map: HashMap::new(), - } - } - - pub fn default_res() -> JoshutoResult<Self> { - let raw: RawAppKeyMapping = toml::from_str(DEFAULT_KEYMAP)?; - let keymapping: Self = raw.flatten(); - Ok(keymapping) +impl TomlConfigFile for AppKeyMapping { + fn get_config(file_name: &str) -> Self { + parse_to_config_file::<AppKeyMappingCrude, AppKeyMapping>(file_name) + .unwrap_or_else(Self::default) } } @@ -94,13 +102,6 @@ impl std::default::Default for AppKeyMapping { } } -impl ConfigStructure for AppKeyMapping { - fn get_config(file_name: &str) -> Self { - parse_to_config_file::<RawAppKeyMapping, AppKeyMapping>(file_name) - .unwrap_or_else(Self::default) - } -} - fn insert_keycommand( keymap: &mut AppKeyMapping, keycommand: Command, diff --git a/src/config/mimetype/registry.rs b/src/config/mimetype/registry.rs index f3736eb..26ba824 100644 --- a/src/config/mimetype/registry.rs +++ b/src/config/mimetype/registry.rs @@ -1,41 +1,20 @@ use serde_derive::Deserialize; use std::collections::HashMap; +use crate::config::{parse_to_config_file, TomlConfigFile}; + use super::{AppList, AppMimetypeEntry}; -use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; pub type MimetypeRegistry = HashMap<String, AppList>; |