use super::cls_key_map::KeyMap;
use crate::SortBy;
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct BrowserConfig {
pub keymap: KeyMap,
pub initial_path: Option<PathBuf>,
pub show_hidden: bool,
pub sort_by: SortBy,
pub scroll_padding: usize,
pub history_limit: usize,
pub clear_selection_on_navigate: bool,
pub confirm_delete: bool,
pub confirm_overwrite: bool,
pub readonly: bool,
pub respect_ignore_files: bool,
pub show_parent_entry: bool,
}
impl Default for BrowserConfig {
fn default() -> Self {
Self {
keymap: KeyMap::default(),
initial_path: None,
show_hidden: false,
sort_by: SortBy::Name,
scroll_padding: 2,
history_limit: 50,
clear_selection_on_navigate: true,
confirm_delete: true,
confirm_overwrite: true,
readonly: false,
respect_ignore_files: false,
show_parent_entry: true,
}
}
}
impl BrowserConfig {
pub fn open_dialog() -> Self {
Self {
readonly: true,
..Default::default()
}
}
pub fn save_dialog() -> Self {
Self {
readonly: false,
..Default::default()
}
}
pub fn project_explorer() -> Self {
Self {
respect_ignore_files: true,
show_hidden: false,
..Default::default()
}
}
pub fn with_initial_path(mut self, path: impl Into<PathBuf>) -> Self {
self.initial_path = Some(path.into());
self
}
pub fn with_keymap(mut self, keymap: KeyMap) -> Self {
self.keymap = keymap;
self
}
pub fn with_show_hidden(mut self, show: bool) -> Self {
self.show_hidden = show;
self
}
pub fn with_sort_by(mut self, sort_by: SortBy) -> Self {
self.sort_by = sort_by;
self
}
pub fn with_readonly(mut self, readonly: bool) -> Self {
self.readonly = readonly;
self
}
pub fn with_respect_ignore_files(mut self, respect: bool) -> Self {
self.respect_ignore_files = respect;
self
}
pub fn with_show_parent_entry(mut self, show: bool) -> Self {
self.show_parent_entry = show;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let config = BrowserConfig::default();
assert!(!config.readonly);
assert!(!config.show_hidden);
assert!(config.show_parent_entry);
assert!(config.confirm_delete);
}
#[test]
fn test_open_dialog_is_readonly() {
let config = BrowserConfig::open_dialog();
assert!(config.readonly);
}
#[test]
fn test_save_dialog_is_writable() {
let config = BrowserConfig::save_dialog();
assert!(!config.readonly);
}
#[test]
fn test_project_explorer_respects_ignore() {
let config = BrowserConfig::project_explorer();
assert!(config.respect_ignore_files);
assert!(!config.show_hidden);
}
#[test]
fn test_builder_pattern() {
let config = BrowserConfig::default()
.with_show_hidden(true)
.with_readonly(true)
.with_initial_path("/home/user");
assert!(config.show_hidden);
assert!(config.readonly);
assert_eq!(config.initial_path, Some(PathBuf::from("/home/user")));
}
}