From a0286692964d120ee8a6ea36fbfb2f577dc2305c Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Mon, 16 Mar 2020 21:02:35 -0400 Subject: cargo clippy --- src/commands/bulk_rename.rs | 4 +- src/commands/command_line.rs | 3 +- src/commands/cursor_move.rs | 2 +- src/commands/file_ops/local_state.rs | 3 +- src/commands/file_ops/paste_copy.rs | 1 - src/commands/file_ops/paste_cut.rs | 1 - src/commands/open_file.rs | 4 +- src/commands/rename_file.rs | 8 +- src/commands/set_mode.rs | 2 +- src/config/config.rs | 4 +- src/config/keymap.rs | 15 ++- src/config/mimetype.rs | 2 +- src/config/theme.rs | 8 +- src/context.rs | 2 +- src/fs/dirlist.rs | 4 +- src/fs/entry.rs | 2 +- src/fs/metadata.rs | 2 +- src/preview.rs | 168 --------------------------------- src/run.rs | 4 +- src/tab.rs | 3 +- src/ui/widgets/tui_dirlist.rs | 5 +- src/ui/widgets/tui_dirlist_detailed.rs | 5 +- src/ui/widgets/tui_menu.rs | 6 +- src/util/key_mapping.rs | 6 +- 24 files changed, 39 insertions(+), 225 deletions(-) delete mode 100644 src/preview.rs diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index e22be06..f1f9549 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -57,7 +57,7 @@ impl BulkRename { let file_name = path.file_name().unwrap(); let file_name_as_bytes = file_name.to_str().unwrap().as_bytes(); file.write(file_name_as_bytes)?; - file.write(&['\n' as u8])?; + file.write(&[b'\n'])?; } } @@ -82,7 +82,7 @@ impl BulkRename { for line in reader.lines() { let line2 = line?; let line = line2.trim(); - if line.len() == 0 { + if line.is_empty() { continue; } let path = path::PathBuf::from(line); diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index f44c4a5..0714ed2 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -58,7 +58,6 @@ impl std::fmt::Display for CommandLine { impl JoshutoRunnable for CommandLine { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { - let res = self.readline(context, backend); - res + self.readline(context, backend) } } diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index 0306d93..ba536bb 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -13,7 +13,7 @@ pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) { let mut path: Option = None; if let Some(curr_list) = curr_tab.curr_list_mut() { - if let Some(_) = curr_list.index { + if curr_list.index.is_some() { let dir_len = curr_list.contents.len(); if new_index >= dir_len { new_index = dir_len - 1; diff --git a/src/commands/file_ops/local_state.rs b/src/commands/file_ops/local_state.rs index 2849893..12ffa6c 100644 --- a/src/commands/file_ops/local_state.rs +++ b/src/commands/file_ops/local_state.rs @@ -46,8 +46,7 @@ impl LocalState { } pub fn take_selected_files() -> Option> { - let paths = SELECTED_FILES.lock().unwrap().take(); - paths + SELECTED_FILES.lock().unwrap().take() } pub fn get_file_operation() -> FileOp { diff --git a/src/commands/file_ops/paste_copy.rs b/src/commands/file_ops/paste_copy.rs index 3a430d1..b1a7368 100644 --- a/src/commands/file_ops/paste_copy.rs +++ b/src/commands/file_ops/paste_copy.rs @@ -5,7 +5,6 @@ use std::thread; use crate::context::JoshutoContext; use crate::io::{IOWorkerThread, Options}; -use crate::util::event::Event; use super::local_state::LocalState; use super::name_resolution::rename_filename_conflict; diff --git a/src/commands/file_ops/paste_cut.rs b/src/commands/file_ops/paste_cut.rs index fb9dc72..2bb9adc 100644 --- a/src/commands/file_ops/paste_cut.rs +++ b/src/commands/file_ops/paste_cut.rs @@ -5,7 +5,6 @@ use std::thread; use crate::context::JoshutoContext; use crate::io::{IOWorkerThread, Options}; -use crate::util::event::Event; use super::local_state::LocalState; use super::name_resolution::rename_filename_conflict; diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index c871c29..5d1e7de 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -53,7 +53,7 @@ impl OpenFile { LoadChild::load_child(context)?; } else if let Some(paths) = filepaths { let options = Self::get_options(paths[0]); - if options.len() > 0 { + if !options.is_empty() { backend.terminal_drop(); let res = options[0].execute_with(&paths); backend.terminal_restore()?; @@ -97,7 +97,7 @@ impl OpenFileWith { backend: &mut TuiBackend, paths: &[&PathBuf], ) -> std::io::Result<()> { - const PROMPT: &'static str = "open_with "; + const PROMPT: &str = "open_with "; let mimetype_options: Vec<&JoshutoMimetypeEntry> = OpenFile::get_options(&paths[0]); diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index 7ee4272..0bab5e6 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -109,9 +109,7 @@ impl JoshutoRunnable for RenameFileAppend { let mut file_name: Option = None; if let Some(curr_list) = context.curr_tab_ref().curr_list_ref() { - file_name = curr_list - .get_curr_ref() - .and_then(|s| Some(s.file_name().to_string())); + file_name = curr_list.get_curr_ref().map(|s| s.file_name().to_string()); } if let Some(file_name) = file_name { @@ -160,9 +158,7 @@ impl JoshutoRunnable for RenameFilePrepend { let mut file_name: Option = None; if let Some(curr_list) = context.curr_tab_ref().curr_list_ref() { - file_name = curr_list - .get_curr_ref() - .and_then(|s| Some(s.file_name().to_string())); + file_name = curr_list.get_curr_ref().map(|s| s.file_name().to_string()); } if let Some(file_name) = file_name { diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index d23c366..b3e6bf5 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -52,7 +52,7 @@ impl JoshutoRunnable for SetMode { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { use std::os::unix::fs::PermissionsExt; - const PREFIX: &'static str = "set_mode "; + const PREFIX: &str = "set_mode "; let entry = context.tabs[context.curr_tab_index] .curr_list_ref() diff --git a/src/config/config.rs b/src/config/config.rs index 0e7c856..029bbcb 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -44,7 +44,7 @@ impl SortRawOption { impl std::default::Default for SortRawOption { fn default() -> Self { - SortRawOption { + Self { show_hidden: bool::default(), directories_first: default_true(), case_sensitive: bool::default(), @@ -121,7 +121,7 @@ impl std::default::Default for JoshutoConfig { fn default() -> Self { let sort_option = sort::SortOption::default(); - JoshutoConfig { + Self { scroll_offset: default_scroll_offset(), tilde_in_titlebar: default_true(), show_preview: default_true(), diff --git a/src/config/keymap.rs b/src/config/keymap.rs index 9262893..6bf915c 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -82,21 +82,18 @@ fn insert_keycommand( match keymap.entry(key) { Entry::Occupied(mut entry) => match entry.get_mut() { CommandKeybind::CompositeKeybind(ref mut m) => { - return insert_keycommand(m, keycommand, &keycodes[1..]) + insert_keycommand(m, keycommand, &keycodes[1..]) } - _ => return Err(format!("Error: Keybindings ambiguous for {}", keycommand)), + _ => Err(format!("Error: Keybindings ambiguous for {}", keycommand)), }, Entry::Vacant(entry) => { let mut new_map = JoshutoCommandMapping::new(); let result = insert_keycommand(&mut new_map, keycommand, &keycodes[1..]); - match result { - Ok(_) => { - let composite_command = CommandKeybind::CompositeKeybind(new_map); - entry.insert(composite_command); - } - _ => {} + if result.is_ok() { + let composite_command = CommandKeybind::CompositeKeybind(new_map); + entry.insert(composite_command); } - return result; + result } } } diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index f6d81de..f8c4f69 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -172,7 +172,7 @@ impl ConfigStructure for JoshutoMimetype { impl std::default::Default for JoshutoMimetype { fn default() -> Self { - JoshutoMimetype { + Self { empty_vec: Vec::new(), mimetype: HashMap::new(), extension: HashMap::new(), diff --git a/src/config/theme.rs b/src/config/theme.rs index 38af29f..541c340 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -87,8 +87,8 @@ impl JoshutoStyleThemeRaw { impl std::default::Default for JoshutoStyleThemeRaw { fn default() -> Self { Self { - bg: String::new(), - fg: String::new(), + bg: "".to_string(), + fg: "".to_string(), bold: false, underline: false, invert: false, @@ -193,7 +193,7 @@ impl JoshutoStyleTheme { impl std::default::Default for JoshutoStyleTheme { fn default() -> Self { - JoshutoStyleTheme { + Self { fg: default_color(), bg: default_color(), bold: false, @@ -242,7 +242,7 @@ impl std::default::Default for JoshutoTheme { .set_bold(true); let ext = HashMap::new(); - JoshutoTheme { + Self { selection, executable, regular, diff --git a/src/context.rs b/src/context.rs index 0370f1b..9f357b5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -20,7 +20,7 @@ pub struct JoshutoContext { impl JoshutoContext { pub fn new(config_t: config::JoshutoConfig) -> Self { - JoshutoContext { + Self { exit: false, curr_tab_index: 0, tabs: Vec::new(), diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs index 283fd41..97a03fc 100644 --- a/src/fs/dirlist.rs +++ b/src/fs/dirlist.rs @@ -24,7 +24,7 @@ impl JoshutoDirList { let metadata = JoshutoMetadata::from(&path)?; - Ok(JoshutoDirList { + Ok(Self { index, path, content_outdated: false, @@ -66,7 +66,7 @@ impl JoshutoDirList { .iter() .enumerate() .find(|(_, e)| e.file_name() == entry.file_name()) - .and_then(|(i, _)| Some(i)) + .map(|(i, _)| i) .or(Some(contents_len - 1)) } None => Some(0), diff --git a/src/fs/entry.rs b/src/fs/entry.rs index 5f24de1..3cba354 100644 --- a/src/fs/entry.rs +++ b/src/fs/entry.rs @@ -31,7 +31,7 @@ impl JoshutoDirEntry { let path = direntry.path(); let metadata = JoshutoMetadata::from(&path)?; - Ok(JoshutoDirEntry { + Ok(Self { name, path, metadata, diff --git a/src/fs/metadata.rs b/src/fs/metadata.rs index dbf4d37..e1e55e8 100644 --- a/src/fs/metadata.rs +++ b/src/fs/metadata.rs @@ -33,7 +33,7 @@ impl JoshutoMetadata { #[cfg(unix)] let mode = metadata.mode(); - Ok(JoshutoMetadata { + Ok(Self { len, modified, permissions, diff --git a/src/preview.rs b/src/preview.rs deleted file mode 100644 index 755d6bb..0000000 --- a/src/preview.rs +++ /dev/null @@ -1,168 +0,0 @@ -use std::collections::{hash_map::Entry, HashMap}; -use std::io::BufRead; -use std::path; -use std::process; - -use crate::config::{JoshutoConfig, JoshutoPreviewEntry}; -use crate::fs::{JoshutoDirEntry, JoshutoDirList}; -use crate::tab::JoshutoTab; -use crate::ui; -use crate::window::panel::JoshutoPanel; -use crate::PREVIEW_T; - -pub fn preview_parent(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) { - if let Some(path) = curr_tab.curr_path.parent() { - preview_directory(&mut curr_tab.history, path, win, config_t); - } else { - ncurses::werase(win.win); - win.queue_for_refresh(); - } -} - -pub fn preview_entry(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) { - ncurses::werase(win.win); - if let Some(s) = curr_tab.curr_list.get_curr_ref() { - if s.file_path().is_dir() { - preview_directory( - &mut curr_tab.history, - s.file_path().as_path(), - win, - config_t, - ); - } else if s.metadata.file_type.is_file() { - if s.metadata.len <= config_t.max_preview_size { - // preview_file(s, win); - } else { - ui::wprint_err(win, "File size exceeds max preview size"); - } - } else { - ui::wprint_err(win, "Not a regular file"); - } - } - win.queue_for_refresh(); -} - -fn preview_directory( - history: &mut HashMap, - path: &path::Path, - win: &JoshutoPanel, - config_t: &JoshutoConfig, -) -> std::io::Result<()> { - match history.entry(path.to_path_buf().clone()) { - Entry::Occupied(mut entry) => { - let dirlist = entry.get_mut(); - if dirlist.need_update() { - dirlist.reload_contents(&config_t.sort_option)? - } else { - let metadata = std::fs::symlink_metadata(dirlist.file_path())?; - - let modified = metadata.modified()?; - if modified > dirlist.metadata.modified { - dirlist.reload_contents(&config_t.sort_option)? - } - } - ui::display_contents(win, dirlist, config_t, &ui::SECONDARY_DISPLAY_OPTION); - } - Entry::Vacant(entry) => { - if let Ok(s) = JoshutoDirList::new(path.to_path_buf().clone(), &config_t.sort_option) { - ui::display_contents( - win, - entry.insert(s), - config_t, - &ui::SECONDARY_DISPLAY_OPTION, - ); - } - } - } - win.queue_for_refresh(); - Ok(()) -} - -fn preview_file(entry: &JoshutoDirEntry, win: &JoshutoPanel) { - let path = entry.file_path(); - match path.extension() { - Some(file_ext) => match PREVIEW_T.extension.get(file_ext.to_str().unwrap()) { - Some(s) => preview_with(path, win, &s), - None => {} /* - None => if let Some(mimetype) = tree_magic::from_filepath(&path) { - match PREVIEW_T.mimetype.get(mimetype.as_str()) { - Some(s) => preview_with(path, win, &s), - None => if let Some(ind) = mimetype.find('/') { - let supertype = &mimetype[..ind]; - if supertype == "text" { - preview_text(path, win); - } else if let Some(s) = PREVIEW_T.mimetype.get(supertype) { - preview_with(path, win, &s); - } - }, - } - } - */ - }, - None => {} /* - if let Some(mimetype) = tree_magic::from_filepath(&path) { - match PREVIEW_T.mimetype.get(mimetype.as_str()) { - Some(s) => preview_with(path, win, &s), - None => if let Some(ind) = mimetype.find('/') { - let supertype = &mimetype[..ind]; - if supertype == "text" { - preview_text(path, win); - } else if let Some(s) = PREVIEW_T.mimetype.get(supertype) { - preview_with(path, win, &s); - } - }, - } - } - */ - } -} - -fn preview_with(path: &path::Path, win: &JoshutoPanel, entry: &JoshutoPreviewEntry) { - let mut command = process::Command::new(&entry.program); - command - .args(entry.args.as_ref().unwrap_or(&Vec::new())) - .arg(path.as_os_str()) - .stdin(std::process::Stdio::piped()) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::piped()); - - match command.spawn() { - Ok(child) => { - if let Some(output) = child.stdout { - let reader = std::io::BufReader::new(output); - - let mut i = 0; - for line in reader.lines() { - if let Ok(line) = line { - ncurses::mvwaddnstr(win.win, i, 0, &line, win.cols); - i += 1; - } - if i == win.rows { - break; - } - } - } - } - Err(e) => { - eprintln!("{:?}", e); - ui::wprint_err(win, e.to_string().as_str()); - } - } -} - -fn preview_text(path: &path::Path, win: &JoshutoPanel) { - match std::fs::File::open(path) { - Err(e) => ui::wprint_err(win, e.to_string().as_str()), - Ok(f) => { - let reader = std::io::BufReader::new(f); - for (i, line) in reader.lines().enumerate() { - if let Ok(line) = line { - ncurses::mvwaddstr(win.win, i as i32, 0, &line); - } - if i >= win.rows as usize { - break; - } - } - } - } -} diff --git a/src/run.rs b/src/run.rs index e43ce84..b18567f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -31,7 +31,7 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: while !context.exit { /* checking if there are workers that need to be run */ if !context.worker_queue.is_empty() { - if let None = io_observer.as_ref() { + if io_observer.is_none() { let worker = context.worker_queue.pop_front().unwrap(); io_observer = { let event_tx = context.events.event_tx.clone(); @@ -43,7 +43,7 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: let event = match context.events.next() { Ok(event) => event, - Err(e) => return Ok(()), + Err(_) => return Ok(()), // TODO }; match event { diff --git a/src/tab.rs b/src/tab.rs index 22674be..b7aabce 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -14,8 +14,7 @@ impl JoshutoTab { let mut history = JoshutoHistory::new(); history.populate_to_root(&curr_path, sort_option)?; - let tab = JoshutoTab { curr_path, history }; - Ok(tab) + Ok(Self { curr_path, history }) } pub fn curr_list_ref(&self) -> Option<&JoshutoDirList> { diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs index 4992fab..5eeb23b 100644 --- a/src/ui/widgets/tui_dirlist.rs +++ b/src/ui/widgets/tui_dirlist.rs @@ -46,11 +46,8 @@ impl<'a> Widget for TuiDirList<'a> { }; let area_width = area.width as usize - 1; - for (i, entry) in self - .dirlist - .contents + for (i, entry) in self.dirlist.contents[skip_dist..] .iter() - .skip(skip_dist) .enumerate() .take(area.height as usize) { diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index 54de784..08c6f80 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -50,11 +50,8 @@ impl<'a> Widget for TuiDirListDetailed<'a> { }; let area_width = area.width as usize; - for (i, entry) in self - .dirlist - .contents + for (i, entry) in self.dirlist.contents[skip_dist..] .iter() - .skip(skip_dist) .enumerate() .take(area.height as usize) { diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs index efa8d7d..5ac008b 100644 --- a/src/ui/widgets/tui_menu.rs +++ b/src/ui/widgets/tui_menu.rs @@ -66,7 +66,7 @@ impl TuiCommandMenu { let menu_rect = Rect { x: 0, - y: y, + y, width: f_size.width, height: (display_str_len + BORDER_HEIGHT) as u16, }; @@ -100,11 +100,11 @@ impl TuiCommandMenu { } pub struct TuiMenu<'a> { - options: &'a Vec<&'a str>, + options: &'a [&'a str], } impl<'a> TuiMenu<'a> { - pub fn new(options: &'a Vec<&str>) -> Self { + pub fn new(options: &'a [&'a str]) -> Self { Self { options } } diff --git a/src/util/key_mapping.rs b/src/util/key_mapping.rs index 4b0dbc3..31f20a8 100644 --- a/src/util/key_mapping.rs +++ b/src/util/key_mapping.rs @@ -39,14 +39,14 @@ pub fn str_to_key(s: &str) -> Option { } if s.starts_with("ctrl+") { - let ch = s.chars().skip("ctrl+".len()).next(); + let ch = s.chars().nth("ctrl+".len()); let key = match ch { Some(ch) => Some(Key::Ctrl(ch)), None => None, }; return key; } else if s.starts_with("alt+") { - let ch = s.chars().skip("alt+".len()).next(); + let ch = s.chars().nth("alt+".len()); let key = match ch { Some(ch) => Some(Key::Alt(ch)), None => None, @@ -60,5 +60,5 @@ pub fn str_to_key(s: &str) -> Option { }; return key; } - return None; + None } -- cgit v1.2.3