Selaa lähdekoodia

Isolate and document the environment variables

Benjamin Sago 8 vuotta sitten
vanhempi
sitoutus
945fa1e83d
4 muutettua tiedostoa jossa 38 lisäystä ja 5 poistoa
  1. 2 1
      src/options/colours.rs
  2. 3 2
      src/options/mod.rs
  3. 27 0
      src/options/vars.rs
  4. 6 2
      src/options/view.rs

+ 2 - 1
src/options/colours.rs

@@ -64,6 +64,7 @@ impl Colours {
     where TW: Fn() -> Option<usize>, V: Vars {
         use self::TerminalColours::*;
         use output::lsc::LSColors;
+        use options::vars;
 
         let tc = TerminalColours::deduce(matches)?;
         if tc == Never || (tc == Automatic && widther().is_none()) {
@@ -73,7 +74,7 @@ impl Colours {
         let scale = matches.has_where(|f| f.matches(&flags::COLOR_SCALE) || f.matches(&flags::COLOUR_SCALE))?;
         let mut colours = Colours::colourful(scale.is_some());
 
-        if let Some(lsc) = vars.get("LS_COLORS") {
+        if let Some(lsc) = vars.get(vars::LS_COLORS) {
             let lsc = lsc.to_string_lossy();
             let lsc = LSColors::parse(lsc.as_ref());
 

+ 3 - 2
src/options/mod.rs

@@ -89,7 +89,7 @@ use self::version::VersionString;
 mod misfire;
 pub use self::misfire::Misfire;
 
-mod vars;
+pub mod vars;
 pub use self::vars::Vars;
 
 mod parser;
@@ -123,8 +123,9 @@ impl Options {
     where I: IntoIterator<Item=&'args OsString>,
           V: Vars {
         use options::parser::{Matches, Strictness};
+        use options::vars;
 
-        let strictness = match vars.get("EXA_STRICT") {
+        let strictness = match vars.get(vars::EXA_STRICT) {
             None                         => Strictness::UseLastArguments,
             Some(ref t) if t.is_empty()  => Strictness::UseLastArguments,
             _                            => Strictness::ComplainAboutRedundantArguments,

+ 27 - 0
src/options/vars.rs

@@ -1,6 +1,33 @@
 use std::ffi::OsString;
 
 
+// General variables
+
+/// Environment variable used to colour files, both by their filesystem type
+/// (symlink, socket, directory) and their file name or extension (image,
+/// video, archive);
+pub static LS_COLORS: &str = "LS_COLORS";
+
+/// Environment variable used to override the width of the terminal, in
+/// characters.
+pub static COLUMNS: &str = "COLUMNS";
+
+
+// exa-specific variables
+
+/// Environment variable used to switch on strict argument checking, such as
+/// complaining if an argument was specified twice, or if two conflict.
+/// This is meant to be so you don’t accidentally introduce the wrong
+/// behaviour in a script, rather than for general command-line use.
+pub static EXA_STRICT: &str = "EXA_STRICT";
+
+/// Environment variable used to limit the grid-details view
+/// (`--grid --long`) so it’s only activated if there’s at least the given
+/// number of rows of output.
+pub static EXA_GRID_ROWS: &str = "EXA_GRID_ROWS";
+
+
+
 /// Mockable wrapper for `std::env::var_os`.
 pub trait Vars {
     fn get(&self, name: &'static str) -> Option<OsString>;

+ 6 - 2
src/options/view.rs

@@ -155,7 +155,9 @@ impl TerminalWidth {
     ///
     /// Returns an error if a requested width doesn’t parse to an integer.
     fn deduce<V: Vars>(vars: &V) -> Result<TerminalWidth, Misfire> {
-        if let Some(columns) = vars.get("COLUMNS").and_then(|s| s.into_string().ok()) {
+        use options::vars;
+
+        if let Some(columns) = vars.get(vars::COLUMNS).and_then(|s| s.into_string().ok()) {
             match columns.parse() {
                 Ok(width)  => Ok(TerminalWidth::Set(width)),
                 Err(e)     => Err(Misfire::FailedParse(e)),
@@ -184,7 +186,9 @@ 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()) {
+        use options::vars;
+
+        if let Some(columns) = vars.get(vars::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)),