From 945fa1e83d76030adf7779df1c110d91b1fb1c5a Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sat, 26 Aug 2017 21:19:06 +0100 Subject: [PATCH] Isolate and document the environment variables --- src/options/colours.rs | 3 ++- src/options/mod.rs | 5 +++-- src/options/vars.rs | 27 +++++++++++++++++++++++++++ src/options/view.rs | 8 ++++++-- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/options/colours.rs b/src/options/colours.rs index 64e2f96..06a978c 100644 --- a/src/options/colours.rs +++ b/src/options/colours.rs @@ -64,6 +64,7 @@ impl Colours { where TW: Fn() -> Option, 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()); diff --git a/src/options/mod.rs b/src/options/mod.rs index 5ef3bf1..89e7b4f 100644 --- a/src/options/mod.rs +++ b/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, 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, diff --git a/src/options/vars.rs b/src/options/vars.rs index 4116457..59b155d 100644 --- a/src/options/vars.rs +++ b/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; diff --git a/src/options/view.rs b/src/options/view.rs index 93aec33..b394d07 100644 --- a/src/options/view.rs +++ b/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(vars: &V) -> Result { - 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(vars: &V) -> Result { - 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)),