Skip to content

Commit

Permalink
Add support for --literal flag and literal entry in config (#900)
Browse files Browse the repository at this point in the history
Add support for `--literal` in order to opt out showing filenames with
quotes
Closes: #894
  • Loading branch information
PanGan21 authored Sep 19, 2023
1 parent 93b3fb0 commit bfbb217
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [v1.0.0] - 2023-08-25

### Added
- Add support for `--literal` from [PanGan21](https://github.com/PanGan21)
- Add CI to build aarch64 macOS target and skip on test [#878](https://github.com/lsd-rs/lsd/pull/878) from [zwpaper](https://github.com/zwpaper)
- Add complete color theming support for Git [k4yt3x](https://github.com/k4yt3x)
- Add [Git integration](https://github.com/Peltoche/lsd/issues/7) from [hpwxf](https://github.com/hpwxf)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ symlink-arrow: ⇒
# Possible values: false, true
header: false

# == Literal ==
# Whether to show quotes on filenames.
# Possible values: false, true
literal: false

# == Truncate owner ==
# How to truncate the username and group names for a file if they exceed a certain
# number of characters.
Expand Down
3 changes: 3 additions & 0 deletions doc/lsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
`--header`
: Display block headers

`-N --literal`
: Print entry names without quoting

`--truncate-owner-after`
: Truncate the user and group names if they exceed a certain number of characters

Expand Down
4 changes: 4 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ pub struct Cli {
#[arg(long, hide = !cfg!(windows))]
pub system_protected: bool,

/// Print entry names without quoting
#[arg(short = 'N', long)]
pub literal: bool,

/// Print help information
#[arg(long, action = ArgAction::Help)]
help: (),
Expand Down
8 changes: 8 additions & 0 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Config {
pub symlink_arrow: Option<String>,
pub hyperlink: Option<HyperlinkOption>,
pub header: Option<bool>,
pub literal: Option<bool>,
pub truncate_owner: Option<TruncateOwner>,
}

Expand Down Expand Up @@ -104,6 +105,7 @@ impl Config {
symlink_arrow: None,
hyperlink: None,
header: None,
literal: None,
truncate_owner: None,
}
}
Expand Down Expand Up @@ -332,6 +334,11 @@ hyperlink: never
# Specifies how the symlink arrow display, chars in both ascii and utf8
symlink-arrow: ⇒
# == Literal ==
# Whether to print entry names without quoting
# Possible values: false, true
literal: false
# == Truncate owner ==
# How to truncate the username and group name for the file if they exceed a
# certain number of characters.
Expand Down Expand Up @@ -406,6 +413,7 @@ mod tests {
symlink_arrow: Some("⇒".into()),
hyperlink: Some(HyperlinkOption::Never),
header: None,
literal: Some(false),
truncate_owner: Some(config_file::TruncateOwner {
after: None,
marker: Some("".to_string()),
Expand Down
6 changes: 4 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::color::Colors;
use crate::display;
use crate::flags::{ColorOption, Display, Flags, HyperlinkOption, Layout, SortOrder, ThemeOption};
use crate::flags::{
ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, SortOrder, ThemeOption,
};
use crate::git::GitCache;
use crate::icon::Icons;

Expand Down Expand Up @@ -71,7 +73,7 @@ impl Core {
// or require a raw output (like the `wc` command).
inner_flags.layout = Layout::OneLine;

flags.should_quote = false;
flags.literal = Literal(true);
};

let sorters = sort::assemble_sorters(&flags);
Expand Down
10 changes: 5 additions & 5 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ fn get_output(
icons,
display_option,
flags.hyperlink,
flags.should_quote,
flags.literal.0,
),
meta.indicator.render(flags),
]);
Expand Down Expand Up @@ -523,7 +523,7 @@ mod tests {
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
false,
)
.to_string();

Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {
&Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
false,
)
.to_string();

Expand Down Expand Up @@ -592,7 +592,7 @@ mod tests {
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
false,
)
.to_string();

Expand Down Expand Up @@ -633,7 +633,7 @@ mod tests {
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
false,
)
.to_string();

Expand Down
6 changes: 4 additions & 2 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod icons;
pub mod ignore_globs;
pub mod indicators;
pub mod layout;
pub mod literal;
pub mod permission;
pub mod recursion;
pub mod size;
Expand All @@ -33,6 +34,7 @@ pub use icons::Icons;
pub use ignore_globs::IgnoreGlobs;
pub use indicators::Indicators;
pub use layout::Layout;
pub use literal::Literal;
pub use permission::PermissionFlag;
pub use recursion::Recursion;
pub use size::SizeFlag;
Expand Down Expand Up @@ -74,8 +76,8 @@ pub struct Flags {
pub symlink_arrow: SymlinkArrow,
pub hyperlink: HyperlinkOption,
pub header: Header,
pub literal: Literal,
pub truncate_owner: TruncateOwner,
pub should_quote: bool,
}

impl Flags {
Expand Down Expand Up @@ -105,8 +107,8 @@ impl Flags {
symlink_arrow: SymlinkArrow::configure_from(cli, config),
hyperlink: HyperlinkOption::configure_from(cli, config),
header: Header::configure_from(cli, config),
literal: Literal::configure_from(cli, config),
truncate_owner: TruncateOwner::configure_from(cli, config),
should_quote: true,
})
}
}
Expand Down
85 changes: 85 additions & 0 deletions src/flags/literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! This module defines the [Literal]. To set it up from [Cli], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method.

use super::Configurable;

use crate::app::Cli;
use crate::config_file::Config;

/// The flag to set in order to show literal file names without quotes.
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)]
pub struct Literal(pub bool);

impl Configurable<Self> for Literal {
/// Get a potential `Literal` value from [Cli].
///
/// If the "literal" argument is passed, this returns a `Literal` with value `true` in a
/// [Some]. Otherwise this returns `Literal` with value `false` in a [Some].
fn from_cli(cli: &Cli) -> Option<Self> {
if cli.literal {
Some(Self(true))
} else {
Some(Self(false))
}
}

/// Get a potential `Literal` value from a [Config].
///
/// If the `Config::indicators` has value,
/// this returns its value as the value of the `Literal`, in a [Some].
/// Otherwise this returns `Literal` with value `false` in a [Some].
fn from_config(config: &Config) -> Option<Self> {
if let Some(value) = config.literal {
Some(Self(value))
} else {
Some(Self(false))
}
}
}

#[cfg(test)]
mod test {
use clap::Parser;

use super::Literal;

use crate::app::Cli;
use crate::config_file::Config;
use crate::flags::Configurable;

#[test]
fn test_from_cli_none() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(Literal(false)), Literal::from_cli(&cli));
}

#[test]
fn test_from_cli_literal() {
let argv = ["lsd", "--literal"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(Literal(true)), Literal::from_cli(&cli));
}

#[test]
fn test_from_config_none() {
assert_eq!(
Some(Literal(false)),
Literal::from_config(&Config::with_none())
);
}

#[test]
fn test_from_config_true() {
let mut c = Config::with_none();
c.literal = Some(true);
assert_eq!(Some(Literal(true)), Literal::from_config(&c));
}

#[test]
fn test_from_config_false() {
let mut c = Config::with_none();
c.literal = Some(false);
assert_eq!(Some(Literal(false)), Literal::from_config(&c));
}
}
Loading

0 comments on commit bfbb217

Please sign in to comment.