Просмотр исходного кода

Tie the row threshold to EXA_GRID_ROWS

This makes it its own type, rather than just another environment variable that’s easily missed.
Benjamin Sago 8 лет назад
Родитель
Сommit
bcf5213cc8
2 измененных файлов с 41 добавлено и 4 удалено
  1. 20 1
      src/options/view.rs
  2. 21 3
      src/output/grid_details.rs

+ 20 - 1
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<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();

+ 21 - 3
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<usize>,
+    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<usize>,
+    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;
                     }