Move Git shared state to structs

This was being passed around everywhere as a parameter, when it can exist just as nicely as a struct field. This means many functions can take one argument less.
This commit is contained in:
Benjamin Sago 2020-10-13 01:28:42 +01:00
parent ecbe63bf1e
commit 80f9806bbc
3 changed files with 36 additions and 28 deletions

View File

@ -246,8 +246,9 @@ impl<'args> Exa<'args> {
let recurse = self.options.dir_action.recurse_options();
let git_ignoring = self.options.filter.git_ignore == GitIgnore::CheckAndIgnore;
let r = details::Render { dir, files, colours, style, opts, filter, recurse, git_ignoring };
r.render(self.git.as_ref(), &mut self.writer)
let git = self.git.as_ref();
let r = details::Render { dir, files, colours, style, opts, filter, recurse, git_ignoring, git };
r.render(&mut self.writer)
}
Mode::GridDetails(ref opts) => {
@ -257,8 +258,9 @@ impl<'args> Exa<'args> {
let row_threshold = opts.row_threshold;
let git_ignoring = self.options.filter.git_ignore == GitIgnore::CheckAndIgnore;
let r = grid_details::Render { dir, files, colours, style, grid, details, filter, row_threshold, git_ignoring };
r.render(self.git.as_ref(), &mut self.writer)
let git = self.git.as_ref();
let r = grid_details::Render { dir, files, colours, style, grid, details, filter, row_threshold, git_ignoring, git };
r.render(&mut self.writer)
}
}
}

View File

@ -129,6 +129,8 @@ pub struct Render<'a> {
/// Whether we are skipping Git-ignored files.
pub git_ignoring: bool,
pub git: Option<&'a GitCache>,
}
@ -149,18 +151,18 @@ impl<'a> AsRef<File<'a>> for Egg<'a> {
impl<'a> Render<'a> {
pub fn render<W: Write>(self, mut git: Option<&'a GitCache>, w: &mut W) -> io::Result<()> {
pub fn render<W: Write>(mut self, w: &mut W) -> io::Result<()> {
let mut pool = Pool::new(num_cpus::get() as u32);
let mut rows = Vec::new();
if let Some(ref table) = self.opts.table {
match (git, self.dir) {
(Some(g), Some(d)) => if ! g.has_anything_for(&d.path) { git = None },
(Some(g), None) => if ! self.files.iter().any(|f| g.has_anything_for(&f.path)) { git = None },
match (self.git, self.dir) {
(Some(g), Some(d)) => if ! g.has_anything_for(&d.path) { self.git = None },
(Some(g), None) => if ! self.files.iter().any(|f| g.has_anything_for(&f.path)) { self.git = None },
(None, _) => {/* Keep Git how it is */},
}
let mut table = Table::new(table, git, self.colours);
let mut table = Table::new(table, self.git, self.colours);
if self.opts.header {
let header = table.header_row();
@ -171,14 +173,14 @@ impl<'a> Render<'a> {
// This is weird, but I cant find a way around it:
// https://internals.rust-lang.org/t/should-option-mut-t-implement-copy/3715/6
let mut table = Some(table);
self.add_files_to_table(&mut pool, &mut table, &mut rows, &self.files, git, TreeDepth::root());
self.add_files_to_table(&mut pool, &mut table, &mut rows, &self.files, TreeDepth::root());
for row in self.iterate_with_table(table.unwrap(), rows) {
writeln!(w, "{}", row.strings())?
}
}
else {
self.add_files_to_table(&mut pool, &mut None, &mut rows, &self.files, git, TreeDepth::root());
self.add_files_to_table(&mut pool, &mut None, &mut rows, &self.files, TreeDepth::root());
for row in self.iterate(rows) {
writeln!(w, "{}", row.strings())?
@ -190,7 +192,7 @@ impl<'a> Render<'a> {
/// Adds files to the table, possibly recursively. This is easily
/// parallelisable, and uses a pool of threads.
fn add_files_to_table<'dir, 'ig>(&self, pool: &mut Pool, table: &mut Option<Table<'a>>, rows: &mut Vec<Row>, src: &[File<'dir>], git: Option<&'ig GitCache>, depth: TreeDepth) {
fn add_files_to_table<'dir>(&self, pool: &mut Pool, table: &mut Option<Table<'a>>, rows: &mut Vec<Row>, src: &[File<'dir>], depth: TreeDepth) {
use std::sync::{Arc, Mutex};
use log::*;
use crate::fs::feature::xattr;
@ -306,7 +308,7 @@ impl<'a> Render<'a> {
rows.push(row);
if let Some(ref dir) = egg.dir {
for file_to_add in dir.files(self.filter.dot_filter, git, self.git_ignoring) {
for file_to_add in dir.files(self.filter.dot_filter, self.git, self.git_ignoring) {
match file_to_add {
Ok(f) => {
files.push(f);
@ -328,7 +330,7 @@ impl<'a> Render<'a> {
rows.push(self.render_error(&error, TreeParams::new(depth.deeper(), false), path));
}
self.add_files_to_table(pool, table, rows, &files, git, depth.deeper());
self.add_files_to_table(pool, table, rows, &files, depth.deeper());
continue;
}
}

View File

@ -78,6 +78,8 @@ pub struct Render<'a> {
/// Whether we are skipping Git-ignored files.
pub git_ignoring: bool,
pub git: Option<&'a GitCache>,
}
impl<'a> Render<'a> {
@ -98,6 +100,7 @@ impl<'a> Render<'a> {
recurse: None,
filter: self.filter,
git_ignoring: self.git_ignoring,
git: self.git,
}
}
@ -113,27 +116,28 @@ impl<'a> Render<'a> {
recurse: None,
filter: self.filter,
git_ignoring: self.git_ignoring,
git: self.git,
}
}
// This doesnt take an IgnoreCache even though the details one does
// because grid-details has no tree view.
pub fn render<W: Write>(self, git: Option<&GitCache>, w: &mut W) -> io::Result<()> {
if let Some((grid, width)) = self.find_fitting_grid(git) {
pub fn render<W: Write>(mut self, w: &mut W) -> io::Result<()> {
if let Some((grid, width)) = self.find_fitting_grid() {
write!(w, "{}", grid.fit_into_columns(width))
}
else {
self.give_up().render(git, w)
self.give_up().render(w)
}
}
pub fn find_fitting_grid(&self, git: Option<&GitCache>) -> Option<(grid::Grid, grid::Width)> {
pub fn find_fitting_grid(&mut self) -> Option<(grid::Grid, grid::Width)> {
let options = self.details.table.as_ref().expect("Details table options not given!");
let drender = self.details();
let (first_table, _) = self.make_table(options, git, &drender);
let (first_table, _) = self.make_table(options, &drender);
let rows = self.files.iter()
.map(|file| first_table.row_for_file(file, file_has_xattrs(file)))
@ -154,12 +158,12 @@ impl<'a> Render<'a> {
})
.collect::<Vec<_>>();
let mut last_working_table = self.make_grid(1, options, git, &file_names, rows.clone(), &drender);
let mut last_working_table = self.make_grid(1, options, &file_names, rows.clone(), &drender);
// If we cant fit everything in a grid 100 columns wide, then
// something has gone seriously awry
for column_count in 2..100 {
let grid = self.make_grid(column_count, options, git, &file_names, rows.clone(), &drender);
let grid = self.make_grid(column_count, options, &file_names, rows.clone(), &drender);
let the_grid_fits = {
let d = grid.fit_into_columns(column_count);
@ -186,14 +190,14 @@ impl<'a> Render<'a> {
None
}
fn make_table(&'a self, options: &'a TableOptions, mut git: Option<&'a GitCache>, drender: &DetailsRender) -> (Table<'a>, Vec<DetailsRow>) {
match (git, self.dir) {
(Some(g), Some(d)) => if ! g.has_anything_for(&d.path) { git = None },
(Some(g), None) => if ! self.files.iter().any(|f| g.has_anything_for(&f.path)) { git = None },
fn make_table(&mut self, options: &'a TableOptions, drender: &DetailsRender) -> (Table<'a>, Vec<DetailsRow>) {
match (self.git, self.dir) {
(Some(g), Some(d)) => if ! g.has_anything_for(&d.path) { self.git = None },
(Some(g), None) => if ! self.files.iter().any(|f| g.has_anything_for(&f.path)) { self.git = None },
(None, _) => {/* Keep Git how it is */},
}
let mut table = Table::new(options, git, self.colours);
let mut table = Table::new(options, self.git, self.colours);
let mut rows = Vec::new();
if self.details.header {
@ -205,10 +209,10 @@ impl<'a> Render<'a> {
(table, rows)
}
fn make_grid(&'a self, column_count: usize, options: &'a TableOptions, git: Option<&GitCache>, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
fn make_grid(&mut self, column_count: usize, options: &'a TableOptions, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
let mut tables = Vec::new();
for _ in 0 .. column_count {
tables.push(self.make_table(options, git, drender));
tables.push(self.make_table(options, drender));
}
let mut num_cells = rows.len();