Skip to content

Commit

Permalink
feat: add auto option to hyperlink option to match both ls behaviour …
Browse files Browse the repository at this point in the history
…and color option behaviour
  • Loading branch information
mrnossiom committed Dec 27, 2023
1 parent c787891 commit 0650fba
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
1 change: 0 additions & 1 deletion rustfmt.toml

This file was deleted.

16 changes: 11 additions & 5 deletions src/options/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ impl QuoteStyle {
}

impl EmbedHyperlinks {
fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
let flagged = matches.has(&flags::HYPERLINK)?;
pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
let Some(word) = matches.get(&flags::HYPERLINK)? else {
return Ok(Self::Automatic)
};

if flagged {
Ok(Self::On)
if word == "always" {
Ok(Self::Always)
} else if word == "auto" || word == "automatic" {
Ok(Self::Automatic)
} else if word == "never" {
Ok(Self::Never)
} else {
Ok(Self::Off)
Err(OptionsError::BadArgument(&flags::HYPERLINK, word.into()))
}
}
}
2 changes: 1 addition & 1 deletion src/options/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub static TIME: Arg = Arg { short: Some(b't'), long: "time", take
pub static ACCESSED: Arg = Arg { short: Some(b'u'), long: "accessed", takes_value: TakesValue::Forbidden };
pub static CREATED: Arg = Arg { short: Some(b'U'), long: "created", takes_value: TakesValue::Forbidden };
pub static TIME_STYLE: Arg = Arg { short: None, long: "time-style", takes_value: TakesValue::Necessary(Some(TIME_STYLES)) };
pub static HYPERLINK: Arg = Arg { short: None, long: "hyperlink", takes_value: TakesValue::Forbidden };
pub static HYPERLINK: Arg = Arg { short: None, long: "hyperlink", takes_value: TakesValue::Optional(Some(WHEN), "auto") };
pub static MOUNTS: Arg = Arg { short: Some(b'M'), long: "mounts", takes_value: TakesValue::Forbidden };
pub static SMART_GROUP: Arg = Arg { short: None, long: "smart-group", takes_value: TakesValue::Forbidden };
const TIMES: Values = &["modified", "changed", "accessed", "created"];
Expand Down
3 changes: 3 additions & 0 deletions src/options/theme.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::options::parser::MatchedFlags;
use crate::options::{flags, vars, OptionsError, Vars};
use crate::output::color_scale::ColorScaleOptions;
use crate::output::file_name::EmbedHyperlinks;
use crate::theme::{Definitions, Options, UseColours};

impl Options {
pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
let use_colours = UseColours::deduce(matches, vars)?;
let colour_scale = ColorScaleOptions::deduce(matches, vars)?;
let embed_hyperlinks = EmbedHyperlinks::deduce(matches)?;

let definitions = if use_colours == UseColours::Never {
Definitions::default()
Expand All @@ -17,6 +19,7 @@ impl Options {
Ok(Self {
use_colours,
colour_scale,
embed_hyperlinks,
definitions,
})
}
Expand Down
22 changes: 18 additions & 4 deletions src/output/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl Options {
mounted_fs: file.mount_point_info(),
}
}

pub fn do_embed_hyperlinks(&self) -> bool {
match self.embed_hyperlinks {
EmbedHyperlinks::Always => true,
EmbedHyperlinks::Never => false,
EmbedHyperlinks::Automatic => self.is_a_tty,
}
}
}

/// When displaying a file name, there needs to be some way to handle broken
Expand Down Expand Up @@ -114,8 +122,14 @@ pub enum ShowIcons {
/// Whether to embed hyperlinks.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum EmbedHyperlinks {
Off,
On,
/// Embed links even when output isn’t going to a terminal.
Always,

/// Embed links when output is going to a terminal, but not otherwise.
Automatic,

/// Never embed links, even when output is going to a terminal.
Never,
}

/// Whether or not to wrap file names with spaces in quotes.
Expand Down Expand Up @@ -230,7 +244,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
classify: Classify::JustFilenames,
quote_style: QuoteStyle::QuoteSpaces,
show_icons: ShowIcons::Never,
embed_hyperlinks: EmbedHyperlinks::Off,
embed_hyperlinks: EmbedHyperlinks::Never,
is_a_tty: self.options.is_a_tty,
};

Expand Down Expand Up @@ -373,7 +387,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
let mut bits = Vec::new();

let mut display_hyperlink = false;
if self.options.embed_hyperlinks == EmbedHyperlinks::On {
if self.options.do_embed_hyperlinks() {
if let Some(abs_path) = self
.file
.absolute_path()
Expand Down
18 changes: 6 additions & 12 deletions src/output/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use term_grid as tg;

use crate::fs::filter::FileFilter;
use crate::fs::File;
use crate::output::file_name::ShowIcons;
use crate::output::file_name::{Classify, Options as FileStyle};
use crate::output::file_name::{EmbedHyperlinks, ShowIcons};
use crate::theme::Theme;

use super::file_name::QuoteStyle;
Expand Down Expand Up @@ -64,33 +64,27 @@ impl<'a> Render<'a> {
};
let contents = filename.paint();
let width = match (
filename.options.embed_hyperlinks,
filename.options.do_embed_hyperlinks(),
filename.options.show_icons,
) {
(
EmbedHyperlinks::On,
ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing),
) => {
(true, ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing)) => {
filename.bare_utf8_width()
+ classification_width
+ 1
+ (spacing as usize)
+ space_filename_offset
}
(EmbedHyperlinks::On, ShowIcons::Never) => {
(true, ShowIcons::Never) => {
filename.bare_utf8_width() + classification_width + space_filename_offset
}
(
EmbedHyperlinks::Off,
ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing),
) => {
(false, ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing)) => {
filename.bare_utf8_width()
+ classification_width
+ 1
+ (spacing as usize)
+ space_filename_offset
}
(EmbedHyperlinks::Off, _) => *contents.width(),
(false, _) => *contents.width(),
};

grid.add(tg::Cell {
Expand Down
14 changes: 6 additions & 8 deletions src/output/grid_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::output::details::{
Options as DetailsOptions, Render as DetailsRender, Row as DetailsRow,
};
use crate::output::file_name::Options as FileStyle;
use crate::output::file_name::{EmbedHyperlinks, ShowIcons};
use crate::output::file_name::ShowIcons;
use crate::output::grid::Options as GridOptions;
use crate::output::table::{Options as TableOptions, Row as TableRow, Table};
use crate::output::tree::{TreeDepth, TreeParams};
Expand Down Expand Up @@ -186,19 +186,17 @@ impl<'a> Render<'a> {
QuoteStyle::QuoteSpaces => 0, // Default case
};
let width = match (
filename.options.embed_hyperlinks,
filename.options.do_embed_hyperlinks(),
filename.options.show_icons,
) {
(EmbedHyperlinks::On, ShowIcons::Automatic(spacing)) => {
(true, ShowIcons::Automatic(spacing)) => {
filename.bare_utf8_width() + 1 + (spacing as usize) + space_filename_offset
}
(EmbedHyperlinks::On, ShowIcons::Always(spacing)) => {
(true, ShowIcons::Always(spacing)) => {
filename.bare_utf8_width() + 1 + (spacing as usize) + space_filename_offset
}
(EmbedHyperlinks::On, ShowIcons::Never) => {
filename.bare_utf8_width() + space_filename_offset
}
(EmbedHyperlinks::Off, _) => *contents.width(),
(true, ShowIcons::Never) => filename.bare_utf8_width() + space_filename_offset,
(false, _) => *contents.width(),
};

TextCell {
Expand Down
4 changes: 3 additions & 1 deletion src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ansiterm::Style;
use crate::fs::File;
use crate::info::filetype::FileType;
use crate::output::color_scale::ColorScaleOptions;
use crate::output::file_name::Colours as FileNameColours;
use crate::output::file_name::{Colours as FileNameColours, EmbedHyperlinks};
use crate::output::render;

mod ui_styles;
Expand All @@ -20,6 +20,8 @@ pub struct Options {

pub colour_scale: ColorScaleOptions,

pub embed_hyperlinks: EmbedHyperlinks,

pub definitions: Definitions,
}

Expand Down

0 comments on commit 0650fba

Please sign in to comment.