diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-08-06 14:02:51 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-08-06 14:02:51 -0400 |
commit | e0bbe7f3890ee66acfc83357720c9bfabf782c39 (patch) | |
tree | 5cbffcecf97b9db516448cce5614d5aebcdb6a03 | |
parent | c176412f865c14f90a2392e93421f0523ae3b11b (diff) |
make usercache persistent
-rw-r--r-- | src/commands/mod.rs | 12 | ||||
-rw-r--r-- | src/commands/open_file.rs | 28 | ||||
-rw-r--r-- | src/config/config.rs | 2 | ||||
-rw-r--r-- | src/config/keymap.rs | 22 | ||||
-rw-r--r-- | src/config/mimetype.rs | 1 | ||||
-rw-r--r-- | src/error.rs | 1 | ||||
-rw-r--r-- | src/preview.rs | 2 | ||||
-rw-r--r-- | src/ui.rs | 31 | ||||
-rw-r--r-- | src/unix.rs | 3 |
9 files changed, 55 insertions, 47 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d94e1a0..ad60802 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -55,12 +55,6 @@ pub enum CommandKeybind { CompositeKeybind(JoshutoCommandMapping), } -pub trait JoshutoRunnable { - fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) -> JoshutoResult<()>; -} - -pub trait JoshutoCommand: JoshutoRunnable + std::fmt::Display + std::fmt::Debug {} - impl std::fmt::Display for CommandKeybind { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { @@ -70,6 +64,12 @@ impl std::fmt::Display for CommandKeybind { } } +pub trait JoshutoRunnable { + fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) -> JoshutoResult<()>; +} + +pub trait JoshutoCommand: JoshutoRunnable + std::fmt::Display + std::fmt::Debug {} + pub fn from_args(command: String, args: Vec<String>) -> JoshutoResult<Box<JoshutoCommand>> { match command.as_str() { "bulk_rename" => Ok(Box::new(self::BulkRename::new())), diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 5eb8493..8b60a2f 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -68,7 +68,7 @@ impl OpenFile { } let mimetype_options = Self::get_options(&paths[0]); if !mimetype_options.is_empty() { - mimetype_options[0].execute_with(&paths); + mimetype_options[0].execute_with(&paths)?; } else if context.config_t.xdg_open { ncurses::savetty(); ncurses::endwin(); @@ -76,7 +76,7 @@ impl OpenFile { ncurses::resetty(); ncurses::refresh(); } else { - OpenFileWith::open_with(&paths); + OpenFileWith::open_with(&paths)?; } let curr_tab = &mut context.tabs[context.curr_tab_index]; if curr_tab.curr_list.need_update() { @@ -137,7 +137,7 @@ impl OpenFileWith { "open_file_with" } - pub fn open_with(paths: &[&PathBuf]) -> JoshutoResult<()> { + pub fn open_with(paths: &[&PathBuf]) -> std::io::Result<()> { const PROMPT: &str = ":open_with "; let mimetype_options: Vec<&JoshutoMimetypeEntry> = OpenFile::get_options(&paths[0]); @@ -174,27 +174,23 @@ impl OpenFileWith { Some(user_input) => match user_input.parse::<usize>() { Ok(n) => { if n < mimetype_options.len() { - mimetype_options[n].execute_with(paths); - Ok(()) + mimetype_options[n].execute_with(paths) } else { - Err(JoshutoError::new( - JoshutoErrorKind::IOInvalidData, + let err = std::io::Error::new( + std::io::ErrorKind::InvalidData, "option does not exist".to_owned(), - )) + ); + Err(err) } } Err(_) => { let mut args_iter = user_input.split_whitespace(); match args_iter.next() { - Some(s) => { - let command = String::from(s); - JoshutoMimetypeEntry::new(command) + Some(s) => JoshutoMimetypeEntry::new(s.to_owned()) .add_args(args_iter) - .execute_with(paths); - } - None => {} + .execute_with(paths), + None => Ok(()), } - Ok(()) } }, } @@ -222,7 +218,7 @@ impl JoshutoRunnable for OpenFileWith { Some(_) => {} } let paths = curr_list.get_selected_paths(); - Self::open_with(&paths); + Self::open_with(&paths)?; Ok(()) } } diff --git a/src/config/config.rs b/src/config/config.rs index f4f962e..0e7c856 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -113,7 +113,7 @@ pub struct JoshutoConfig { impl ConfigStructure for JoshutoConfig { fn get_config() -> Self { parse_to_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE) - .unwrap_or_else(JoshutoConfig::default) + .unwrap_or_else(Self::default) } } diff --git a/src/config/keymap.rs b/src/config/keymap.rs index 8735a00..10d6be7 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -129,7 +129,7 @@ impl Flattenable<JoshutoKeyMapping> for JoshutoRawKeymapping { impl ConfigStructure for JoshutoKeyMapping { fn get_config() -> Self { parse_to_config_file::<JoshutoRawKeymapping, JoshutoKeyMapping>(KEYMAP_FILE) - .unwrap_or_else(JoshutoKeyMapping::default) + .unwrap_or_else(Self::default) } } @@ -159,39 +159,39 @@ pub type JoshutoCommandMapping = HashMap<i32, CommandKeybind>; impl ConfigStructure for JoshutoCommandMapping { fn get_config() -> Self { parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE) - .unwrap_or_else(JoshutoCommandMapping::default) + .unwrap_or_else(Self::default) } } fn insert_keycommand( - map: &mut JoshutoCommandMapping, + keymap: &mut JoshutoCommandMapping, keycommand: Box<JoshutoCommand>, - keys: &[i32], + keycodes: &[i32], ) { - match keys.len() { + match keycodes.len() { 0 => {} - 1 => match map.entry(keys[0]) { + 1 => match keymap.entry(keycodes[0]) { Entry::Occupied(_) => { - eprintln!("Error: Keybindings ambiguous"); + eprintln!("Error: Keybindings ambiguous for {}", keycommand); exit(1); } Entry::Vacant(entry) => { entry.insert(CommandKeybind::SimpleKeybind(keycommand)); } }, - _ => match map.entry(keys[0]) { + _ => match keymap.entry(keycodes[0]) { Entry::Occupied(mut entry) => match entry.get_mut() { CommandKeybind::CompositeKeybind(ref mut m) => { - insert_keycommand(m, keycommand, &keys[1..]) + insert_keycommand(m, keycommand, &keycodes[1..]) } _ => { - eprintln!("Error: Keybindings ambiguous"); + eprintln!("Error: Keybindings ambiguous for {}", keycommand); exit(1); } }, Entry::Vacant(entry) => { let mut new_map = JoshutoCommandMapping::new(); - insert_keycommand(&mut new_map, keycommand, &keys[1..]); + insert_keycommand(&mut new_map, keycommand, &keycodes[1..]); let composite_command = CommandKeybind::CompositeKeybind(new_map); entry.insert(composite_command); } diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 8ba3cf0..c69d4df 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -25,6 +25,7 @@ pub struct JoshutoMimetypeEntry { confirm_exit: bool, } +#[allow(dead_code)] impl JoshutoMimetypeEntry { pub fn new(command: String) -> Self { Self { diff --git a/src/error.rs b/src/error.rs index 43d1d3e..630dfc8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -61,6 +61,7 @@ pub struct JoshutoError { _cause: String, } +#[allow(dead_code)] impl JoshutoError { pub fn new(_kind: JoshutoErrorKind, _cause: String) -> Self { JoshutoError { _kind, _cause } diff --git a/src/preview.rs b/src/preview.rs index 0f97a22..755d6bb 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -50,7 +50,7 @@ fn preview_directory( ) -> std::io::Result<()> { match history.entry(path.to_path_buf().clone()) { Entry::Occupied(mut entry) => { - let mut dirlist = entry.get_mut(); + let dirlist = entry.get_mut(); if dirlist.need_update() { dirlist.reload_contents(&config_t.sort_option)? } else { @@ -1,9 +1,3 @@ -use std::fs; -use std::time; - -use users::mock::{Groups, Users}; -use users::UsersCache; - use crate::config::{JoshutoColorTheme, JoshutoConfig}; use crate::context::JoshutoContext; use crate::fs::{JoshutoDirEntry, JoshutoDirList}; @@ -12,6 +6,14 @@ use crate::window; use crate::THEME_T; +use std::fs; +use std::time; +use std::sync::Mutex; + +use lazy_static::lazy_static; +use users::mock::{Groups, Users}; +use users::UsersCache; + pub const ERR_COLOR: i16 = 240; pub const EMPTY_COLOR: i16 = 241; @@ -65,13 +67,17 @@ pub fn getmaxyx() -> (i32, i32) { (term_rows, term_cols) } -pub fn display_menu(win: &window::JoshutoPanel, vals: &[String]) { +pub fn display_menu<I, S>(win: &window::JoshutoPanel, items: I) +where + I: IntoIterator<Item = S>, + S: AsRef<str>, +{ ncurses::werase(win.win); ncurses::mvwhline(win.win, 0, 0, 0, win.cols); - for (i, val) in vals.iter().enumerate() { + for (i, val) in items.into_iter().enumerate() { ncurses::wmove(win.win, (i + 1) as i32, 0); - ncurses::waddstr(win.win, val.as_str()); + ncurses::waddstr(win.win, val.as_ref()); } ncurses::wnoutrefresh(win.win); } @@ -241,6 +247,10 @@ pub fn display_contents( win.queue_for_refresh(); } +lazy_static! { + static ref USERCACHE: Mutex<UsersCache> = Mutex::new(UsersCache::new()); +} + pub fn wprint_file_status( win: &window::JoshutoPanel, entry: &JoshutoDirEntry, @@ -252,7 +262,8 @@ pub fn wprint_file_status( ncurses::waddch(win.win, ' ' as ncurses::chtype); ncurses::waddstr(win.win, &format!("{}/{} ", index + 1, len)); - let usercache: UsersCache = UsersCache::new(); + let usercache = USERCACHE.lock().unwrap(); + match usercache.get_user_by_uid(entry.metadata.uid) { Some(s) => match s.name().to_str() { Some(name) => ncurses::waddstr(win.win, name), diff --git a/src/unix.rs b/src/unix.rs index 03c20b1..1c25cd2 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,5 +1,4 @@ -use std::path::{Path, PathBuf}; -use std::process; +use std::path::Path; pub fn is_executable(mode: u32) -> bool { const LIBC_PERMISSION_VALS: [libc::mode_t; 3] = [libc::S_IXUSR, libc::S_IXGRP, libc::S_IXOTH]; |