diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-02-16 10:58:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-16 10:58:11 -0500 |
commit | 130f54a49ca0a9852c488b7583d6cdf053855263 (patch) | |
tree | f9b58da3f2a107d136a66c61d08dead883965864 | |
parent | 055ac602929af367da2af77999e73de6bf8fd344 (diff) | |
parent | 8b04fec104cd20b86ff98f4da225bb866a407f8c (diff) |
Merge pull request #24 from cjbassi/clippy
Fix most clippy lints
-rw-r--r-- | src/commands/file_operations.rs | 17 | ||||
-rw-r--r-- | src/commands/mod.rs | 18 | ||||
-rw-r--r-- | src/commands/new_directory.rs | 2 | ||||
-rw-r--r-- | src/commands/open_file.rs | 60 | ||||
-rw-r--r-- | src/commands/parent_directory.rs | 2 | ||||
-rw-r--r-- | src/commands/set_mode.rs | 4 | ||||
-rw-r--r-- | src/commands/tab_operations.rs | 2 | ||||
-rw-r--r-- | src/commands/tab_switch.rs | 4 | ||||
-rw-r--r-- | src/config/config.rs | 2 | ||||
-rw-r--r-- | src/config/keymap.rs | 44 | ||||
-rw-r--r-- | src/config/mimetype.rs | 6 | ||||
-rw-r--r-- | src/config/preview.rs | 6 | ||||
-rw-r--r-- | src/config/theme.rs | 4 | ||||
-rw-r--r-- | src/history.rs | 7 | ||||
-rw-r--r-- | src/run.rs | 12 | ||||
-rw-r--r-- | src/sort.rs | 2 | ||||
-rw-r--r-- | src/structs.rs | 4 | ||||
-rw-r--r-- | src/tab.rs | 18 | ||||
-rw-r--r-- | src/textfield.rs | 47 | ||||
-rw-r--r-- | src/ui.rs | 55 | ||||
-rw-r--r-- | src/unix.rs | 11 | ||||
-rw-r--r-- | src/window/panel.rs | 4 |
22 files changed, 145 insertions, 186 deletions
diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs index 53936a0..3fbd6aa 100644 --- a/src/commands/file_operations.rs +++ b/src/commands/file_operations.rs @@ -49,7 +49,7 @@ fn repopulated_selected_files(dirlist: &JoshutoDirList) -> bool { *data = contents; return true; } - return false; + false } enum FileOp { @@ -165,9 +165,8 @@ impl PasteFiles { } let (tx, rx) = sync::mpsc::channel(); - let handle; - if dest_ino == path_ino { - handle = thread::spawn(move || { + let handle = if dest_ino == path_ino { + thread::spawn(move || { let mut paths = selected_files.lock().unwrap(); let mut progress_info = ProgressInfo { bytes_finished: 1, @@ -189,14 +188,14 @@ impl PasteFiles { std::fs::rename(&path, &destination).unwrap(); destination.pop(); - progress_info.bytes_finished = progress_info.bytes_finished + 1; + progress_info.bytes_finished += 1; tx.send(progress_info.clone()).unwrap(); } paths.clear(); 0 - }); + }) } else { - handle = thread::spawn(move || { + thread::spawn(move || { let mut paths = selected_files.lock().unwrap(); let handle = |process_info: fs_extra::TransitProcess| { @@ -211,8 +210,8 @@ impl PasteFiles { fs_extra::move_items_with_progress(&paths, &destination, &options, handle).unwrap(); paths.clear(); 0 - }); - } + }) + }; let thread = FileOperationThread { tab_src: tab_src_index, tab_dest, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 954dbdc..e7b4df3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -76,7 +76,7 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo if let Some(args) = args { match wordexp::wordexp(args[0].as_str(), 0) { Ok(exp_strs) => { - for exp_str in exp_strs { + if let Some(exp_str) = exp_strs.into_iter().next() { let path = PathBuf::from(exp_str); return Some(Box::new(self::ChangeDirectory::new(path))); } @@ -86,13 +86,13 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo } } } - return None; + None } "close_tab" => Some(Box::new(self::CloseTab::new())), "copy_files" => Some(Box::new(self::CopyFiles::new())), "cursor_move" => { if let Some(args) = args { - if args.len() > 0 { + if !args.is_empty() { match args[0].parse::<i32>() { Ok(s) => { return Some(Box::new(self::CursorMove::new(s))); @@ -103,7 +103,7 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo } } } - return None; + None } "cursor_move_home" => Some(Box::new(self::CursorMoveHome::new())), "cursor_move_end" => Some(Box::new(self::CursorMoveEnd::new())), @@ -150,7 +150,7 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo "rename_file" => { let method: RenameFileMethod; if let Some(args) = args { - if args.len() > 0 { + if !args.is_empty() { method = match args[0].as_str() { "prepend" => self::RenameFileMethod::Prepend, "overwrite" => self::RenameFileMethod::Overwrite, @@ -198,7 +198,7 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo "set_mode" => Some(Box::new(self::SetMode::new())), "tab_switch" => { if let Some(args) = args { - if args.len() > 0 { + if !args.is_empty() { match args[0].parse::<i32>() { Ok(s) => { return Some(Box::new(self::TabSwitch::new(s))); @@ -209,7 +209,7 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo } } } - return None; + None } "toggle_hidden" => Some(Box::new(self::ToggleHiddenFiles::new())), _ => None, @@ -227,7 +227,7 @@ pub fn collect_selected_paths(dirlist: &structs::JoshutoDirList) -> Option<Vec<P .filter(|entry| entry.selected) .map(|entry| entry.path.clone()) .collect(); - if selected.len() > 0 { + if !selected.is_empty() { Some(selected) } else { Some(vec![dirlist.contents[dirlist.index as usize].path.clone()]) @@ -235,7 +235,7 @@ pub fn collect_selected_paths(dirlist: &structs::JoshutoDirList) -> Option<Vec<P } #[allow(dead_code)] -pub fn split_shell_style(line: &String) -> Vec<&str> { +pub fn split_shell_style(line: &str) -> Vec<&str> { let mut args: Vec<&str> = Vec::new(); let mut char_ind = line.char_indices(); diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index 35a3b18..2ff5701 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -30,7 +30,7 @@ impl std::fmt::Display for NewDirectory { impl JoshutoRunnable for NewDirectory { fn execute(&self, context: &mut JoshutoContext) { let (term_rows, term_cols) = ui::getmaxyx(); - const PROMPT: &'static str = ":mkdir "; + const PROMPT: &str = ":mkdir "; let user_input: Option<String>; diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 9f4d88e..3facb4f 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -3,7 +3,7 @@ extern crate ncurses; extern crate open; use std::env; -use std::path; +use std::path::{Path, PathBuf}; use commands::{self, JoshutoCommand, JoshutoRunnable}; use config::mimetype; @@ -27,40 +27,30 @@ impl OpenFile { "open_file" } - pub fn get_options<'a>(path: &path::PathBuf) -> Vec<&'a mimetype::JoshutoMimetypeEntry> { + pub fn get_options<'a>(path: &Path) -> Vec<&'a mimetype::JoshutoMimetypeEntry> { let mut mimetype_options: Vec<&mimetype::JoshutoMimetypeEntry> = Vec::new(); - match path.extension() { - Some(file_ext) => { - if let Some(file_ext) = file_ext.to_str() { - match mimetype_t.extension.get(file_ext) { - Some(s) => { - for option in s { - mimetype_options.push(&option); - } - } - None => {} + if let Some(file_ext) = path.extension() { + if let Some(file_ext) = file_ext.to_str() { + if let Some(s) = mimetype_t.extension.get(file_ext) { + for option in s { + mimetype_options.push(&option); } } } - None => {} } let detective = mime_detective::MimeDetective::new().unwrap(); - match detective.detect_filepath(path) { - Ok(mime_type) => match mimetype_t.mimetype.get(mime_type.type_().as_str()) { - Some(s) => { - for option in s { - mimetype_options.push(&option); - } + if let Ok(mime_type) = detective.detect_filepath(path) { + if let Some(s) = mimetype_t.mimetype.get(mime_type.type_().as_str()) { + for option in s { + mimetype_options.push(&option); } - None => {} - }, - Err(_) => {} + } } mimetype_options } - fn into_directory(path: &path::PathBuf, context: &mut JoshutoContext) { + fn enter_directory(path: &Path, context: &mut JoshutoContext) { let curr_tab = &mut context.tabs[context.curr_tab_index]; match env::set_current_dir(path) { @@ -103,12 +93,12 @@ impl OpenFile { } } - fn into_file(paths: &Vec<path::PathBuf>) { + fn open_file(paths: &[PathBuf]) { let mimetype_options = Self::get_options(&paths[0]); ncurses::savetty(); ncurses::endwin(); - if mimetype_options.len() > 0 { + if !mimetype_options.is_empty() { unix::open_with_entry(paths, &mimetype_options[0]); } else { open::that(&paths[0]).unwrap(); @@ -129,7 +119,7 @@ impl std::fmt::Display for OpenFile { impl JoshutoRunnable for OpenFile { fn execute(&self, context: &mut JoshutoContext) { - let mut path: Option<path::PathBuf> = None; + let mut path: Option<PathBuf> = None; if let Some(curr_list) = context.tabs[context.curr_tab_index].curr_list.as_ref() { if let Some(entry) = curr_list.get_curr_ref() { if entry.path.is_dir() { @@ -138,7 +128,7 @@ impl JoshutoRunnable for OpenFile { } } if let Some(path) = path { - Self::into_directory(&path, context); + Self::enter_directory(&path, context); { let curr_tab = &mut context.tabs[context.curr_tab_index]; curr_tab.refresh( @@ -151,14 +141,14 @@ impl JoshutoRunnable for OpenFile { preview::preview_file(context); ncurses::doupdate(); } else { - let paths: Option<Vec<path::PathBuf>> = + let paths: Option<Vec<PathBuf>> = match context.tabs[context.curr_tab_index].curr_list.as_ref() { Some(s) => commands::collect_selected_paths(s), None => None, }; if let Some(paths) = paths { - if paths.len() > 0 { - Self::into_file(&paths); + if !paths.is_empty() { + Self::open_file(&paths); } else { ui::wprint_msg(&context.views.bot_win, "No files selected: 0"); } @@ -181,7 +171,7 @@ impl OpenFileWith { "open_file_with" } - pub fn open_with(paths: &Vec<path::PathBuf>) { + pub fn open_with(paths: &[PathBuf]) { const PROMPT: &str = ":open_with "; let mimetype_options: Vec<&mimetype::JoshutoMimetypeEntry> = @@ -218,7 +208,7 @@ impl OpenFileWith { ncurses::doupdate(); if let Some(user_input) = user_input { - if user_input.len() == 0 { + if user_input.is_empty() { return; } match user_input.parse::<usize>() { @@ -232,10 +222,8 @@ impl OpenFileWith { } } Err(_) => { - let args: Vec<String> = user_input - .split_whitespace() - .map(|x| String::from(x)) - .collect(); + let args: Vec<String> = + user_input.split_whitespace().map(String::from).collect(); ncurses::savetty(); ncurses::endwin(); unix::open_with_args(&paths, &args); diff --git a/src/commands/parent_directory.rs b/src/commands/parent_directory.rs index d9066c6..d158d3b 100644 --- a/src/commands/parent_directory.rs +++ b/src/commands/parent_directory.rs @@ -17,7 +17,7 @@ impl ParentDirectory { } pub fn parent_directory(context: &mut JoshutoContext) { - if context.curr_tab_mut().curr_path.pop() == false { + if !context.curr_tab_mut().curr_path.pop() { return; } diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index b0cf073..061b5c3 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -53,14 +53,14 @@ impl SetMode { let mut mode: libc::mode_t = 0; for (i, ch) in s.chars().enumerate() { if ch == LIBC_PERMISSION_VALS[i].1 { - mode = mode | LIBC_PERMISSION_VALS[i].0; + mode |= LIBC_PERMISSION_VALS[i].0; } } unix::set_mode(entry.path.as_path(), mode); entry.metadata.permissions.set_mode(mode + (1 << 15)); return true; } - return false; + false } } diff --git a/src/commands/tab_operations.rs b/src/commands/tab_operations.rs index b35c375..accf369 100644 --- a/src/commands/tab_operations.rs +++ b/src/commands/tab_operations.rs @@ -72,7 +72,7 @@ impl CloseTab { context.tabs.remove(context.curr_tab_index); if context.curr_tab_index > 0 { - context.curr_tab_index = context.curr_tab_index - 1; + context.curr_tab_index -= 1; } TabSwitch::tab_switch(context.curr_tab_index as i32, context); } diff --git a/src/commands/tab_switch.rs b/src/commands/tab_switch.rs index 583347e..719b9cb 100644 --- a/src/commands/tab_switch.rs +++ b/src/commands/tab_switch.rs @@ -47,10 +47,10 @@ impl JoshutoRunnable for TabSwitch { let mut new_index = context.curr_tab_index as i32 + self.movement; let tab_len = context.tabs.len() as i32; while new_index < 0 { - new_index = new_index + tab_len; + new_index += tab_len; } while new_index >= tab_len { - new_index = new_index - tab_len; + new_index -= tab_len; } Self::tab_switch(new_index, context); } diff --git a/src/config/config.rs b/src/config/config.rs index b970c16..6e9e738 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -120,6 +120,6 @@ impl JoshutoConfig { pub fn get_config() -> JoshutoConfig { parse_config_file::<JoshutoRawConfig, JoshutoConfig>(CONFIG_FILE) - .unwrap_or_else(|| JoshutoConfig::new()) + .unwrap_or_else(JoshutoConfig::new) } } diff --git a/src/config/keymap.rs b/src/config/keymap.rs index 639c33c..c71ef8d 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -60,7 +60,7 @@ impl JoshutoKeymap { pub fn get_config() -> JoshutoKeymap { parse_config_file::<JoshutoRawKeymap, JoshutoKeymap>(KEYMAP_FILE) - .unwrap_or_else(|| JoshutoKeymap::new()) + .unwrap_or_else(JoshutoKeymap::new) } } @@ -70,33 +70,27 @@ fn insert_keycommand( keys: &[String], ) { if keys.len() == 1 { - match key_to_i32(&keys[0]) { - Some(s) => { - map.insert(s, commands::CommandKeybind::SimpleKeybind(keycommand)); - } - None => {} + if let Some(s) = key_to_i32(&keys[0]) { + map.insert(s, commands::CommandKeybind::SimpleKeybind(keycommand)); } } else { - match key_to_i32(&keys[0]) { - Some(s) => { - let mut new_map: HashMap<i32, commands::CommandKeybind>; - match map.remove(&s) { - Some(commands::CommandKeybind::CompositeKeybind(mut m)) => { - new_map = m; - } - Some(_) => { - eprintln!("Error: Keybindings ambiguous"); - exit(1); - } - None => { - new_map = HashMap::new(); - } + if let Some(s) = key_to_i32(&keys[0]) { + let mut new_map: HashMap<i32, commands::CommandKeybind>; + match map.remove(&s) { + Some(commands::CommandKeybind::CompositeKeybind(mut m)) => { + new_map = m; + } + Some(_) => { + eprintln!("Error: Keybindings ambiguous"); + exit(1); + } + None => { + new_map = HashMap::new(); } - insert_keycommand(&mut new_map, keycommand, &keys[1..]); - let composite_command = commands::CommandKeybind::CompositeKeybind(new_map); - map.insert(s as i32, composite_command); } - None => {} + insert_keycommand(&mut new_map, keycommand, &keys[1..]); + let composite_command = commands::CommandKeybind::CompositeKeybind(new_map); + map.insert(s as i32, composite_command); } } } @@ -108,7 +102,7 @@ pub fn key_to_i32(keycode: &str) -> Option<i32> { return Some(ch as i32); } } - return None; + None } else { match keycode { "Tab" => Some(TAB), diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 1be211f..256aa9e 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -56,8 +56,8 @@ impl JoshutoRawMimetype { impl Flattenable<JoshutoMimetype> for JoshutoRawMimetype { fn flatten(self) -> JoshutoMimetype { - let mimetype = self.mimetype.unwrap_or(HashMap::new()); - let extension = self.extension.unwrap_or(HashMap::new()); + let mimetype = self.mimetype.unwrap_or_default(); + let extension = self.extension.unwrap_or_default(); JoshutoMimetype { mimetype, @@ -82,6 +82,6 @@ impl JoshutoMimetype { pub fn get_config() -> JoshutoMimetype { parse_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE) - .unwrap_or_else(|| JoshutoMimetype::new()) + .unwrap_or_else(JoshutoMimetype::new) } } diff --git a/src/config/preview.rs b/src/config/preview.rs index d8f3072..b24e5c9 100644 --- a/src/config/preview.rs +++ b/src/config/preview.rs @@ -30,8 +30,8 @@ impl JoshutoRawPreview { impl Flattenable<JoshutoPreview> for JoshutoRawPreview { fn flatten(self) -> JoshutoPreview { - let mimetype = self.mimetype.unwrap_or(HashMap::new()); - let extension = self.extension.unwrap_or(HashMap::new()); + let mimetype = self.mimetype.unwrap_or_default(); + let extension = self.extension.unwrap_or_default(); JoshutoPreview { mimetype, @@ -56,6 +56,6 @@ impl JoshutoPreview { pub fn get_config() -> JoshutoPreview { parse_config_file::<JoshutoRawPreview, JoshutoPreview>(PREVIEW_FILE) - .unwrap_or_else(|| JoshutoPreview::new()) + .unwrap_or_else(JoshutoPreview::new) } } diff --git a/src/config/theme.rs b/src/config/theme.rs index 2e27663..0c43ed8 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -114,7 +114,7 @@ impl Flattenable<JoshutoTheme> for JoshutoRawTheme { prefixsize: None, }); - let mut extraw = self.ext.unwrap_or(HashMap::new()); + let mut extraw = self.ext.unwrap_or_default(); let mut ext: HashMap<String, JoshutoColorTheme> = HashMap::new(); for (k, v) in extraw.drain() { ext.insert(k, v.flatten()); @@ -225,6 +225,6 @@ impl JoshutoTheme { pub fn get_config() -> JoshutoTheme { parse_config_file::<JoshutoRawTheme, JoshutoTheme>(::THEME_FILE) - .unwrap_or_else(|| JoshutoTheme::new()) + .unwrap_or_else(JoshutoTheme::new) } } diff --git a/src/history.rs b/src/history.rs index 8f0a270..bc1fe7c 100644 --- a/src/history.rs +++ b/src/history.rs @@ -18,7 +18,7 @@ impl DirHistory { pub fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_type: &sort::SortType) { let mut ancestors = pathbuf.ancestors(); if let Some(mut ancestor) = ancestors.next() { - while let Some(curr) = ancestors.next() { + for curr in ancestors { match structs::JoshutoDirList::new(curr.to_path_buf().clone(), sort_type) { Ok(mut s) => { for (i, dirent) in s.contents.iter().enumerate() { @@ -49,7 +49,7 @@ impl DirHistory { Ok(dir_entry) } None => { - let path_clone = path.clone().to_path_buf(); + let path_clone = path.to_path_buf(); structs::JoshutoDirList::new(path_clone, &sort_type) } } @@ -69,8 +69,7 @@ impl DirHistory { } } Entry::Vacant(entry) => { - if let Ok(s) = structs::JoshutoDirList::new(path.clone().to_path_buf(), &sort_type) - { + if let Ok(s) = structs::JoshutoDirList::new(path.to_path_buf(), &sort_type) { entry.insert(s); } } @@ -11,8 +11,8 @@ use preview; use ui; use window::JoshutoPanel; -fn recurse_get_keycommand<'a>( - keymap: &'a HashMap<i32, CommandKeybind>, +fn recurse_get_keycommand( + keymap: &HashMap<i32, CommandKeybind>, ) -> Option<&Box<dyn JoshutoCommand>> { let (term_rows, term_cols) = ui::getmaxyx(); ncurses::timeout(-1); @@ -91,7 +91,7 @@ fn process_threads(context: &mut JoshutoContext) { as f32; ui::draw_progress_bar(&context.views.bot_win, percent); ncurses::wnoutrefresh(context.views.bot_win.win); - i = i + 1; + i += 1; } ncurses::doupdate(); } @@ -125,7 +125,7 @@ fn process_threads(context: &mut JoshutoContext) { ncurses::doupdate(); } Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { - i = i + 1; + i += 1; } } } @@ -155,7 +155,7 @@ pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { ncurses::doupdate(); loop { - if context.threads.len() > 0 { + if !context.threads.is_empty() { ncurses::timeout(0); process_threads(&mut context); } else { @@ -174,7 +174,7 @@ pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { continue; } - let keycommand: &std::boxed::Box<dyn JoshutoCommand>; + let keycommand: &Box<dyn JoshutoCommand>; match keymap_t.keymaps.get(&ch) { Some(CommandKeybind::CompositeKeybind(m)) => match recurse_get_keycommand(&m) { diff --git a/src/sort.rs b/src/sort.rs index 9ae8778..b1f01fc 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -103,7 +103,7 @@ fn filter_hidden_files( match result { Ok(direntry) => match direntry.file_name().into_string() { Ok(file_name) => { - if file_name.starts_with(".") { + if file_name.starts_with('.') { None } else { match structs::JoshutoDirEntry::from(&direntry) { diff --git a/src/structs.rs b/src/structs.rs index 1743dc5..070e7b3 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -91,7 +91,7 @@ impl JoshutoDirList { let mut contents = Self::read_dir_list(path.as_path(), sort_type)?; contents.sort_by(&sort_type.compare_func()); - let index = if contents.len() > 0 { 0 } else { -1 }; + let index = if !contents.is_empty() { 0 } else { -1 }; let metadata = fs::metadata(&path)?; let metadata = JoshutoMetadata::from(&metadata)?; @@ -126,7 +126,7 @@ impl JoshutoDirList { return self.metadata.modified < modified; } } - return true; + true } pub fn update_contents(&mut self, sort_type: &sort::SortType) -> Result<(), std::io::Error> { @@ -42,24 +42,18 @@ impl JoshutoTab { pub fn reload_contents(&mut self, sort_type: &sort::SortType) { let mut list = self.curr_list.take(); - match list { - Some(ref mut s) => { - if s.path.exists() { - s.update_contents(sort_type).unwrap(); - } + if let Some(ref mut s) = list { + if s.path.exists() { + s.update_contents(sort_type).unwrap(); } - None => {} }; self.curr_list = list; list = self.parent_list.take(); - match list { - Some(ref mut s) => { - if s.path.exists() { - s.update_contents(sort_type).unwrap(); - } + if let Some(ref mut s) = list { + if s.path.exists() { + s.update_contents(sort_type).unwrap(); } - None => {} }; self.parent_list = list; } diff --git a/src/textfield.rs b/src/textfield.rs index 503097f..51c634b 100644 --- a/src/textfield.rs +++ b/src/textfield.rs @@ -2,6 +2,7 @@ extern crate ncurses; extern crate unicode_width; use config::keymap; +use std::iter::FromIterator; use window; pub struct JoshutoTextField { @@ -24,7 +25,7 @@ impl JoshutoTextField { for ch in prefix.chars() { let char_len = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1); buf_vec.push(ch); - curs_x = curs_x + char_len as i32; + curs_x += char_len as i32; } let curr_index: usize = buf_vec.len(); @@ -32,8 +33,7 @@ impl JoshutoTextField { buf_vec.push(ch); } ncurses::timeout(-1); - let user_input = self.readline_(buf_vec, curs_x, curr_index); - user_input + self.readline_(buf_vec, curs_x, curr_index) } fn readline_( @@ -56,7 +56,7 @@ impl JoshutoTextField { loop { ncurses::wmove(win, coord.0, coord.1 as i32); { - let str_ch: String = buffer.iter().collect(); + let str_ch: String = String::from_iter(&buffer); ncurses::waddstr(win, &str_ch); } ncurses::waddstr(win, " "); @@ -82,25 +82,23 @@ impl JoshutoTextField { } else if ch == ncurses::KEY_END { let buffer_len = buffer.len(); if curr_index != buffer_len { - for i in curr_index..buffer_len { - curs_x = curs_x + buffer[i] as i32; + for x in &buffer { + curs_x += *x as i32; } curr_index = buffer_len; } } else if ch == ncurses::KEY_LEFT { if curr_index > 0 { - curr_index = curr_index - 1; - curs_x = curs_x - - unicode_width::UnicodeWidthChar::width(buffer[curr_index]).unwrap_or(1) - as i32; + curr_index -= 1; + curs_x -= unicode_width::UnicodeWidthChar::width(buffer[curr_index]) + .unwrap_or(1) as i32; } } else if ch == ncurses::KEY_RIGHT { let buffer_len = buffer.len(); if curr_index < buffer_len { - curs_x = curs_x - + unicode_width::UnicodeWidthChar::width(buffer[curr_index]).unwrap_or(1) - as i32; - curr_index = curr_index + 1; + curs_x += unicode_width::UnicodeWidthChar::width(buffer[curr_index]) + .unwrap_or(1) as i32; + curr_index += 1; } } else if ch == keymap::BACKSPACE { let buffer_len = buffer.len(); @@ -109,16 +107,14 @@ impl JoshutoTextField { } if curr_index == buffer_len { - curr_index = curr_index - 1; + curr_index -= 1; if let Some(ch) = buffer.pop() { - curs_x = - curs_x - unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as i32; + curs_x -= unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as i32; } } else if curr_index > 0 { - curr_index = curr_index - 1; + curr_index -= 1; let ch = buffer.remove(curr_index); - curs_x = - curs_x - unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as i32; + curs_x -= unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as i32; } } else if ch == ncurses::KEY_DC { let buffer_len = buffer.len(); @@ -130,9 +126,8 @@ impl JoshutoTextField { if curr_index > 0 { let ch = buffer.remove(curr_index); |