Skip to content

Commit

Permalink
added -f option to README and help text (#8)
Browse files Browse the repository at this point in the history
* added option -f to display the full path just like tree -f

* added -f option to README and help text

* fixed broken help CI test

* Set up GitHub Actions (#17)

* Set up GitHub Actions

* Update rust.yml

* added test fixture + fixed problem when start path is not ./

* fixed prefix problem with nested singleton directories

Co-authored-by: Christoph MΓΌller <[email protected]>
Co-authored-by: Jake Zimmerman <[email protected]>
  • Loading branch information
3 people authored Mar 9, 2021
1 parent 11aab7f commit 0036c20
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Arguments:
Options:
--color (always|auto|never)
Whether to colorize the output [default: auto]
-f Prints the full path prefix for each file.
-h, --help Print this help message
Example:
Expand Down
48 changes: 40 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,56 @@ impl PathTrie {
join_with_parent: bool,
lscolors: &LsColors,
parent_path: PathBuf,
full_path: bool,
) {
let normal_prefix = format!("{}β”‚ ", prefix);
let last_prefix = format!("{} ", prefix);

for (idx, (path, it)) in self.trie.iter().enumerate() {
let current_path = parent_path.join(path);
let style = ansi_style_for_path(&lscolors, &current_path);
let painted = style.paint(path.to_string_lossy());

let contains_singleton_dir = it.contains_singleton_dir();

let painted = match full_path {
false => style.paint(path.to_string_lossy()),
true => match contains_singleton_dir && !join_with_parent {
false => style.paint(current_path.to_string_lossy()),
true => style.paint(""),
},
};

// If this folder only contains a single dir, we skip printing it because it will be
// picked up and printed on the next iteration. If this is a full path (even if it
// contains more than one directory), we also want to skip printing, because the full
// path will be printed all at once (see painted above), not part by part.
// If this is a full path however the prefix must be printed at the very beginning.
let should_print = (contains_singleton_dir && !join_with_parent)
|| !contains_singleton_dir
|| !full_path;

let newline = if contains_singleton_dir { "" } else { "\n" };
let is_last = idx == self.trie.len() - 1;

let next_prefix = if join_with_parent {
let joiner = if top || parent_path == PathBuf::from("/") {
let joiner = if full_path || top || parent_path == PathBuf::from("/") {
""
} else {
"/"
};
print!("{}{}{}", style.paint(joiner), painted, newline);
if should_print {
print!("{}{}{}", style.paint(joiner), painted, newline);
}
prefix
} else if !is_last {
print!("{}β”œβ”€β”€ {}{}", prefix, painted, newline);
if should_print {
print!("{}β”œβ”€β”€ {}{}", prefix, painted, newline);
}
&normal_prefix
} else {
print!("{}└── {}{}", prefix, painted, newline);
if should_print {
print!("{}└── {}{}", prefix, painted, newline);
}
&last_prefix
};

Expand All @@ -81,11 +105,12 @@ impl PathTrie {
contains_singleton_dir,
lscolors,
current_path,
full_path,
)
}
}

fn print(&self, lscolors: &LsColors) {
fn print(&self, lscolors: &LsColors, full_path: bool) {
if self.trie.is_empty() {
println!();
return;
Expand All @@ -100,7 +125,14 @@ impl PathTrie {
println!("{}", style.paint(current_path.to_string_lossy()));
}

self._print(true, "", contains_singleton_dir, &lscolors, current_path)
self._print(
true,
"",
contains_singleton_dir,
&lscolors,
current_path,
full_path,
)
}
}

Expand Down Expand Up @@ -143,7 +175,7 @@ fn main() -> io::Result<()> {
options::Colorize::Never => LsColors::empty(),
};

trie.print(&lscolors);
trie.print(&lscolors, options.full_path);

io::Result::Ok(())
}
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Default for Colorize {
pub struct Options {
pub filename: Option<String>,
pub colorize: Colorize,
pub full_path: bool,
}

const USAGE: &str = "\
Expand All @@ -48,6 +49,7 @@ Arguments:
Options:
--color (always|auto|never)
Whether to colorize the output [default: auto]
-f Prints the full path prefix for each file.
-h, --help Print this help message
-v, --version Print the version and exit
Expand Down Expand Up @@ -84,6 +86,11 @@ pub fn parse_options_or_die() -> Options {
exit(0);
}

if arg == "-f" {
options.full_path = true;
continue;
}

if arg == "--color" {
if let Some(color) = argv.next() {
match color.parse() {
Expand Down
Empty file added test/cli/full_path/dir1/bar.txt
Empty file.
Empty file added test/cli/full_path/dir1/foo.txt
Empty file.
Empty file added test/cli/full_path/dir2/qux.txt
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions test/cli/full_path/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

echo ----- -f -----
find test/cli/full_path | src/as_tree -f
12 changes: 12 additions & 0 deletions test/cli/full_path/run.sh.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
----- -f -----
./test/cli/full_path
β”œβ”€β”€ ./test/cli/full_path/dir1
β”‚ β”œβ”€β”€ ./test/cli/full_path/dir1/bar.txt
β”‚ └── ./test/cli/full_path/dir1/foo.txt
β”œβ”€β”€ ./test/cli/full_path/dir2
β”‚ └── ./test/cli/full_path/dir2/qux.txt
β”œβ”€β”€ ./test/cli/full_path/dir3/dir4
β”‚ └── ./test/cli/full_path/dir3/dir4/zap.txt
β”œβ”€β”€ ./test/cli/full_path/run
β”œβ”€β”€ ./test/cli/full_path/run.sh
└── ./test/cli/full_path/run.sh.exp
2 changes: 2 additions & 0 deletions test/cli/help/run.sh.exp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Arguments:
Options:
--color (always|auto|never)
Whether to colorize the output [default: auto]
-f Prints the full path prefix for each file.
-h, --help Print this help message
-v, --version Print the version and exit

Expand All @@ -28,6 +29,7 @@ Arguments:
Options:
--color (always|auto|never)
Whether to colorize the output [default: auto]
-f Prints the full path prefix for each file.
-h, --help Print this help message
-v, --version Print the version and exit

Expand Down

0 comments on commit 0036c20

Please sign in to comment.