Skip to content

Commit

Permalink
Merge pull request #114 from jhscheer/add_fr_51
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis authored Apr 7, 2023
2 parents 12a3818 + ebd67e8 commit 285e7b3
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 118 deletions.
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ clap_complete = "4.1.1"
filesize = "0.2.0"
ignore = "0.4.2"
indextree = "4.6.0"
is-terminal = "0.4.6"
lscolors = { version = "0.13.0", features = ["ansi_term"] }
once_cell = "1.17.0"
thiserror = "1.0.40"
Expand Down
23 changes: 21 additions & 2 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use clap::{
parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Id, Parser,
};
use ignore::overrides::{Override, OverrideBuilder};
use is_terminal::IsTerminal;
use std::{
convert::From,
ffi::{OsStr, OsString},
io::{stdin, BufRead},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -39,7 +41,7 @@ pub struct Context {

/// Include or exclude files using glob patterns
#[arg(short, long)]
glob: Vec<String>,
pub glob: Vec<String>,

/// Include or exclude files using glob patterns; case insensitive
#[arg(long)]
Expand Down Expand Up @@ -134,7 +136,24 @@ impl Context {
/// Initializes [Context], optionally reading in the configuration file to override defaults.
/// Arguments provided will take precedence over config.
pub fn init() -> Result<Self, Error> {
let user_args = Self::command().args_override_self(true).get_matches();
let mut args: Vec<_> = std::env::args().collect();

// If there's input on stdin we add each line as a separate glob pattern
if !stdin().is_terminal() {
stdin()
.lock()
.lines()
.filter_map(|s| s.ok())
.filter(|l| !l.is_empty())
.for_each(|line| {
args.push("--glob".into());
args.push(line);
});
}

let user_args = Self::command()
.args_override_self(true)
.get_matches_from(args);

let no_config = user_args.get_one("no_config").map_or(false, bool::clone);

Expand Down
11 changes: 10 additions & 1 deletion src/render/tree/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ impl Node {
|size| size_loc.format(size),
);

let size_padding = if size.is_empty() {
String::new()
} else {
String::from(" ")
};

let icon = if self.show_icon {
self.get_icon().unwrap()
} else {
Expand All @@ -228,7 +234,10 @@ impl Node {

match size_loc {
SizeLocation::Right => {
write!(f, "{prefix}{icon:<icon_padding$}{styled_name} {size}")
write!(
f,
"{prefix}{icon:<icon_padding$}{styled_name}{size_padding}{size}"
)
}
SizeLocation::Left => {
write!(f, "{size} {prefix}{icon:<icon_padding$}{styled_name}")
Expand Down
8 changes: 1 addition & 7 deletions tests/file_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ fn file_count() {
#[test]
fn file_count_report() {
assert_eq!(
utils::run_cmd(&[
"--count",
"--report",
"--sort",
"name",
"tests/data"
]),
utils::run_cmd(&["--count", "--report", "--sort", "name", "tests/data"]),
indoc!(
"
d 1241 B data
Expand Down
72 changes: 49 additions & 23 deletions tests/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ mod utils;
#[test]
fn glob() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--glob",
"*.txt",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "--glob", "*.txt", "tests/data"]),
indoc!(
"
data (1.07 KiB)
Expand All @@ -30,18 +24,12 @@ fn glob() {
#[test]
fn glob_negative() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--glob",
"!*.txt",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "--glob", "!*.txt", "tests/data"]),
indoc!(
"
data (143 B)
├─ dream_cycle
├─ lipsum
├─ dream_cycle
├─ lipsum
└─ the_yellow_king (143 B)
└─ cassildas_song.md (143 B)"
)
Expand Down Expand Up @@ -77,13 +65,7 @@ fn glob_case_insensitive() {
#[test]
fn iglob() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--iglob",
"*.TXT",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "--iglob", "*.TXT", "tests/data"]),
indoc!(
"
data (1.07 KiB)
Expand All @@ -98,3 +80,47 @@ fn iglob() {
)
)
}

#[test]
fn glob_stdin() {
use std::io::Write;
use std::process::{Command, Stdio};
use strip_ansi_escapes::strip as strip_ansi_escapes;
let expected = indoc!(
"
data (304 B)
├─ dream_cycle
├─ lipsum
├─ nemesis.txt (161 B)
└─ the_yellow_king (143 B)
└─ cassildas_song.md (143 B)
"
);
let stdin = String::from("cassildas_song.md\nnemesis.txt\n");

let cmd = Command::new("cargo")
.args([
"run",
"--",
"--threads",
"1",
"--no-config",
"--sort",
"name",
"tests/data",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
write!(cmd.stdin.as_ref().unwrap(), "{}", stdin).unwrap();
let output = cmd.wait_with_output().unwrap();

assert_eq!(
String::from_utf8(strip_ansi_escapes(output.stdout).unwrap()).unwrap(),
expected
);
assert!(output.status.success());
}
8 changes: 1 addition & 7 deletions tests/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ mod utils;
#[test]
fn level() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--level",
"1",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "--level", "1", "tests/data"]),
indoc!(
"
data (1.21 KiB)
Expand Down
9 changes: 1 addition & 8 deletions tests/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ mod utils;
#[test]
fn prune() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--glob",
"*.txt",
"--prune",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "--glob", "*.txt", "--prune", "tests/data"]),
indoc!(
"
data (1.07 KiB)
Expand Down
17 changes: 2 additions & 15 deletions tests/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ fn report() {
#[test]
fn report_human() {
assert_eq!(
utils::run_cmd(&[
"--report",
"--human",
"--sort",
"name",
"tests/data"
]),
utils::run_cmd(&["--report", "--human", "--sort", "name", "tests/data"]),
indoc!(
"
d 1.21 KiB data
Expand All @@ -51,14 +45,7 @@ fn report_human() {
#[test]
fn report_with_level() {
assert_eq!(
utils::run_cmd(&[
"--report",
"--level",
"1",
"--sort",
"name",
"tests/data"
]),
utils::run_cmd(&["--report", "--level", "1", "--sort", "name", "tests/data"]),
indoc!(
"
d 1241 B data
Expand Down
18 changes: 2 additions & 16 deletions tests/size_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ fn size_left_bin() {
#[test]
fn size_left_si() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"-p",
"si",
"--size-left",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "-p", "si", "--size-left", "tests/data"]),
"1.24 KB data
308 B ├─ dream_cycle
308 B │ └─ polaris.txt
Expand All @@ -44,14 +37,7 @@ fn size_left_si() {
#[test]
fn size_left_altered_precision() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"-n",
"3",
"--size-left",
"tests/data"
]),
utils::run_cmd(&["--sort", "name", "-n", "3", "--size-left", "tests/data"]),
"1.212 KiB data
308 B ├─ dream_cycle
308 B │ └─ polaris.txt
Expand Down
Loading

0 comments on commit 285e7b3

Please sign in to comment.