Skip to content

Commit

Permalink
feat: add hidden flag to traverse hidden files
Browse files Browse the repository at this point in the history
Add the `hidden` and `no-hidden` flag that allows
configuring the traversal of hidden files.

Examples:

```
treefmt --hidden
```
will traverse hidden files.

```
treefmt --hidden --no-hidden
```
will not traverse hidden files, same as the default (`treefmt`).

```
treefmt --hidden --no-hidden --hidden
```
will traverse hidden files.
  • Loading branch information
a-kenji committed Sep 24, 2023
1 parent 654ecef commit 181ec88
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
too-many-arguments-threshold = 9
too-many-arguments-threshold = 10
8 changes: 8 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ treefmt [FLAGS] [OPTIONS] [--] [paths]...

> Only apply selected formatters. Defaults to all formatters.
`-H, --hidden`

> Also traverse hidden files (files that start with a .). This behaviour can be overridden with the `no-hidden` flag.
`--no-hidden`

> Override the `--hidden` flag. Don't traverse hidden files.
`--tree-root <tree-root>`

> Set the path to the tree root directory where treefmt will look for the files to format. Defaults to the folder holding the `treefmt.toml` file. It’s mostly useful in combination with `--config-file` to specify the project root which won’t coincide with the directory holding `treefmt.toml`.
Expand Down
2 changes: 2 additions & 0 deletions src/command/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn format_cmd(
work_dir: &Path,
config_file: &Path,
paths: &[PathBuf],
hidden: bool,
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn format_cmd(
&cache_dir,
config_file,
&paths,
hidden,
no_cache,
clear_cache,
fail_on_change,
Expand Down
14 changes: 12 additions & 2 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Create a new treefmt.toml
/// Create a new treefmt.toml.
#[arg(short, long, default_value_t = false)]
pub init: bool,

Expand All @@ -38,6 +38,15 @@ pub struct Cli {
#[arg(short, long, default_value_t = false)]
pub clear_cache: bool,

#[arg(long = "hidden", short = 'H')]
/// Include hidden files while traversing the tree.
/// Override with the --no-hidden flag.
pub hidden: bool,
/// Overrides the --hidden flag.
/// Don't include hidden files while traversing the tree.
#[arg(long, overrides_with = "hidden", hide = true)]
no_hidden: bool,

/// Exit with error if any changes were made. Useful for CI.
#[arg(
long,
Expand All @@ -51,7 +60,7 @@ pub struct Cli {
#[arg(long, default_value_t = false)]
pub allow_missing_formatter: bool,

/// Log verbosity is based off the number of v used
/// Log verbosity is based off the number of v used.
#[clap(flatten)]
pub verbose: Verbosity,

Expand Down Expand Up @@ -145,6 +154,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
&cli.work_dir,
config_file,
&cli.paths,
cli.hidden,
cli.no_cache,
cli.clear_cache,
cli.fail_on_change,
Expand Down
2 changes: 2 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn run_treefmt(
cache_dir: &Path,
treefmt_toml: &Path,
paths: &[PathBuf],
hidden: bool,
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
Expand Down Expand Up @@ -146,6 +147,7 @@ pub fn run_treefmt(
for path in paths[1..].iter() {
builder.add(path);
}
builder.hidden(!hidden);
// TODO: builder has a lot of interesting options.
// TODO: use build_parallel with a Visitor.
// See https://docs.rs/ignore/0.4.17/ignore/struct.WalkParallel.html#method.visit
Expand Down

0 comments on commit 181ec88

Please sign in to comment.