Introduce EXA_ICON_SPACING environment variable

This commit remove the extra space that was added between icons and file names in commit 128fadd, and adds an option to put them back.

Re-fixes GH-619 and fixes GH-541.
This commit is contained in:
Benjamin Sago 2020-10-24 19:39:22 +01:00
parent c83359225b
commit 51be9f4c43
18 changed files with 389 additions and 271 deletions

View File

@ -208,6 +208,12 @@ Limits the grid-details view (`exa --grid --long`) so its only activate
With widescreen displays, its possible for the grid to look very wide and sparse, on just one or two lines with none of the columns lining up.
By specifying a minimum number of rows, you can only use the view if its going to be worth using.
## `EXA_ICON_SPACING`
Specifies the number of spaces to print between an icon (see the `--icons` option) and its file name.
Different terminals display icons differently, as they usually take up more than one character width on screen, so theres no “standard” number of spaces that exa can use to separate an icon from text. One space may place the icon too close to the text, and two spaces may place it too far away. So the choice is left up to the user to configure depending on their terminal emulator.
## `LS_COLORS`, `EXA_COLORS`
Specifies the colour scheme used to highlight files based on their name and kind, as well as highlighting metadata and parts of the UI.

View File

@ -1,15 +1,16 @@
use crate::options::{flags, OptionsError};
use crate::options::parser::MatchedFlags;
use crate::options::vars::{self, Vars};
use crate::output::file_name::{Options, Classify};
use crate::output::file_name::{Options, Classify, ShowIcons};
impl Options {
pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
let classify = Classify::deduce(matches)?;
let icons = matches.has(&flags::ICONS)?;
let show_icons = ShowIcons::deduce(matches, vars)?;
Ok(Self { classify, icons })
Ok(Self { classify, show_icons })
}
}
@ -21,3 +22,20 @@ impl Classify {
else { Ok(Self::JustFilenames) }
}
}
impl ShowIcons {
pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
if ! matches.has(&flags::ICONS)? {
Ok(Self::Off)
}
else if let Some(columns) = vars.get(vars::EXA_ICON_SPACING).and_then(|s| s.into_string().ok()) {
match columns.parse() {
Ok(width) => Ok(Self::On(width)),
Err(e) => Err(OptionsError::FailedParse(e)),
}
}
else {
Ok(Self::On(1))
}
}
}

View File

@ -39,6 +39,12 @@ pub static EXA_DEBUG: &str = "EXA_DEBUG";
/// number of rows of output.
pub static EXA_GRID_ROWS: &str = "EXA_GRID_ROWS";
/// Environment variable used to specify how many spaces to print between an
/// icon and its file name. Different terminals display icons differently,
/// with 1 space bringing them too close together or 2 spaces putting them too
/// far apart, so this may be necessary depending on how they are shown.
pub static EXA_ICON_SPACING: &str = "EXA_ICON_SPACING";
/// Mockable wrapper for `std::env::var_os`.
pub trait Vars {

View File

@ -12,7 +12,7 @@ impl View {
pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
let mode = Mode::deduce(matches, vars)?;
let width = TerminalWidth::deduce(vars)?;
let file_style = FileStyle::deduce(matches)?;
let file_style = FileStyle::deduce(matches, vars)?;
Ok(Self { mode, width, file_style })
}
}

View File

@ -18,7 +18,7 @@ pub struct Options {
pub classify: Classify,
/// Whether to prepend icon characters before file names.
pub icons: bool,
pub show_icons: ShowIcons,
}
impl Options {
@ -72,6 +72,19 @@ impl Default for Classify {
}
/// Whether and how to show icons.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum ShowIcons {
/// Dont show icons at all.
Off,
/// Show icons next to file names, with the given number of spaces between
/// the icon and the file name.
On(u32),
}
/// A **file name** holds all the information necessary to display the name
/// of the given file. This is used in all of the views.
pub struct FileName<'a, 'dir, C> {
@ -112,12 +125,17 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
pub fn paint(&self) -> TextCellContents {
let mut bits = Vec::new();
if self.options.icons {
if let ShowIcons::On(spaces_count) = self.options.show_icons {
let style = iconify_style(self.style());
let file_icon = icon_for_file(self.file).to_string();
bits.push(style.paint(file_icon));
bits.push(Style::default().paint(" "));
match spaces_count {
1 => bits.push(Style::default().paint(" ")),
2 => bits.push(Style::default().paint(" ")),
n => bits.push(Style::default().paint(spaces(n))),
}
}
if self.file.parent_dir.is_none() {
@ -152,7 +170,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
if ! target.name.is_empty() {
let target_options = Options {
classify: Classify::JustFilenames,
icons: false,
show_icons: ShowIcons::Off,
};
let target = FileName {
@ -320,3 +338,9 @@ pub trait Colours: FiletypeColours {
fn colour_file(&self, file: &File<'_>) -> Style;
}
/// Generate a string made of `n` spaces.
fn spaces(width: u32) -> String {
(0 .. width).into_iter().map(|_| ' ').collect()
}

View File

@ -77,3 +77,38 @@ stdout = { file = "outputs/links_oneline_icons.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'oneline', 'icons' ]
# icon spacing tests
[[cmd]]
name = "EXA_ICON_SPACING=0 exa -1 --icons puts no spaces between icons and file names"
shell = "EXA_ICON_SPACING=0 exa -1 --icons /testcases/links"
stdout = { file = "outputs/links_oneline_icons_width0.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'env', 'oneline', 'icons' ]
[[cmd]]
name = "EXA_ICON_SPACING=1 exa -1 --icons puts one space between icons and file names"
shell = "EXA_ICON_SPACING=1 exa -1 --icons /testcases/links"
stdout = { file = "outputs/links_oneline_icons.ansitxt" } # same as the default
stderr = { empty = true }
status = 0
tags = [ 'env', 'oneline', 'icons' ]
[[cmd]]
name = "EXA_ICON_SPACING=2 exa -1 --icons puts two spaces between icons and file names"
shell = "EXA_ICON_SPACING=2 exa -1 --icons /testcases/links"
stdout = { file = "outputs/links_oneline_icons_width2.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'env', 'oneline', 'icons' ]
[[cmd]]
name = "EXA_ICON_SPACING=3 exa -1 --icons puts three spaces between icons and file names"
shell = "EXA_ICON_SPACING=3 exa -1 --icons /testcases/links"
stdout = { file = "outputs/links_oneline_icons_width3.ansitxt" }
stderr = { empty = true }
status = 0
tags = [ 'env', 'oneline', 'icons' ]

View File

@ -1,7 +1,6 @@
 1_bytes  3_KiB  5_MiB  8_bytes  10_KiB  12_MiB
 1_KiB  3_MiB  6_bytes  8_KiB  10_MiB  13_bytes
 1_MiB  4_bytes  6_KiB  8_MiB  11_bytes  13_KiB
 2_bytes  4_KiB  6_MiB  9_bytes  11_KiB  13_MiB
 2_KiB  4_MiB  7_bytes  9_KiB  11_MiB
 2_MiB  5_bytes  7_KiB  9_MiB  12_bytes
 3_bytes  5_KiB  7_MiB  10_bytes  12_KiB
 1_bytes  3_bytes  5_bytes  7_bytes  9_bytes  11_bytes  13_bytes
 1_KiB  3_KiB  5_KiB  7_KiB  9_KiB  11_KiB  13_KiB
 1_MiB  3_MiB  5_MiB  7_MiB  9_MiB  11_MiB  13_MiB
 2_bytes  4_bytes  6_bytes  8_bytes  10_bytes  12_bytes
 2_KiB  4_KiB  6_KiB  8_KiB  10_KiB  12_KiB
 2_MiB  4_MiB  6_MiB  8_MiB  10_MiB  12_MiB

View File

@ -0,0 +1,10 @@
broken -> nowhere
current_dir -> .
forbidden -> /proc/1/root
itself -> itself
parent_dir -> ..
root -> /
some_file
some_file_absolute -> /testcases/links/some_file
some_file_relative -> some_file
usr -> /usr

View File

@ -0,0 +1,10 @@
 broken -> nowhere
 current_dir -> .
 forbidden -> /proc/1/root
 itself -> itself
 parent_dir -> ..
 root -> /
 some_file
 some_file_absolute -> /testcases/links/some_file
 some_file_relative -> some_file
 usr -> /usr

View File

@ -0,0 +1,10 @@
 broken -> nowhere
 current_dir -> .
 forbidden -> /proc/1/root
 itself -> itself
 parent_dir -> ..
 root -> /
 some_file
 some_file_absolute -> /testcases/links/some_file
 some_file_relative -> some_file
 usr -> /usr