diff options
author | Canop <cano.petrole@gmail.com> | 2019-02-01 21:42:35 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2019-02-01 21:42:35 +0100 |
commit | cd97c2bb5d8efd65db56c7e6cc93601505a996cf (patch) | |
tree | 47dccf6578c903a08b5163da625170edcb52ff63 | |
parent | 6881cb8e153c7fe866e3ed45b1f394db82f7e5bf (diff) |
a few sacrifices to the Clippy god
-rw-r--r-- | src/app.rs | 6 | ||||
-rw-r--r-- | src/browser_states.rs | 18 | ||||
-rw-r--r-- | src/commands.rs | 10 | ||||
-rw-r--r-- | src/conf.rs | 2 | ||||
-rw-r--r-- | src/file_sizes.rs | 2 | ||||
-rw-r--r-- | src/flat_tree.rs | 5 | ||||
-rw-r--r-- | src/screen_text.rs | 4 | ||||
-rw-r--r-- | src/screens.rs | 12 | ||||
-rw-r--r-- | src/tree_build.rs | 34 | ||||
-rw-r--r-- | src/tree_views.rs | 204 | ||||
-rw-r--r-- | src/verbs.rs | 2 |
11 files changed, 153 insertions, 146 deletions
@@ -184,12 +184,6 @@ impl App { ) -> Result<Option<Launchable>, ProgramError> { let mut screen = Screen::new()?; - write!( - screen.stdout, - "{}{}", - termion::clear::All, - termion::cursor::Hide - )?; // create the initial state if let Some(bs) = BrowserState::new( diff --git a/src/browser_states.rs b/src/browser_states.rs index 07168c6..3f374ae 100644 --- a/src/browser_states.rs +++ b/src/browser_states.rs @@ -57,7 +57,7 @@ impl BrowserState { )) } fn page_height(screen: &Screen) -> i32 { - (screen.h as i32) - 2 + i32::from(screen.h) - 2 } pub fn displayed_tree(&self) -> &Tree { match &self.filtered_tree { @@ -257,10 +257,13 @@ impl AppState for BrowserState { ) } else { let line = &tree.lines[tree.selection]; - screen.write_status_text(match line.is_dir() { - true => "Hit <enter> to focus, or type a space then a verb", - false => "Hit <enter> to open the file, or type a space then a verb", - }) + screen.write_status_text( + if line.is_dir() { + "Hit <enter> to focus, or type a space then a verb" + } else { + "Hit <enter> to open the file, or type a space then a verb" + } + ) } } } @@ -279,10 +282,7 @@ impl AppState for BrowserState { color::Bg(color::AnsiValue::grayscale(1)), termion::clear::UntilNewline, color::Fg(color::AnsiValue::grayscale(15)), - match tree.options.show_hidden { - true => 'y', - false => 'n', - }, + if tree.options.show_hidden { 'y' } else { 'n' }, match tree.options.respect_git_ignore { OptionBool::Auto => 'a', OptionBool::Yes => 'y', diff --git a/src/commands.rs b/src/commands.rs index 9b8b562..38b250d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -78,9 +78,11 @@ impl CommandParts { impl Action { fn from(cp: &CommandParts, finished: bool) -> Action { if let Some(verb) = &cp.verb { - match finished { - false => Action::VerbEdit(String::from(verb.as_str())), - true => Action::Verb(String::from(verb.as_str())), + let verb = String::from(verb.as_str()); + if finished { + Action::Verb(verb) + } else { + Action::VerbEdit(verb) } } else if finished { Action::OpenSelection @@ -135,7 +137,7 @@ impl Command { // This specific syntax isn't definitive pub fn from(raw: String) -> Command { let parts = CommandParts::from(&raw); - let action = Action::from(&parts, raw.contains(":")); + let action = Action::from(&parts, raw.contains(':')); Command { raw, parts, action } } pub fn add_key(&mut self, key: Key) { diff --git a/src/conf.rs b/src/conf.rs index 49286dd..e927414 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -98,7 +98,7 @@ impl Conf { let name = match string_field(verb_value, "name") { Some(s) => s, None => { - if execution.starts_with(":") { + if execution.starts_with(':') { // we'll assume that this entry isn't a new verb definition // but just the addition of a shortcut for a built-in verb "unnamed".to_string() diff --git a/src/file_sizes.rs b/src/file_sizes.rs index 030dfa0..2b1482f 100644 --- a/src/file_sizes.rs +++ b/src/file_sizes.rs @@ -76,7 +76,7 @@ impl Size { /// format a number of bytes as a string /// (probably fast enough but not benchmarked) - pub fn to_string(&self) -> String { + pub fn to_string(self) -> String { let mut v = self.0; let mut i = 0; while v >= 9000 && i < SIZE_NAMES.len() - 1 { diff --git a/src/flat_tree.rs b/src/flat_tree.rs index 6801a1a..bd404ec 100644 --- a/src/flat_tree.rs +++ b/src/flat_tree.rs @@ -195,10 +195,7 @@ impl Tree { return false; } let line = &self.lines[line_index]; - match depth >= line.depth as usize { - true => false, - false => line.left_branchs[depth], - } + depth < usize::from(line.depth) && line.left_branchs[depth] } pub fn move_selection(&mut self, dy: i32, page_height: i32) { // only work for +1 or -1 diff --git a/src/screen_text.rs b/src/screen_text.rs index 616d287..ac5a2db 100644 --- a/src/screen_text.rs +++ b/src/screen_text.rs @@ -86,7 +86,7 @@ impl<'a, R> TextTable<'a, R> { width, }); } - fn compute_col_widths(&mut self, rows: &'a Vec<R>) { + fn compute_col_widths(&mut self, rows: &'a[R]) { for row in rows { for c in &mut self.cols { c.width = c.width.max((c.extract)(row).len()); @@ -96,7 +96,7 @@ impl<'a, R> TextTable<'a, R> { // write the table into the text. // Right now, to ease width computations, md transformation is done // only in the last column - pub fn write(&mut self, rows: &'a Vec<R>, text: &mut Text) { + pub fn write(&mut self, rows: &'a[R], text: &mut Text) { lazy_static! { static ref bar: String = format!( " {}│{} ", diff --git a/src/screens.rs b/src/screens.rs index c622a51..8fe4dfe 100644 --- a/src/screens.rs +++ b/src/screens.rs @@ -23,6 +23,7 @@ impl Screen { let stdout = AlternateScreen::from(stdout().into_raw_mode()?); let mut screen = Screen { w:0, h:0, stdout }; screen.read_size()?; + write!(screen.stdout, "{}", termion::cursor::Hide)?; Ok(screen) } pub fn read_size(&mut self) -> io::Result<()> { @@ -82,10 +83,10 @@ impl ScreenArea { // (note that this may lead to flickering) #[allow(dead_code)] pub fn draw_scrollbar(&self, screen: &mut Screen) -> io::Result<()> { - let h = (self.bottom as i32) - (self.top as i32) + 1; + let h = i32::from(self.bottom) - i32::from(self.top) + 1; if self.content_length > h { let sbh = h * h / self.content_length; - let sc = self.top as i32 + self.scroll * h / self.content_length; + let sc = i32::from(self.top) + self.scroll * h / self.content_length; write!( screen.stdout, "{}", @@ -109,7 +110,10 @@ impl ScreenArea { return None; } let sbh = h * h / self.content_length; - let sc = self.top as i32 + self.scroll * h / self.content_length; - Some((sc as u16, (sc + sbh).min(self.bottom as i32 - 1) as u16)) + let sc = i32::from(self.top) + self.scroll * h / self.content_length; + Some(( + sc as u16, + (sc + sbh).min(i32::from(self.bottom) - 1) as u16, + )) } } diff --git a/src/tree_build.rs b/src/tree_build.rs index feb5cf9..625bbdd 100644 --- a/src/tree_build.rs +++ b/src/tree_build.rs @@ -96,7 +96,7 @@ impl BLine { if options.pattern.is_some() { if let Some(m) = options.pattern.find(&name) { // we dope less deep entries - score = m.score + 10000 - (depth as i32); + score = m.score + 10000 - i32::from(depth); } else { has_match = false; } @@ -107,14 +107,7 @@ impl BLine { return BLineResult::Invalid; } }; - if file_type.is_file() { - if !has_match { - return BLineResult::FilteredOutByPattern; - } - if options.only_folders { - return BLineResult::FilteredOutAsNonFolder; - } - } else if file_type.is_symlink() { + if file_type.is_file() || file_type.is_symlink() { if !has_match { return BLineResult::FilteredOutByPattern; } @@ -322,13 +315,12 @@ impl TreeBuilder { ) -> Option<usize> { let bline = &mut self.blines[bline_idx]; if let Some(children) = &bline.children { - match bline.next_child_idx < children.len() { - true => { - let next_child = children[bline.next_child_idx]; - bline.next_child_idx += 1; - Some(next_child) - } - false => Option::None, + if bline.next_child_idx < children.len() { + let next_child = children[bline.next_child_idx]; + bline.next_child_idx += 1; + Some(next_child) + } else { + Option::None } } else { unreachable!(); @@ -466,15 +458,13 @@ impl TreeBuilder { } // makes a tree from the builder's specific structure - fn into_tree(&mut self, out_blines: &[usize]) -> Tree { + fn take(&mut self, out_blines: &[usize]) -> Tree { let mut lines: Vec<TreeLine> = Vec::new(); for idx in out_blines.iter() { if self.blines[*idx].has_match { // we need to count the children, so we load them - if self.blines[*idx].file_type.is_dir() { - if self.blines[*idx].children.is_none() { - self.load_children(*idx); - } + if self.blines[*idx].file_type.is_dir() && self.blines[*idx].children.is_none() { + self.load_children(*idx); } lines.push(self.blines[*idx].to_tree_line()); } @@ -500,7 +490,7 @@ impl TreeBuilder { match self.gather_lines(task_lifetime) { Some(out_blines) => { self.trim_excess(&out_blines); - Some(self.into_tree(&out_blines)) + Some(self.take(&out_blines)) } None => None, // interrupted } diff --git a/src/tree_views.rs b/src/tree_views.rs index 3a7987b..988c3c9 100644 --- a/src/tree_views.rs +++ b/src/tree_views.rs @@ -7,9 +7,19 @@ use users::{Groups, Users, UsersCache}; use crate::flat_tree::{LineType, Tree, TreeLine}; use crate::patterns::Pattern; use crate::screens::{Screen, ScreenArea}; +use crate::file_sizes::Size; pub trait TreeView { fn write_tree(&mut self, tree: &Tree) -> io::Result<()>; + fn write_line_size( + &mut self, + line: &TreeLine, + total_size: Size, + ) -> io::Result<()>; + fn write_mode( + &mut self, + mode: u32, + ) -> io::Result<()>; fn write_line_name( &mut self, line: &TreeLine, @@ -34,8 +44,7 @@ impl TreeView for Screen { max_user_name_len = max_user_name_len.max(user.name().to_string_lossy().len()); } if let Some(group) = users_cache.get_group_by_gid(line.uid) { - max_group_name_len = - max_group_name_len.max(group.name().to_string_lossy().len()); + max_group_name_len = max_group_name_len.max(group.name().to_string_lossy().len()); } } } @@ -61,103 +70,27 @@ impl TreeView for Screen { write!( self.stdout, "{}", - match line.left_branchs[depth as usize] { - true => match tree.has_branch(line_index + 1, depth as usize) { - true => match depth == line.depth - 1 { - true => "├──", - false => "│ ", - }, - false => "└──", - }, - false => " ", + if line.left_branchs[depth as usize] { + if tree.has_branch(line_index + 1, depth as usize) { + if depth == line.depth - 1 { + "├──" + } else { + "│ " + } + } else { + "└──" + } + } else { + " " }, )?; } if tree.options.show_sizes && line_index > 0 { - if let Some(s) = line.size { - let dr: usize = s.discreet_ratio(total_size, 8) as usize; - let s: Vec<char> = s.to_string().chars().collect(); - write!( - self.stdout, - "{}{}", - color::Bg(color::Magenta), - color::Fg(color::AnsiValue::grayscale(15)), - )?; - for i in 0..dr { - write!(self.stdout, "{}", if i < s.len() { s[i] } else { ' ' })?; - } - write!(self.stdout, "{}", color::Bg(color::Reset))?; - write!(self.stdout, "{}", color::Bg(color::AnsiValue::grayscale(2)))?; - for i in dr..8 { - write!(self.stdout, "{}", if i < s.len() { s[i] } else { ' ' })?; - } - write!( - self.stdout, - "{}{} ", - color::Bg(color::Reset), - color::Fg(color::Reset), - )?; - } else { - write!( - self.stdout, - "{}────────{} ", - color::Fg(color::AnsiValue::grayscale(5)), - color::Fg(color::Reset), - )?; - } + self.write_line_size(line, total_size)?; } if tree.options.show_permissions && line_index > 0 { if line.is_selectable() { - write!( - self.stdout, - "{} {}{}{}{}{}{}{}{}{}", - color::Fg(color::AnsiValue::grayscale(15)), - if (line.mode & (1 << 8)) != 0 { - 'r' - } else { - '-' - }, - if (line.mode & (1 << 7)) != 0 { - 'w' - } else { - '-' - }, - if (line.mode & (1 << 6)) != 0 { - 'x' - } else { - '-' - }, - if (line.mode & (1 << 5)) != 0 { - 'r' - } else { - '-' - }, - if (line.mode & (1 << 4)) != 0 { - 'w' - } else { - '-' - }, - if (line.mode & (1 << 3)) != 0 { - 'x' - } else { - '-' - }, - if (line.mode & (1 << 2)) != 0 { - 'r' - } else { - '-' - }, - if (line.mode & (1 << 1)) != 0 { - 'w' - } else { - '-' - }, - if (line.mode & (1 << 0)) != 0 { - 'x' - } else { - '-' - }, - )?; + self.write_mode(line.mode)?; if let Some(user) = users_cache.get_user_by_uid(line.uid) { write!( self.stdout, @@ -207,6 +140,93 @@ impl TreeView for Screen { Ok(()) } + fn write_mode(&mut self, mode: u32) -> io::Result<()> { + write!( + self.stdout, + "{} {}{}{}{}{}{}{}{}{}", + color::Fg(color::AnsiValue::grayscale(15)), + if (mode & (1 << 8)) != 0 { + 'r' + } else { + '-' + }, + if (mode & (1 << 7)) != 0 { + 'w' + } else { + '-' + }, + if (mode & (1 << 6)) != 0 { + 'x' + } else { + '-' + }, + if (mode & (1 << 5)) != 0 { + 'r' + } else { + '-' + }, + if (mode & (1 << 4)) != 0 { + 'w' + } else { + '-' + }, + if (mode & (1 << 3)) != 0 { + 'x' + } else { + '-' + }, + if (mode & (1 << 2)) != 0 { + 'r' + } else { + '-' + }, + if (mode & (1 << 1)) != 0 { + 'w' + } else { + '-' + }, + if (mode & 1) != 0 { + 'x' + } else { + '-' + }, + ) + } + + fn write_line_size(&mut self, line: &TreeLine, total_size: Size) -> io::Result<()> { + if let Some(s) = line.size { + let dr: usize = s.discreet_ratio(total_size, 8) as usize; + let s: Vec<char> = s.to_string().chars().collect(); + write!( + self.stdout, + "{}{}", + color::Bg(color::Magenta), + color::Fg(color::AnsiValue::grayscale(15)), + )?; + for i in 0..dr { + write!(self.stdout, "{}", if i < s.len() { s[i] } else { ' ' })?; + } + write!(self.stdout, "{}", color::Bg(color::Reset))?; + write!(self.stdout, "{}", color::Bg(color::AnsiValue::grayscale(2)))?; + for i in dr..8 { + write!(self.stdout, "{}", if i < s.len() { s[i] } else { ' ' })?; + } + write!( + self.stdout, + "{}{} ", + color::Bg(color::Reset), + color::Fg(color::Reset), + ) + } else { + write!( + self.stdout, + "{}────────{} ", + color::Fg(color::AnsiValue::grayscale(5)), + color::Fg(color::Reset), + ) + } + } + fn write_line_name( &mut self, line: &TreeLine, diff --git a/src/verbs.rs b/src/verbs.rs index 65f367e..1d1e99d 100644 --- a/src/verbs.rs +++ b/src/verbs.rs @@ -49,7 +49,7 @@ impl Verb { ) -> Verb { Verb { name: name.to_string(), - short_key: short_key, + short_key, long_key: name.to_string(), exec_pattern: (format!(":{}", name)).to_string(), description: description.to_string(), |