From d10175338a7ab05b6bc786d4e0fc3723bad68a59 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Sun, 6 Jan 2019 17:30:36 -0500 Subject: move configuration code into config/ folder - change sort related configuration into its own [] --- config/joshuto.toml | 9 +- config/keymap.toml | 10 ++ config/mimetype.toml | 3 + src/joshuto.rs | 14 +-- src/joshuto/command/file_operation.rs | 2 +- src/joshuto/command/open_file.rs | 2 +- src/joshuto/config.rs | 165 ++-------------------------- src/joshuto/config/config.rs | 173 +++++++++++++++++++++++++++++ src/joshuto/config/keymap.rs | 200 ++++++++++++++++++++++++++++++++++ src/joshuto/config/mimetype.rs | 90 +++++++++++++++ src/joshuto/config/theme.rs | 179 ++++++++++++++++++++++++++++++ src/joshuto/input.rs | 4 +- src/joshuto/keymap.rs | 200 ---------------------------------- src/joshuto/mimetype.rs | 90 --------------- src/joshuto/sort.rs | 26 ++--- src/joshuto/theme.rs | 179 ------------------------------ src/joshuto/ui.rs | 2 +- src/main.rs | 8 +- 18 files changed, 698 insertions(+), 658 deletions(-) create mode 100644 src/joshuto/config/config.rs create mode 100644 src/joshuto/config/keymap.rs create mode 100644 src/joshuto/config/mimetype.rs create mode 100644 src/joshuto/config/theme.rs delete mode 100644 src/joshuto/keymap.rs delete mode 100644 src/joshuto/mimetype.rs delete mode 100644 src/joshuto/theme.rs diff --git a/config/joshuto.toml b/config/joshuto.toml index 9cd7c1a..7d0124c 100644 --- a/config/joshuto.toml +++ b/config/joshuto.toml @@ -1,12 +1,13 @@ # sort_type options: size, natural, basename, atime, ctime, mtime, type, random ## currently only supports: natural, mtime sort_type = "natural" -sort_case_sensitive = false -sort_reverse = false -sort_directories_first = true -# show hidden files +[sort_option] show_hidden = false +case_sensitive = false +reverse = false +directories_first = true + show_prerview = true diff --git a/config/keymap.toml b/config/keymap.toml index 4bc64da..6a0050e 100644 --- a/config/keymap.toml +++ b/config/keymap.toml @@ -105,6 +105,16 @@ keys = [ "m", "k" ] command = "mkdir" +[[mapcommand]] +keys = [ "g", "d" ] +command = "cd" +args = [ "~/Downloads" ] + +[[mapcommand]] +keys = [ "g", "h" ] +command = "cd" +args = [ "~/" ] + [[mapcommand]] keys = [ "g", "r" ] command = "cd" diff --git a/config/mimetype.toml b/config/mimetype.toml index c6eb19c..2a6e3e3 100644 --- a/config/mimetype.toml +++ b/config/mimetype.toml @@ -47,6 +47,9 @@ ["nano"] ] +"audio/flac" = [ + [ "mpv" ] +] "audio/x-wav" = [ [ "mpv" ] ] diff --git a/src/joshuto.rs b/src/joshuto.rs index 73a9673..2bd2825 100644 --- a/src/joshuto.rs +++ b/src/joshuto.rs @@ -11,9 +11,7 @@ use std::thread; use std::time; pub mod config; -pub mod keymap; -pub mod mimetype; -pub mod theme; + mod command; mod history; mod input; @@ -37,13 +35,13 @@ pub struct JoshutoContext<'a> { pub preview_list: Option, pub config_t: config::JoshutoConfig, - pub mimetype_t: &'a mimetype::JoshutoMimetype, + pub mimetype_t: &'a config::JoshutoMimetype, } impl<'a> JoshutoContext<'a> { pub fn new(config_t: &config::JoshutoConfig, - mimetype_t: &'a mimetype::JoshutoMimetype) -> Self + mimetype_t: &'a config::JoshutoMimetype) -> Self { let curr_path: path::PathBuf = match env::current_dir() { Ok(path) => { path }, @@ -199,7 +197,7 @@ fn recurse_get_keycommand<'a>(keymap: &'a HashMap) ncurses::update_panels(); ncurses::doupdate(); - if ch == keymap::ESCAPE { + if ch == config::keymap::ESCAPE { None } else { match keymap.get(&ch) { @@ -232,8 +230,8 @@ pub fn resize_handler(context: &mut JoshutoContext) } pub fn run(config_t: config::JoshutoConfig, - keymap_t: keymap::JoshutoKeymap, - mimetype_t: mimetype::JoshutoMimetype) + keymap_t: config::JoshutoKeymap, + mimetype_t: config::JoshutoMimetype) { ui::init_ncurses(); diff --git a/src/joshuto/command/file_operation.rs b/src/joshuto/command/file_operation.rs index 4cad0f0..e56788b 100644 --- a/src/joshuto/command/file_operation.rs +++ b/src/joshuto/command/file_operation.rs @@ -12,7 +12,7 @@ use std::thread; use joshuto; use joshuto::command; use joshuto::input; -use joshuto::keymap; +use joshuto::config::keymap; use joshuto::structs; use joshuto::ui; use joshuto::window; diff --git a/src/joshuto/command/open_file.rs b/src/joshuto/command/open_file.rs index 0489b89..26ef400 100644 --- a/src/joshuto/command/open_file.rs +++ b/src/joshuto/command/open_file.rs @@ -10,7 +10,7 @@ use std::path; use joshuto; use joshuto::command; use joshuto::input; -use joshuto::mimetype; +use joshuto::config::mimetype; use joshuto::structs; use joshuto::ui; use joshuto::unix; diff --git a/src/joshuto/config.rs b/src/joshuto/config.rs index 5545013..1ef10fb 100644 --- a/src/joshuto/config.rs +++ b/src/joshuto/config.rs @@ -1,156 +1,9 @@ -extern crate whoami; -extern crate toml; -extern crate xdg; - -use std::fs; -use std::process; - -use joshuto; -use joshuto::sort; - -#[derive(Debug, Deserialize)] -pub struct JoshutoRawConfig { - scroll_offset: Option, - show_hidden: Option, - sort_type: Option, - sort_directories_first: Option, - sort_reverse: Option, - sort_case_sensitive: Option, - column_ratio: Option<[usize; 3]>, -} - -impl JoshutoRawConfig { - #[allow(dead_code)] - pub fn new() -> Self - { - JoshutoRawConfig { - scroll_offset: Some(8), - show_hidden: Some(false), - sort_type: Some(String::from("natural")), - sort_directories_first: None, - sort_reverse: None, - sort_case_sensitive: None, - column_ratio: Some([1, 3, 4]), - } - } - - pub fn flatten(self) -> JoshutoConfig - { - let username : String = whoami::username(); - let hostname : String = whoami::hostname(); - - let column_ratio = match self.column_ratio { - Some(s) => (s[0], s[1], s[2]), - None => (1, 3, 4), - }; - - let scroll_offset: usize = self.scroll_offset.unwrap_or(6); - - let show_hidden: bool = self.show_hidden.unwrap_or(false); - let sort_case_sensitive: bool = self.sort_case_sensitive.unwrap_or(false); - let sort_reverse: bool = self.sort_reverse.unwrap_or(false); - let sort_directories_first: bool = self.sort_directories_first.unwrap_or(true); - - let sort_struct = sort::SortStruct { - show_hidden, - sort_directories_first, - sort_case_sensitive, - sort_reverse, - }; - - let sort_type: sort::SortType = match self.sort_type { - Some(s) => { - match s.as_str() { - "natural" => sort::SortType::SortNatural(sort_struct), - "mtime" => sort::SortType::SortMtime(sort_struct), - _ => sort::SortType::SortNatural(sort_struct), - } - } - _ => sort::SortType::SortNatural(sort_struct), - }; - - JoshutoConfig { - username, - hostname, - scroll_offset, - sort_type, - column_ratio, - } - } -} - -#[derive(Debug, Clone)] -pub struct JoshutoConfig { - pub username: String, - pub hostname: String, - pub scroll_offset: usize, - pub sort_type: joshuto::sort::SortType, - pub column_ratio: (usize, usize, usize), -} - -impl JoshutoConfig { - - pub fn new() -> Self - { - let sort_struct = sort::SortStruct { - show_hidden: false, - sort_directories_first: true, - sort_case_sensitive: false, - sort_reverse: false, - }; - let sort_type = sort::SortType::SortNatural(sort_struct); - - let username : String = whoami::username(); - let hostname : String = whoami::hostname(); - - JoshutoConfig { - username, - hostname, - scroll_offset: 6, - sort_type, - column_ratio: (1, 3, 4), - } - } - - fn read_config() -> Option - { - match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { - Ok(dirs) => { - let config_path = dirs.find_config_file(::CONFIG_FILE)?; - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - } - - pub fn get_config() -> Self - { - match Self::read_config() { - Some(config) => { - config.flatten() - } - None => { - JoshutoConfig::new() - } - } - } -} +pub mod config; +pub mod keymap; +pub mod mimetype; +pub mod theme; + +pub use self::config::JoshutoConfig; +pub use self::keymap::JoshutoKeymap; +pub use self::mimetype::JoshutoMimetype; +pub use self::theme::JoshutoTheme; diff --git a/src/joshuto/config/config.rs b/src/joshuto/config/config.rs new file mode 100644 index 0000000..406d126 --- /dev/null +++ b/src/joshuto/config/config.rs @@ -0,0 +1,173 @@ +extern crate whoami; +extern crate toml; +extern crate xdg; + +use std::fs; +use std::process; + +use joshuto; +use joshuto::sort; + +#[derive(Clone, Debug, Deserialize)] +pub struct SortRawOption { + pub show_hidden: Option, + pub directories_first: Option, + pub case_sensitive: Option, + pub reverse: Option, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct JoshutoRawConfig { + scroll_offset: Option, + sort_type: Option, + sort_option: Option, + column_ratio: Option<[usize; 3]>, +} + +impl JoshutoRawConfig { + #[allow(dead_code)] + pub fn new() -> Self + { + JoshutoRawConfig { + scroll_offset: Some(8), + sort_type: Some(String::from("natural")), + sort_option: None, + column_ratio: Some([1, 3, 4]), + } + } + + pub fn flatten(self) -> JoshutoConfig + { + let username : String = whoami::username(); + let hostname : String = whoami::hostname(); + + let column_ratio = match self.column_ratio { + Some(s) => (s[0], s[1], s[2]), + None => (1, 3, 4), + }; + + let scroll_offset: usize = self.scroll_offset.unwrap_or(6); + + let show_hidden: bool; + let case_sensitive: bool; + let reverse: bool; + let directories_first: bool; + + match self.sort_option { + Some(s) => { + show_hidden = s.show_hidden.unwrap_or(false); + case_sensitive = s.case_sensitive.unwrap_or(false); + reverse = s.reverse.unwrap_or(false); + directories_first = s.directories_first.unwrap_or(true); + } + None => { + show_hidden = false; + case_sensitive = false; + reverse = false; + directories_first = true; + } + } + + let sort_option = sort::SortOption { + show_hidden, + directories_first, + case_sensitive, + reverse, + }; + + let sort_type: sort::SortType = match self.sort_type { + Some(s) => { + match s.as_str() { + "natural" => sort::SortType::SortNatural(sort_option), + "mtime" => sort::SortType::SortMtime(sort_option), + _ => sort::SortType::SortNatural(sort_option), + } + } + _ => sort::SortType::SortNatural(sort_option), + }; + + JoshutoConfig { + username, + hostname, + scroll_offset, + sort_type, + column_ratio, + } + } +} + +#[derive(Debug, Clone)] +pub struct JoshutoConfig { + pub username: String, + pub hostname: String, + pub scroll_offset: usize, + pub sort_type: joshuto::sort::SortType, + pub column_ratio: (usize, usize, usize), +} + +impl JoshutoConfig { + + pub fn new() -> Self + { + let sort_option = sort::SortOption { + show_hidden: false, + directories_first: true, + case_sensitive: false, + reverse: false, + }; + let sort_type = sort::SortType::SortNatural(sort_option); + + let username : String = whoami::username(); + let hostname : String = whoami::hostname(); + + JoshutoConfig { + username, + hostname, + scroll_offset: 6, + sort_type, + column_ratio: (1, 3, 4), + } + } + + fn read_config() -> Option + { + match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { + Ok(dirs) => { + let config_path = dirs.find_config_file(::CONFIG_FILE)?; + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> Self + { + match Self::read_config() { + Some(config) => { + config.flatten() + } + None => { + JoshutoConfig::new() + } + } + } +} diff --git a/src/joshuto/config/keymap.rs b/src/joshuto/config/keymap.rs new file mode 100644 index 0000000..96357da --- /dev/null +++ b/src/joshuto/config/keymap.rs @@ -0,0 +1,200 @@ +extern crate fs_extra; +extern crate toml; +extern crate xdg; + +use std::collections::HashMap; +use std::fs; +use std::process; + +use joshuto::command; +use joshuto::input; + +pub const BACKSPACE: i32 = 0x7F; +pub const TAB: i32 = 0x9; +pub const ENTER: i32 = 0xA; +pub const ESCAPE: i32 = 0x1B; + +/* #define KEY_ALT(x) KEY_F(60) + (x - 'A') */ + +#[derive(Debug, Deserialize)] +struct JoshutoMapCommand { + pub keys: Vec, + pub command: String, + pub args: Option>, +} + + +#[derive(Debug, Deserialize)] +struct JoshutoRawKeymap { + mapcommand: Option>, +} + +impl JoshutoRawKeymap { + + pub fn flatten(self) -> JoshutoKeymap + { + let mut keymaps: HashMap = HashMap::new(); + if let Some(maps) = self.mapcommand { + for mapcommand in maps { + match command::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) { + Some(command) => { + insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]); + }, + None => { + println!("Unknown command: {}", mapcommand.command); + } + } + } + } + JoshutoKeymap { + keymaps, + } + } +} + +#[derive(Debug)] +pub struct JoshutoKeymap { + pub keymaps: HashMap, +} + +impl JoshutoKeymap { + pub fn new() -> Self + { + let keymaps = input::initialize_default_keymap(); + JoshutoKeymap { + keymaps, + } + } + + fn read_config() -> Option + { + match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { + Ok(dirs) => { + let config_path = dirs.find_config_file(::KEYMAP_FILE)?; + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> JoshutoKeymap + { + match JoshutoKeymap::read_config() { + Some(config) => { + config.flatten() + } + None => { + JoshutoKeymap::new() + } + } + } +} + + +fn insert_keycommand(map: &mut HashMap, + keycommand: Box, keys: &[String]) +{ + if keys.len() == 1 { + match key_to_i32(&keys[0]) { + Some(s) => { + map.insert(s, command::CommandKeybind::SimpleKeybind(keycommand)); + }, + None => {} + } + } else { + match key_to_i32(&keys[0]) { + Some(s) => { + let mut new_map: HashMap; + match map.remove(&s) { + Some(command::CommandKeybind::CompositeKeybind(mut m)) => { + new_map = m; + }, + Some(_) => { + eprintln!("Error: Keybindings ambiguous"); + process::exit(1); + }, + None => { + new_map = HashMap::new(); + } + } + insert_keycommand(&mut new_map, keycommand, &keys[1..]); + let composite_command = command::CommandKeybind::CompositeKeybind(new_map); + map.insert(s as i32, composite_command); + }, + None => {} + } + } +} + + +pub fn key_to_i32(keycode: &str) -> Option +{ + if keycode.len() == 1 { + for ch in keycode.chars() { + if ch.is_ascii() { + return Some(ch as i32); + } + } + return None; + } else { + match keycode { + "Comma" => Some(',' as i32), + "Tab" => Some(TAB), + "Space" => Some(' ' as i32), + "Backspace" => Some(BACKSPACE), + "Delete" => Some(ncurses::KEY_DC), + "Enter" => Some(ENTER), + "Escape" => Some(ESCAPE), + + + "F0" => Some(ncurses::KEY_F0), + "F1" => Some(ncurses::KEY_F1), + "F2" => Some(ncurses::KEY_F2), + "F3" => Some(ncurses::KEY_F3), + "F4" => Some(ncurses::KEY_F4), + "F5" => Some(ncurses::KEY_F5), + "F6" => Some(ncurses::KEY_F6), + "F7" => Some(ncurses::KEY_F7), + "F8" => Some(ncurses::KEY_F8), + "F9" => Some(ncurses::KEY_F9), + "F10" => Some(ncurses::KEY_F10), + "F11" => Some(ncurses::KEY_F11), + "F12" => Some(ncurses::KEY_F12), + "F13" => Some(ncurses::KEY_F13), + "F14" => Some(ncurses::KEY_F14), + "F15" => Some(ncurses::KEY_F15), + + "Insert" => Some(ncurses::KEY_IC), /* insert-character key */ + "PageUp" => Some(ncurses::KEY_PPAGE), /* next-page key */ + "PageDown" => Some(ncurses::KEY_NPAGE), /* previous-page key */ + "PrintScreen" => Some(ncurses::KEY_PRINT), /* print key */ + + "Up" => Some(ncurses::KEY_UP), + "Down" => Some(ncurses::KEY_DOWN), + "Left" => Some(ncurses::KEY_LEFT), + "Right" => Some(ncurses::KEY_RIGHT), + "Home" => Some(ncurses::KEY_HOME), + "End" => Some(ncurses::KEY_END), + _ => None, + } + } +} diff --git a/src/joshuto/config/mimetype.rs b/src/joshuto/config/mimetype.rs new file mode 100644 index 0000000..5520791 --- /dev/null +++ b/src/joshuto/config/mimetype.rs @@ -0,0 +1,90 @@ +extern crate toml; +extern crate xdg; + +use std::fs; +use std::collections::HashMap; +use std::process; + +#[derive(Debug, Deserialize)] +pub struct JoshutoRawMimetype { + mimetypes: Option>>>, +} + +impl JoshutoRawMimetype { + #[allow(dead_code)] + pub fn new() -> Self + { + JoshutoRawMimetype { + mimetypes: None, + } + } + + pub fn flatten(self) -> JoshutoMimetype + { + let mimetypes = match self.mimetypes { + Some(s) => s, + None => HashMap::new(), + }; + + JoshutoMimetype { + mimetypes, + } + } +} + +#[derive(Debug)] +pub struct JoshutoMimetype { + pub mimetypes: HashMap>>, +} + +impl JoshutoMimetype { + + pub fn new() -> Self + { + JoshutoMimetype { + mimetypes: HashMap::new(), + } + } + + fn read_config() -> Option + { + match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { + Ok(dirs) => { + let config_path = dirs.find_config_file(::MIMETYPE_FILE)?; + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> Self + { + match Self::read_config() { + Some(config) => { + config.flatten() + } + None => { + Self::new() + } + } + } +} diff --git a/src/joshuto/config/theme.rs b/src/joshuto/config/theme.rs new file mode 100644 index 0000000..cf15d4a --- /dev/null +++ b/src/joshuto/config/theme.rs @@ -0,0 +1,179 @@ +extern crate toml; +extern crate xdg; + +use std::collections::HashMap; +use std::fs; +use std::process; + +#[derive(Debug, Deserialize, Clone)] +pub struct JoshutoColorTheme { + fg: i16, + bg: i16, + bold: bool, + underline: bool, +} + +#[derive(Debug, Deserialize)] +pub struct JoshutoRawTheme { + selection: Option, + directory: Option, + executable: Option, + link: Option, + ext: Option>, +} + +impl JoshutoRawTheme { + #[allow(dead_code)] + pub fn new() -> Self + { + JoshutoRawTheme { + selection: None, + directory: None, + executable: None, + link: None, + ext: None, + } + } + + pub fn flatten(self) -> JoshutoTheme + { + let selection = self.selection.unwrap_or( + JoshutoColorTheme { + fg: ncurses::COLOR_YELLOW, + bg: -1, + bold: true, + underline: false, + } + ); + + let directory = self.directory.unwrap_or( + JoshutoColorTheme { + fg: ncurses::COLOR_BLUE, + bg: -1, + bold: true, + underline: false, + } + ); + + let executable = self.executable.unwrap_or( + JoshutoColorTheme { + fg: ncurses::COLOR_GREEN, + bg: -1, + bold: true, + underline: false, + } + ); + + let link = self.link.unwrap_or( + JoshutoColorTheme { + fg: ncurses::COLOR_CYAN, + bg: -1, + bold: true, + underline: false, + } + ); + + let ext = self.ext.unwrap_or(HashMap::new()); + + JoshutoTheme { + directory, + selection, + executable, + link, + ext, + } + } +} + +#[derive(Debug, Clone)] +pub struct JoshutoTheme { + selection: JoshutoColorTheme, + directory: JoshutoColorTheme, + executable: JoshutoColorTheme, + link: JoshutoColorTheme, + ext: HashMap +} + +impl JoshutoTheme { + pub fn new() -> Self + { + let selection = JoshutoColorTheme { + fg: ncurses::COLOR_YELLOW, + bg: -1, + bold: true, + underline: false, + }; + + let directory = JoshutoColorTheme { + fg: ncurses::COLOR_BLUE, + bg: -1, + bold: true, + underline: false, + }; + + let executable = JoshutoColorTheme { + fg: ncurses::COLOR_GREEN, + bg: -1, + bold: true, + underline: false, + }; + + let link = JoshutoColorTheme { + fg: ncurses::COLOR_CYAN, + bg: -1, + bold: true, + underline: false, + }; + + JoshutoTheme { + directory, + selection, + executable, + link, + ext: HashMap::new(), + } + + } + + fn read_config() -> Option + { + match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { + Ok(dirs) => { + let config_path = dirs.find_config_file(::THEME_FILE)?; + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> Self + { + match Self::read_config() { + Some(config) => { + config.flatten() + } + None => { + Self::new() + } + } + } +} diff --git a/src/joshuto/input.rs b/src/joshuto/input.rs index 4c35261..a132544 100644 --- a/src/joshuto/input.rs +++ b/src/joshuto/input.rs @@ -4,9 +4,9 @@ extern crate wcwidth; use std::collections::HashMap; use joshuto::command; -use joshuto::keymap; +use joshuto::config::keymap; use joshuto::window; -use joshuto::keymap::*; +use joshuto::config::keymap::*; pub fn get_str(win: &window::JoshutoPanel, coord: (i32, i32)) -> Option diff --git a/src/joshuto/keymap.rs b/src/joshuto/keymap.rs deleted file mode 100644 index 96357da..0000000 --- a/src/joshuto/keymap.rs +++ /dev/null @@ -1,200 +0,0 @@ -extern crate fs_extra; -extern crate toml; -extern crate xdg; - -use std::collections::HashMap; -use std::fs; -use std::process; - -use joshuto::command; -use joshuto::input; - -pub const BACKSPACE: i32 = 0x7F; -pub const TAB: i32 = 0x9; -pub const ENTER: i32 = 0xA; -pub const ESCAPE: i32 = 0x1B; - -/* #define KEY_ALT(x) KEY_F(60) + (x - 'A') */ - -#[derive(Debug, Deserialize)] -struct JoshutoMapCommand { - pub keys: Vec, - pub command: String, - pub args: Option>, -} - - -#[derive(Debug, Deserialize)] -struct JoshutoRawKeymap { - mapcommand: Option>, -} - -impl JoshutoRawKeymap { - - pub fn flatten(self) -> JoshutoKeymap - { - let mut keymaps: HashMap = HashMap::new(); - if let Some(maps) = self.mapcommand { - for mapcommand in maps { - match command::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) { - Some(command) => { - insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]); - }, - None => { - println!("Unknown command: {}", mapcommand.command); - } - } - } - } - JoshutoKeymap { - keymaps, - } - } -} - -#[derive(Debug)] -pub struct JoshutoKeymap { - pub keymaps: HashMap, -} - -impl JoshutoKeymap { - pub fn new() -> Self - { - let keymaps = input::initialize_default_keymap(); - JoshutoKeymap { - keymaps, - } - } - - fn read_config() -> Option - { - match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { - Ok(dirs) => { - let config_path = dirs.find_config_file(::KEYMAP_FILE)?; - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - } - - pub fn get_config() -> JoshutoKeymap - { - match JoshutoKeymap::read_config() { - Some(config) => { - config.flatten() - } - None => { - JoshutoKeymap::new() - } - } - } -} - - -fn insert_keycommand(map: &mut HashMap, - keycommand: Box, keys: &[String]) -{ - if keys.len() == 1 { - match key_to_i32(&keys[0]) { - Some(s) => { - map.insert(s, command::CommandKeybind::SimpleKeybind(keycommand)); - }, - None => {} - } - } else { - match key_to_i32(&keys[0]) { - Some(s) => { - let mut new_map: HashMap; - match map.remove(&s) { - Some(command::CommandKeybind::CompositeKeybind(mut m)) => { - new_map = m; - }, - Some(_) => { - eprintln!("Error: Keybindings ambiguous"); - process::exit(1); - }, - None => { - new_map = HashMap::new(); - } - } - insert_keycommand(&mut new_map, keycommand, &keys[1..]); - let composite_command = command::CommandKeybind::CompositeKeybind(new_map); - map.insert(s as i32, composite_command); - }, - None => {} - } - } -} - - -pub fn key_to_i32(keycode: &str) -> Option -{ - if keycode.len() == 1 { - for ch in keycode.chars() { - if ch.is_ascii() { - return Some(ch as i32); - } - } - return None; - } else { - match keycode { - "Comma" => Some(',' as i32), - "Tab" => Some(TAB), - "Space" => Some(' ' as i32), - "Backspace" => Some(BACKSPACE), - "Delete" => Some(ncurses::KEY_DC), - "Enter" => Some(ENTER), - "Escape" => Some(ESCAPE), - - - "F0" => Some(ncurses::KEY_F0), - "F1" => Some(ncurses::KEY_F1), - "F2" => Some(ncurses::KEY_F2), - "F3" => Some(ncurses::KEY_F3), - "F4" => Some(ncurses::KEY_F4), - "F5" => Some(ncurses::KEY_F5), - "F6" => Some(ncurses::KEY_F6), - "F7" => Some(ncurses::KEY_F7), - "F8" => Some(ncurses::KEY_F8), - "F9" => Some(ncurses::KEY_F9), - "F10" => Some(ncurses::KEY_F10), - "F11" => Some(ncurses::KEY_F11), - "F12" => Some(ncurses::KEY_F12), - "F13" => Some(ncurses::KEY_F13), - "F14" => Some(ncurses::KEY_F14), - "F15" => Some(ncurses::KEY_F15), - - "Insert" => Some(ncurses::KEY_IC), /* insert-character key */ - "PageUp" => Some(ncurses::KEY_PPAGE), /* next-page key */ - "PageDown" => Some(ncurses::KEY_NPAGE), /* previous-page key */ - "PrintScreen" => Some(ncurses::KEY_PRINT), /* print key */ - - "Up" => Some(ncurses::KEY_UP), - "Down" => Some(ncurses::KEY_DOWN), - "Left" => Some(ncurses::KEY_LEFT), - "Right" => Some(ncurses::KEY_RIGHT), - "Home" => Some(ncurses::KEY_HOME), - "End" => Some(ncurses::KEY_END), - _ => None, - } - } -} diff --git a/src/joshuto/mimetype.rs b/src/joshuto/mimetype.rs deleted file mode 100644 index 5520791..0000000 --- a/src/joshuto/mimetype.rs +++ /dev/null @@ -1,90 +0,0 @@ -extern crate toml; -extern crate xdg; - -use std::fs; -use std::collections::HashMap; -use std::process; - -#[derive(Debug, Deserialize)] -pub struct JoshutoRawMimetype { - mimetypes: Option>>>, -} - -impl JoshutoRawMimetype { - #[allow(dead_code)] - pub fn new() -> Self - { - JoshutoRawMimetype { - mimetypes: None, - } - } - - pub fn flatten(self) -> JoshutoMimetype - { - let mimetypes = match self.mimetypes { - Some(s) => s, - None => HashMap::new(), - }; - - JoshutoMimetype { - mimetypes, - } - } -} - -#[derive(Debug)] -pub struct JoshutoMimetype { - pub mimetypes: HashMap>>, -} - -impl JoshutoMimetype { - - pub fn new() -> Self - { - JoshutoMimetype { - mimetypes: HashMap::new(), - } - } - - fn read_config() -> Option - { - match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { - Ok(dirs) => { - let config_path = dirs.find_config_file(::MIMETYPE_FILE)?; - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - } - - pub fn get_config() -> Self - { - match Self::read_config() { - Some(config) => { - config.flatten() - } - None => { - Self::new() - } - } - } -} diff --git a/src/joshuto/sort.rs b/src/joshuto/sort.rs index 58dd226..8c2ece8 100644 --- a/src/joshuto/sort.rs +++ b/src/joshuto/sort.rs @@ -5,10 +5,18 @@ use std::time; use joshuto::structs; +#[derive(Debug, Clone)] +pub struct SortOption { + pub show_hidden: bool, + pub directories_first: bool, + pub case_sensitive: bool, + pub reverse: bool, +} + #[derive(Debug, Clone)] pub enum SortType { - SortNatural(SortStruct), - SortMtime(SortStruct), + SortNatural(SortOption), + SortMtime(SortOption), } impl SortType { @@ -16,16 +24,16 @@ impl SortType { { match *self { SortType::SortNatural(ref ss) => { - if ss.sort_directories_first && !ss.sort_case_sensitive && !ss.sort_reverse { + if ss.directories_first && !ss.case_sensitive && !ss.reverse { SortNatural::dir_first_case_insensitive - } else if ss.sort_directories_first && ss.sort_case_sensitive && !ss.sort_reverse { + } else if ss.directories_first && ss.case_sensitive && !ss.reverse { SortNatural::dir_first } else { SortNatural::default_sort } } SortType::SortMtime(ref ss) => { - if ss.sort_directories_first && !ss.sort_reverse { + if ss.directories_first && !ss.reverse { SortMtime::dir_first } else { SortMtime::default_sort @@ -79,14 +87,6 @@ impl SortType { } } -#[derive(Debug, Clone)] -pub struct SortStruct { - pub show_hidden: bool, - pub sort_directories_first: bool, - pub sort_case_sensitive: bool, - pub sort_reverse: bool, -} - fn filter_default(result : Result) -> Option { match result { diff --git a/src/joshuto/theme.rs b/src/joshuto/theme.rs deleted file mode 100644 index cf15d4a..0000000 --- a/src/joshuto/theme.rs +++ /dev/null @@ -1,179 +0,0 @@ -extern crate toml; -extern crate xdg; - -use std::collections::HashMap; -use std::fs; -use std::process; - -#[derive(Debug, Deserialize, Clone)] -pub struct JoshutoColorTheme { - fg: i16, - bg: i16, - bold: bool, - underline: bool, -} - -#[derive(Debug, Deserialize)] -pub struct JoshutoRawTheme { - selection: Option, - directory: Option, - executable: Option, - link: Option, - ext: Option>, -} - -impl JoshutoRawTheme { - #[allow(dead_code)] - pub fn new() -> Self - { - JoshutoRawTheme { - selection: None, - directory: None, - executable: None, - link: None, - ext: None, - } - } - - pub fn flatten(self) -> JoshutoTheme - { - let selection = self.selection.unwrap_or( - JoshutoColorTheme { - fg: ncurses::COLOR_YELLOW, - bg: -1, - bold: true, - underline: false, - } - ); - - let directory = self.directory.unwrap_or( - JoshutoColorTheme { - fg: ncurses::COLOR_BLUE, - bg: -1, - bold: true, - underline: false, - } - ); - - let executable = self.executable.unwrap_or( - JoshutoColorTheme { - fg: ncurses::COLOR_GREEN, - bg: -1, - bold: true, - underline: false, - } - ); - - let link = self.link.unwrap_or( - JoshutoColorTheme { - fg: ncurses::COLOR_CYAN, - bg: -1, - bold: true, - underline: false, - } - ); - - let ext = self.ext.unwrap_or(HashMap::new()); - - JoshutoTheme { - directory, - selection, - executable, - link, - ext, - } - } -} - -#[derive(Debug, Clone)] -pub struct JoshutoTheme { - selection: JoshutoColorTheme, - directory: JoshutoColorTheme, - executable: JoshutoColorTheme, - link: JoshutoColorTheme, - ext: HashMap -} - -impl JoshutoTheme { - pub fn new() -> Self - { - let selection = JoshutoColorTheme { - fg: ncurses::COLOR_YELLOW, - bg: -1, - bold: true, - underline: false, - }; - - let directory = JoshutoColorTheme { - fg: ncurses::COLOR_BLUE, - bg: -1, - bold: true, - underline: false, - }; - - let executable = JoshutoColorTheme { - fg: ncurses::COLOR_GREEN, - bg: -1, - bold: true, - underline: false, - }; - - let link = JoshutoColorTheme { - fg: ncurses::COLOR_CYAN, - bg: -1, - bold: true, - underline: false, - }; - - JoshutoTheme { - directory, - selection, - executable, - link, - ext: HashMap::new(), - } - - } - - fn read_config() -> Option - { - match xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "") { - Ok(dirs) => { - let config_path = dirs.find_config_file(::THEME_FILE)?; - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } - } - - pub fn get_config() -> Self - { - match Self::read_config() { - Some(config) => { - config.flatten() - } - None => { - Self::new() - } - } - } -} diff --git a/src/joshuto/ui.rs b/src/joshuto/ui.rs index 679a2d5..d63bdf6 100644 --- a/src/joshuto/ui.rs +++ b/src/joshuto/ui.rs @@ -5,7 +5,7 @@ use std::fs; use std::path; use joshuto::structs; -use joshuto::theme; +use joshuto::config::theme; use joshuto::unix; use joshuto::window; diff --git a/src/main.rs b/src/main.rs index ef347c0..750fd3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,12 +29,14 @@ fn main() } let config = joshuto::config::JoshutoConfig::get_config(); - let mimetype = joshuto::mimetype::JoshutoMimetype::get_config(); - let keymap = joshuto::keymap::JoshutoKeymap::get_config(); +// println!("{:#?}", config); + + let mimetype = joshuto::config::JoshutoMimetype::get_config(); + let keymap = joshuto::config::JoshutoKeymap::get_config(); // println!("{:#?}", keymap); // println!("{:#?}", keymap.keymaps); - let theme = joshuto::theme::JoshutoTheme::get_config(); + let theme = joshuto::config::JoshutoTheme::get_config(); // println!("{:#?}", theme); joshuto::run(config, keymap, mimetype); -- cgit v1.2.3