Skip to content

Commit

Permalink
Merge pull request #2 from loichyan/feat-output-format
Browse files Browse the repository at this point in the history
  • Loading branch information
loichyan authored Mar 29, 2023
2 parents 89440ce + 6c6bdf6 commit d331efe
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 37 deletions.
74 changes: 62 additions & 12 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ itertools = "0.10"
ngrammatic = "0.4"
once_cell = "1.17"
select = "0.6"
serde_json = "1.0"
thisctx = "0.4"
thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"

[dependencies.serde]
version = "1.0"
features = ["derive"]

[dependencies.clap]
version = "4.1"
features = ["derive"]
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ warning: Found obsolete icon U+F752
...
```

### Interactive patching

The output of `fix` command is similar to `check` and shows a prompt asking the
user to input a new icon to replace the obsolete one.

Expand Down Expand Up @@ -90,6 +92,8 @@ The prompt accepts several types of input:
| Icon name | `nf-md-file_document` |
| Icon character | `󰈙` |

### Fuzzy autocompletion/search

The prompt also provides fuzzy matched suggestions when typing the icon name:

```text
Expand All @@ -105,12 +109,26 @@ search.

### Autofix

`nerdfix` provides some features to autofix obsolete icons:
`nerdfix` provides some features to automatically patch obsolete icons:

- The last input is picked if an icon appears twice.
- Use `--replace FROM,TO` to replace the prefix of an icon name with another,
e.g. `nf-mdi-tab` is replaced by `nf-md-tab` when `--replace nf-mdi-,nf-md-`
is specified.
- Use `fix --replace FROM,TO` to replace the prefix of an icon name with
another, e.g. `nf-mdi-tab` is replaced by `nf-md-tab` when
`--replace nf-mdi-,nf-md-` is specified.

### Structured output

You can use `check --format json` to get structured output for further use.
`nerdfix` prints diagnostics with the following fields line by line:

| Field | Description |
| ----------- | ------------------------------------------------------- |
| `severity` | Severity of a diagnostic |
| `path` | Source file of a diagnostic |
| `type` | Diagnostic type, currently only `obsolete` is supported |
| `span` | Byte index span of an obsolete icon |
| `name` | Icon name |
| `codepoint` | Icon codepoint |

## ⚖️ License

Expand Down
21 changes: 17 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Command line arguments parser.
use crate::error;
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use std::{path::PathBuf, str::FromStr};
use thisctx::IntoError;

const V_PATH: &str = "PATH";
const V_REPLACE: &str = "CLASSES";
const V_CLASSES: &str = "CLASSES";
const V_FORMAT: &str = "FORMAT";

#[derive(Debug, Parser)]
pub struct Cli {
Expand All @@ -29,6 +30,9 @@ pub enum Command {
},
/// Check for obsolete icons.
Check {
/// Output format of diagnostics.
#[arg(long, value_name(V_FORMAT), default_value("console"))]
format: OutputFormat,
/// Path(s) of files to check.
#[arg(value_name(V_PATH))]
source: Vec<PathBuf>,
Expand All @@ -45,7 +49,7 @@ pub enum Command {
///
/// For example, use `--replace nf-mdi-,nf-md-` to replace all `nf-mdi*` icons
/// with the same icons in `nf-md*`.
#[arg(short, long, value_name(V_REPLACE))]
#[arg(long, value_name(V_CLASSES))]
replace: Vec<Replace>,
},
/// Fuzzy search for an icon.
Expand Down Expand Up @@ -81,7 +85,7 @@ impl FromStr for UserInput {
}
}

#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
pub struct Replace {
pub from: String,
pub to: String,
Expand All @@ -100,3 +104,12 @@ impl FromStr for Replace {
})
}
}

#[derive(Clone, Debug, Default, ValueEnum)]
pub enum OutputFormat {
#[value(help("Json output format"))]
Json,
#[default]
#[value(help("Human-readable output format"))]
Console,
}
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,35 @@ fn main_impl() -> error::Result<()> {
let rt = rt.build();
match args.cmd {
Command::Cache { output } => rt.save_cache(&output)?,
Command::Check { source } => {
let mut context = CheckerContext::default();
Command::Check { source, format } => {
let mut context = CheckerContext {
format,
..Default::default()
};
for path in source.iter() {
log_or_break!(rt.check(&mut context, path, false));
}
}
Command::Fix {
source,
mut yes,
yes,
replace,
} => {
let mut context = CheckerContext {
replace,
yes,
..Default::default()
};
for path in source.iter() {
log_or_break!((|| {
if let Some(patched) = rt.check(&mut context, path, true)? {
if !yes {
if !context.yes {
match prompt::prompt_yes_or_no(
"Are your sure to write the patched content?",
None,
)? {
YesOrNo::No => return Ok(()),
YesOrNo::AllYes => yes = true,
YesOrNo::AllYes => context.yes = true,
_ => {}
}
}
Expand Down
Loading

0 comments on commit d331efe

Please sign in to comment.