diff --git a/src/options/view.rs b/src/options/view.rs index dd55b8d..1e65b0a 100644 --- a/src/options/view.rs +++ b/src/options/view.rs @@ -1,5 +1,6 @@ use output::Colours; -use output::{View, Mode, grid, details, grid_details}; +use output::{View, Mode, grid, details}; +use output::grid_details::{self, RowThreshold}; use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions}; use output::file_name::{Classify, FileStyle}; use output::time::TimeFormat; @@ -178,6 +179,24 @@ impl TerminalWidth { } +impl RowThreshold { + + /// Determine whether to use a row threshold based on the given + /// environment variables. + fn deduce(vars: &V) -> Result { + if let Some(columns) = vars.get("EXA_GRID_ROWS").and_then(|s| s.into_string().ok()) { + match columns.parse() { + Ok(rows) => Ok(RowThreshold::MinimumRows(rows)), + Err(e) => Err(Misfire::FailedParse(e)), + } + } + else { + Ok(RowThreshold::AlwaysGrid) + } + } +} + + impl TableOptions { fn deduce(matches: &MatchedFlags) -> Result { let env = Environment::load_all(); diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index 59d0d7a..776c369 100644 --- a/src/output/grid_details.rs +++ b/src/output/grid_details.rs @@ -22,7 +22,25 @@ use output::tree::{TreeParams, TreeDepth}; pub struct Options { pub grid: GridOptions, pub details: DetailsOptions, - pub row_threshold: Option, + pub row_threshold: RowThreshold, +} + +/// The grid-details view can be configured to revert to just a details view +/// (with one column) if it wouldn’t produce enough rows of output. +/// +/// Doing this makes the resulting output look a bit better: when listing a +/// small directory of four files in four columns, the files just look spaced +/// out and it’s harder to see what’s going on. So it can be enabled just for +/// larger directory listings. +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum RowThreshold { + + /// Only use grid-details view if it would result in at least this many + /// rows of output. + MinimumRows(usize), + + /// Use the grid-details view no matter what. + AlwaysGrid, } @@ -55,7 +73,7 @@ pub struct Render<'a> { /// The minimum number of rows that there need to be before grid-details /// mode is activated. - pub row_threshold: Option, + pub row_threshold: RowThreshold, } impl<'a> Render<'a> { @@ -135,7 +153,7 @@ impl<'a> Render<'a> { // If we’ve figured out how many columns can fit in the user’s // terminal, and it turns out there aren’t enough rows to // make it worthwhile, then just resort to the lines view. - if let Some(thresh) = self.row_threshold { + if let RowThreshold::MinimumRows(thresh) = self.row_threshold { if last_working_table.fit_into_columns(column_count - 1).row_count() < thresh { return None; }