From 14144e2ad30e2e092d6b557eabb6886f6558eb73 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Mon, 26 Jun 2017 00:53:48 +0100 Subject: [PATCH] Create Render structs from views MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of having render methods on the types that are now called Options, create new Render structs (one per view) and execute them. This means that it’s easier to extract methods from them — some of them are pretty long. Also, remove the GridDetails struct, which got consumed by Mode (mostly) By introducing another indirection between the structs that command-line options get parsed into and the structs that get rendered, it should be easier to refactor that horrible function in view.rs. --- src/exa.rs | 13 +++++----- src/options/mod.rs | 6 ++--- src/options/view.rs | 18 +++++++------- src/output/details.rs | 50 ++++++++++++++++++++++---------------- src/output/grid.rs | 44 +++++++++++++++++++++------------ src/output/grid_details.rs | 50 ++++++++++++++++++++------------------ src/output/lines.rs | 25 ++++++++++++++----- src/output/mod.rs | 16 ++++++------ 8 files changed, 129 insertions(+), 93 deletions(-) diff --git a/src/exa.rs b/src/exa.rs index 48d3405..fbc0aea 100644 --- a/src/exa.rs +++ b/src/exa.rs @@ -31,7 +31,7 @@ use ansi_term::{ANSIStrings, Style}; use fs::{Dir, File}; use options::{Options, View, Mode}; pub use options::Misfire; -use output::{escape, lines}; +use output::{escape, lines, grid, grid_details, details}; mod fs; mod info; @@ -168,12 +168,13 @@ impl<'w, W: Write + 'w> Exa<'w, W> { /// printing differently... fn print_files(&mut self, dir: Option<&Dir>, files: Vec) -> IOResult<()> { if !files.is_empty() { - let View { ref mode, colours, classify } = self.options.view; + let View { ref mode, ref colours, classify } = self.options.view; + match *mode { - Mode::Grid(ref g) => g.view(&files, self.writer, &colours, classify), - Mode::Details(ref d) => d.view(dir, files, self.writer, &colours, classify), - Mode::GridDetails(ref gd) => gd.view(dir, files, self.writer, &colours, classify), - Mode::Lines => lines::view(files, self.writer, &colours, classify), + Mode::Lines => lines::Render { files, colours, classify }.render(self.writer), + Mode::Grid(ref opts) => grid::Render { files, colours, classify, opts }.render(self.writer), + Mode::Details(ref opts) => details::Render { dir, files, colours, classify, opts }.render(self.writer), + Mode::GridDetails(ref grid, ref details) => grid_details::Render { dir, files, colours, classify, grid, details }.render(self.writer), } } else { diff --git a/src/options/mod.rs b/src/options/mod.rs index 3954af6..a368f8a 100644 --- a/src/options/mod.rs +++ b/src/options/mod.rs @@ -3,7 +3,7 @@ use std::ffi::OsStr; use getopts; use fs::feature::xattr; -use output::{Details, GridDetails}; +use output::details; mod dir_action; pub use self::dir_action::{DirAction, RecurseOptions}; @@ -124,8 +124,8 @@ impl Options { /// results will end up being displayed. pub fn should_scan_for_git(&self) -> bool { match self.view.mode { - Mode::Details(Details { columns: Some(cols), .. }) | - Mode::GridDetails(GridDetails { details: Details { columns: Some(cols), .. }, .. }) => cols.should_scan_for_git(), + Mode::Details(details::Options { columns: Some(cols), .. }) | + Mode::GridDetails(_, details::Options { columns: Some(cols), .. }) => cols.should_scan_for_git(), _ => false, } } diff --git a/src/options/view.rs b/src/options/view.rs index 582e7a6..1633887 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -3,7 +3,7 @@ use std::env::var_os; use getopts; use output::Colours; -use output::{Grid, Details, GridDetails}; +use output::{grid, details}; use output::column::{Columns, TimeTypes, SizeFormat}; use output::file_name::Classify; use options::{FileFilter, DirAction, Misfire}; @@ -33,9 +33,9 @@ impl View { /// The **mode** is the “type” of output. #[derive(PartialEq, Debug, Clone)] pub enum Mode { - Details(Details), - Grid(Grid), - GridDetails(GridDetails), + Grid(grid::Options), + Details(details::Options), + GridDetails(grid::Options, details::Options), Lines, } @@ -53,7 +53,7 @@ impl Mode { Err(Useless("oneline", true, "long")) } else { - let details = Details { + let details = details::Options { columns: Some(Columns::deduce(matches)?), header: matches.opt_present("header"), recurse: dir_action.recurse_options(), @@ -97,7 +97,7 @@ impl Mode { } } else if matches.opt_present("tree") { - let details = Details { + let details = details::Options { columns: None, header: false, recurse: dir_action.recurse_options(), @@ -108,7 +108,7 @@ impl Mode { Ok(Mode::Details(details)) } else { - let grid = Grid { + let grid = grid::Options { across: matches.opt_present("across"), console_width: width, }; @@ -122,7 +122,7 @@ impl Mode { // fallback to the lines view. if matches.opt_present("tree") { - let details = Details { + let details = details::Options { columns: None, header: false, recurse: dir_action.recurse_options(), @@ -144,7 +144,7 @@ impl Mode { if let Mode::Details(details) = view { let others = other_options_scan()?; match others { - Mode::Grid(grid) => return Ok(Mode::GridDetails(GridDetails { grid, details })), + Mode::Grid(grid) => return Ok(Mode::GridDetails(grid, details)), _ => return Ok(others), }; } diff --git a/src/output/details.rs b/src/output/details.rs index 2e938c4..405fec9 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -113,7 +113,7 @@ use output::file_name::{FileName, LinkStyle, Classify}; /// Almost all the heavy lifting is done in a Table object, which handles the /// columns for each row. #[derive(PartialEq, Debug, Clone, Default)] -pub struct Details { +pub struct Options { /// A Columns object that says which columns should be included in the /// output in the general case. Directories themselves can pick which @@ -217,16 +217,22 @@ fn determine_time_zone() -> TZResult { TimeZone::from_file("/etc/localtime") } -impl Details { - /// Print the details of the given vector of files -- all of which will - /// have been read from the given directory, if present -- to stdout. - pub fn view(&self, dir: Option<&Dir>, files: Vec, w: &mut W, colours: &Colours, classify: Classify) -> IOResult<()> { +pub struct Render<'a> { + pub dir: Option<&'a Dir>, + pub files: Vec>, + pub colours: &'a Colours, + pub classify: Classify, + pub opts: &'a Options, +} + +impl<'a> Render<'a> { + pub fn render(&self, w: &mut W) -> IOResult<()> { // First, transform the Columns object into a vector of columns for // the current directory. - let columns_for_dir = match self.columns { - Some(cols) => cols.for_dir(dir), + let columns_for_dir = match self.opts.columns { + Some(cols) => cols.for_dir(self.dir), None => Vec::new(), }; @@ -236,18 +242,18 @@ impl Details { // Build the table to put rows in. let mut table = Table { columns: &*columns_for_dir, - colours: colours, - classify: classify, - xattr: self.xattr, + colours: self.colours, + classify: self.classify, + xattr: self.opts.xattr, env: env, rows: Vec::new(), }; // Next, add a header if the user requests it. - if self.header { table.add_header() } + if self.opts.header { table.add_header() } // Then add files to the table and print it out. - self.add_files_to_table(&mut table, files, 0); + self.add_files_to_table(&mut table, &self.files, 0); for cell in table.print_table() { writeln!(w, "{}", cell.strings())?; } @@ -257,7 +263,7 @@ impl Details { /// Adds files to the table, possibly recursively. This is easily /// parallelisable, and uses a pool of threads. - fn add_files_to_table<'dir, U: Users+Groups+Send>(&self, mut table: &mut Table, src: Vec>, depth: usize) { + fn add_files_to_table<'dir, U: Users+Groups+Send>(&self, mut table: &mut Table, src: &Vec>, depth: usize) { use num_cpus; use scoped_threadpool::Pool; use std::sync::{Arc, Mutex}; @@ -271,12 +277,12 @@ impl Details { xattrs: Vec, errors: Vec<(IOError, Option)>, dir: Option, - file: File<'a>, + file: &'a File<'a>, } impl<'a> AsRef> for Egg<'a> { fn as_ref(&self) -> &File<'a> { - &self.file + self.file } } @@ -288,6 +294,8 @@ impl Details { let file_eggs = file_eggs.clone(); let table = table.clone(); + let recurse = self.opts.recurse; + scoped.execute(move || { let mut errors = Vec::new(); let mut xattrs = Vec::new(); @@ -307,7 +315,7 @@ impl Details { let mut dir = None; - if let Some(r) = self.recurse { + if let Some(r) = recurse { if file.is_directory() && r.tree && !r.is_too_deep(depth) { if let Ok(d) = file.to_dir(false) { dir = Some(d); @@ -321,7 +329,7 @@ impl Details { } }); - self.filter.sort_files(&mut file_eggs); + self.opts.filter.sort_files(&mut file_eggs); let num_eggs = file_eggs.len(); for (index, egg) in file_eggs.into_iter().enumerate() { @@ -345,7 +353,7 @@ impl Details { } } - self.filter.filter_child_files(&mut files); + self.opts.filter.filter_child_files(&mut files); if !files.is_empty() { for xattr in egg.xattrs { @@ -356,7 +364,7 @@ impl Details { table.add_error(&error, depth + 1, false, path); } - self.add_files_to_table(table, files, depth + 1); + self.add_files_to_table(table, &files, depth + 1); continue; } } @@ -465,8 +473,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { self.rows.push(row); } - pub fn filename(&self, file: File, links: LinkStyle) -> TextCellContents { - FileName::new(&file, links, self.classify, &self.colours).paint() + pub fn filename(&self, file: &File, links: LinkStyle) -> TextCellContents { + FileName::new(file, links, self.classify, &self.colours).paint() } pub fn add_file_with_cells(&mut self, cells: Vec, name_cell: TextCell, depth: usize, last: bool) { diff --git a/src/output/grid.rs b/src/output/grid.rs index ab58bad..7f9637c 100644 --- a/src/output/grid.rs +++ b/src/output/grid.rs @@ -1,6 +1,6 @@ use std::io::{Write, Result as IOResult}; -use term_grid as grid; +use term_grid as tg; use fs::File; use output::colours::Colours; @@ -8,40 +8,52 @@ use output::file_name::{FileName, LinkStyle, Classify}; #[derive(PartialEq, Debug, Copy, Clone)] -pub struct Grid { +pub struct Options { pub across: bool, pub console_width: usize, } -impl Grid { - pub fn view(&self, files: &[File], w: &mut W, colours: &Colours, classify: Classify) -> IOResult<()> { - let direction = if self.across { grid::Direction::LeftToRight } - else { grid::Direction::TopToBottom }; +impl Options { + pub fn direction(&self) -> tg::Direction { + if self.across { tg::Direction::LeftToRight } + else { tg::Direction::TopToBottom } + } +} - let mut grid = grid::Grid::new(grid::GridOptions { - direction: direction, - filling: grid::Filling::Spaces(2), + +pub struct Render<'a> { + pub files: Vec>, + pub colours: &'a Colours, + pub classify: Classify, + pub opts: &'a Options, +} + +impl<'a> Render<'a> { + pub fn render(&self, w: &mut W) -> IOResult<()> { + let mut grid = tg::Grid::new(tg::GridOptions { + direction: self.opts.direction(), + filling: tg::Filling::Spaces(2), }); - grid.reserve(files.len()); + grid.reserve(self.files.len()); - for file in files.iter() { - let filename = FileName::new(file, LinkStyle::JustFilenames, classify, colours).paint(); + for file in self.files.iter() { + let filename = FileName::new(file, LinkStyle::JustFilenames, self.classify, self.colours).paint(); let width = filename.width(); - grid.add(grid::Cell { + grid.add(tg::Cell { contents: filename.strings().to_string(), width: *width, }); } - if let Some(display) = grid.fit_into_width(self.console_width) { + if let Some(display) = grid.fit_into_width(self.opts.console_width) { write!(w, "{}", display) } else { // File names too long for a grid - drop down to just listing them! - for file in files.iter() { - let name_cell = FileName::new(file, LinkStyle::JustFilenames, classify, colours).paint(); + for file in self.files.iter() { + let name_cell = FileName::new(file, LinkStyle::JustFilenames, self.classify, self.colours).paint(); writeln!(w, "{}", name_cell.strings())?; } Ok(()) diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index bed100c..933021a 100644 --- a/src/output/grid_details.rs +++ b/src/output/grid_details.rs @@ -11,29 +11,25 @@ use fs::feature::xattr::FileAttributes; use output::cell::TextCell; use output::column::Column; use output::colours::Colours; -use output::details::{Details, Table, Environment}; -use output::grid::Grid; +use output::details::{Table, Environment, Options as DetailsOptions}; +use output::grid::Options as GridOptions; use output::file_name::{Classify, LinkStyle}; -#[derive(PartialEq, Debug, Clone)] -pub struct GridDetails { - pub grid: Grid, - pub details: Details, +pub struct Render<'a> { + pub dir: Option<&'a Dir>, + pub files: Vec>, + pub colours: &'a Colours, + pub classify: Classify, + pub grid: &'a GridOptions, + pub details: &'a DetailsOptions, } -fn file_has_xattrs(file: &File) -> bool { - match file.path.attributes() { - Ok(attrs) => !attrs.is_empty(), - Err(_) => false, - } -} +impl<'a> Render<'a> { + pub fn render(&self, w: &mut W) -> IOResult<()> { -impl GridDetails { - pub fn view(&self, dir: Option<&Dir>, files: Vec, w: &mut W, colours: &Colours, classify: Classify) -> IOResult<()> - where W: Write { let columns_for_dir = match self.details.columns { - Some(cols) => cols.for_dir(dir), + Some(cols) => cols.for_dir(self.dir), None => Vec::new(), }; @@ -41,23 +37,23 @@ impl GridDetails { let (cells, file_names) = { - let first_table = self.make_table(env.clone(), &*columns_for_dir, colours, classify); + let first_table = self.make_table(env.clone(), &*columns_for_dir, self.colours, self.classify); - let cells = files.iter() + let cells = self.files.iter() .map(|file| first_table.cells_for_file(file, file_has_xattrs(file))) .collect::>(); - let file_names = files.into_iter() + let file_names = self.files.iter() .map(|file| first_table.filename(file, LinkStyle::JustFilenames).promote()) .collect::>(); (cells, file_names) }; - let mut last_working_table = self.make_grid(env.clone(), 1, &columns_for_dir, &file_names, cells.clone(), colours, classify); + let mut last_working_table = self.make_grid(env.clone(), 1, &columns_for_dir, &file_names, cells.clone(), self.colours, self.classify); for column_count in 2.. { - let grid = self.make_grid(env.clone(), column_count, &columns_for_dir, &file_names, cells.clone(), colours, classify); + let grid = self.make_grid(env.clone(), column_count, &columns_for_dir, &file_names, cells.clone(), self.colours, self.classify); let the_grid_fits = { let d = grid.fit_into_columns(column_count); @@ -75,7 +71,7 @@ impl GridDetails { Ok(()) } - fn make_table<'a>(&'a self, env: Arc>, columns_for_dir: &'a [Column], colours: &'a Colours, classify: Classify) -> Table { + fn make_table<'g>(&'g self, env: Arc>, columns_for_dir: &'g [Column], colours: &'g Colours, classify: Classify) -> Table { let mut table = Table { columns: columns_for_dir, colours, classify, env, @@ -87,7 +83,7 @@ impl GridDetails { table } - fn make_grid<'a>(&'a self, env: Arc>, column_count: usize, columns_for_dir: &'a [Column], file_names: &[TextCell], cells: Vec>, colours: &'a Colours, classify: Classify) -> grid::Grid { + fn make_grid<'g>(&'g self, env: Arc>, column_count: usize, columns_for_dir: &'g [Column], file_names: &[TextCell], cells: Vec>, colours: &'g Colours, classify: Classify) -> grid::Grid { let mut tables = Vec::new(); for _ in 0 .. column_count { tables.push(self.make_table(env.clone(), columns_for_dir, colours, classify)); @@ -159,3 +155,11 @@ fn divide_rounding_up(a: usize, b: usize) -> usize { if a % b != 0 { result += 1; } result } + + +fn file_has_xattrs(file: &File) -> bool { + match file.path.attributes() { + Ok(attrs) => !attrs.is_empty(), + Err(_) => false, + } +} diff --git a/src/output/lines.rs b/src/output/lines.rs index d963fe3..19a86d7 100644 --- a/src/output/lines.rs +++ b/src/output/lines.rs @@ -9,10 +9,23 @@ use super::colours::Colours; /// The lines view literally just displays each file, line-by-line. -pub fn view(files: Vec, w: &mut W, colours: &Colours, classify: Classify) -> IOResult<()> { - for file in files { - let name_cell = FileName::new(&file, LinkStyle::FullLinkPaths, classify, colours).paint(); - writeln!(w, "{}", ANSIStrings(&name_cell))?; - } - Ok(()) +pub struct Render<'a> { + pub files: Vec>, + pub colours: &'a Colours, + pub classify: Classify, +} + +impl<'a> Render<'a> { + pub fn render(&self, w: &mut W) -> IOResult<()> { + for file in &self.files { + let name_cell = self.render_file(file).paint(); + writeln!(w, "{}", ANSIStrings(&name_cell))?; + } + + Ok(()) + } + + fn render_file<'f>(&self, file: &'f File<'a>) -> FileName<'f, 'a> { + FileName::new(file, LinkStyle::FullLinkPaths, self.classify, self.colours) + } } diff --git a/src/output/mod.rs b/src/output/mod.rs index c698086..1ab9cae 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -1,18 +1,16 @@ pub use self::cell::{TextCell, TextCellContents, DisplayWidth}; pub use self::colours::Colours; -pub use self::details::Details; -pub use self::grid_details::GridDetails; -pub use self::grid::Grid; pub use self::escape::escape; -mod grid; -pub mod details; -pub mod lines; -mod grid_details; pub mod column; +pub mod details; +pub mod file_name; +pub mod grid_details; +pub mod grid; +pub mod lines; + mod cell; mod colours; -mod tree; -pub mod file_name; mod escape; mod render; +mod tree;