Tie the row threshold to EXA_GRID_ROWS

This makes it its own type, rather than just another environment variable that’s easily missed.
This commit is contained in:
Benjamin Sago 2017-08-13 11:15:40 +01:00
parent 6740faa781
commit bcf5213cc8
2 changed files with 41 additions and 4 deletions

View File

@ -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<V: Vars>(vars: &V) -> Result<RowThreshold, Misfire> {
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<Self, Misfire> {
let env = Environment::load_all();

View File

@ -22,7 +22,25 @@ use output::tree::{TreeParams, TreeDepth};
pub struct Options {
pub grid: GridOptions,
pub details: DetailsOptions,
pub row_threshold: Option<usize>,
pub row_threshold: RowThreshold,
}
/// The grid-details view can be configured to revert to just a details view
/// (with one column) if it wouldnt 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 its harder to see whats 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<usize>,
pub row_threshold: RowThreshold,
}
impl<'a> Render<'a> {
@ -135,7 +153,7 @@ impl<'a> Render<'a> {
// If weve figured out how many columns can fit in the users
// terminal, and it turns out there arent 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;
}