Skip to content

Commit

Permalink
Merge pull request #33
Browse files Browse the repository at this point in the history
  • Loading branch information
loichyan authored Nov 22, 2024
2 parents 5682812 + 4724c45 commit baedef9
Show file tree
Hide file tree
Showing 22 changed files with 417 additions and 39 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ noticeable to end-users since the last release. For developers, this project fol

- Add a subcommand, `nerdfix completions <SHELL>`, to generate completions for your shell
([#30](https://github.com/loichyan/nerdfix/pull/30)).
- Support comments in JSON files ([#33](https://github.com/loichyan/nerdfix/pull/33)).
- Add a new `nerdfix query` subcommand, useful for querying icon infos from the database
([#33](https://github.com/loichyan/nerdfix/pull/33)).
- Add a new `codepoint:from/to` substitution type
([#33](https://github.com/loichyan/nerdfix/pull/33)).
- Support checking dropped icons of Nerd Fonts v3.3.0 through the newly added
`nerdfix --nf-version=3.3.0` option ([#33](https://github.com/loichyan/nerdfix/pull/33), thanks
[@Finii](https://github.com/Finii) and [@hasecilu](https://github.com/hasecilu)).

## [0.4.1] - 2024-07-14

Expand Down
42 changes: 42 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
codespan-reporting = "0.11.1"
content_inspector = "0.2.4"
derive_more = { version = "1", features = ["full"] }
extend = "1.2"
indexmap = "2.2"
inquire = "0.7.5"
itertools = "0.13.0"
json-strip-comments = "1"
miette = { version = "7.2", features = ["fancy"] }
noodler = "0.1.0"
nu-ansi-term = "0.50.0"
Expand Down
50 changes: 33 additions & 17 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bytesize::ByteSize;
use camino::Utf8PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use derive_more::Display;
use shadow_rs::formatcp;
use thisctx::IntoError;

Expand All @@ -20,7 +21,8 @@ const V_PATH: &str = "PATH";
const V_SHELL: &str = "SHELL";
const V_SIZE: &str = "SIZE";
const V_SOURCE: &str = "SOURCE";
const V_SUBSTITUTION: &str = "SUBSTITUTION";
const V_SUB: &str = "SUB";
const V_VERSION: &str = "VERSION";
const DEFAULT_SIZE: &str = "16MB";
const INDEX_REV: &str = include_str!("index-rev");
const CLAP_LONG_VERSION: &str = formatcp!("{}\ncheat-sheet: {}", shadow::PKG_VERSION, INDEX_REV);
Expand All @@ -45,8 +47,11 @@ pub struct Cli {
/// them at first.
#[arg(short, long, global = true, value_name = V_PATH)]
pub input: Vec<IoPath>,
/// The version of Nerd Fonts you intend to migrate to.
#[arg(long, global = true, value_name = V_VERSION, default_value_t = NfVersion::default())]
pub nf_version: NfVersion,
/// Perform an exact/prefix substitution.
#[arg(long, global = true, value_name = V_SUBSTITUTION, long_help = SUB_LONG_HELP)]
#[arg(long, global = true, value_name = V_SUB, long_help = SUB_LONG_HELP)]
pub sub: Vec<Substitution>,
/// Decrease log level.
#[arg(
Expand All @@ -69,10 +74,10 @@ pub struct Cli {
#[command(subcommand)]
pub cmd: Command,
/// [deprecated] Use `--input` instead.
#[arg(long, global = true, value_name = V_PATH)]
#[arg(long, global = true, value_name = V_SUB)]
pub substitution: Vec<IoPath>,
/// [deprecated] Use `--sub prefix:` instead.
#[arg(long, global = true, value_name = V_SUBSTITUTION)]
#[arg(long, global = true, value_name = V_SUB)]
pub replace: Vec<Substitution>,
}

Expand All @@ -93,7 +98,7 @@ pub enum Command {
/// Check for obsolete icons.
Check {
/// Output format of diagnostics.
#[arg(long, value_name = V_FORMAT, default_value_t = OutputFormat::Console)]
#[arg(long, value_name = V_FORMAT, default_value_t = OutputFormat::default())]
format: OutputFormat,
/// Recursively traverse all directories.
#[arg(short, long)]
Expand Down Expand Up @@ -142,13 +147,31 @@ pub enum Command {
},
/// Fuzzy search for an icon.
Search {},
/// Query icon infos from the database, returned in JSON.
Query {
#[arg(long, value_parser = crate::icon::parse_codepoint)]
codepoint: Option<char>,
#[arg(long, conflicts_with = "codepoint")]
name: Option<String>,
},
/// Generate shell completions for your shell to stdout.
Completions {
#[arg(value_name = V_SHELL)]
shell: Shell,
},
}

#[derive(Clone, Copy, Debug, Display, Default, Eq, PartialEq, ValueEnum)]
pub enum NfVersion {
#[default]
#[value(name = "3.0.0")]
#[display("3.0.0")]
V3_0_0,
#[value(name = "3.3.0")]
#[display("3.3.0")]
V3_3_0,
}

#[derive(Clone, Debug)]
pub enum UserInput {
Candidate(usize),
Expand Down Expand Up @@ -242,24 +265,17 @@ impl IoPath {
}
}

#[derive(Clone, Debug, Default, ValueEnum)]
#[derive(Clone, Copy, Debug, Display, Default, Eq, PartialEq, ValueEnum)]
pub enum OutputFormat {
#[value(help("Json output format"))]
#[value(help = "Json output format")]
#[display("json")]
Json,
#[default]
#[value(help("Human-readable output format"))]
#[value(help = "Human-readable output format")]
#[display("console")]
Console,
}

impl fmt::Display for OutputFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Json => f.write_str("json"),
Self::Console => f.write_str("console"),
}
}
}

#[derive(Clone, Debug)]
pub struct Outpath(pub Option<IoPath>);

Expand Down
3 changes: 3 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum SubstitutionType {
#[default]
Exact,
Prefix,
Codepoint,
}

mod codepoint {
Expand Down Expand Up @@ -96,6 +97,7 @@ mod substitution {
Ok(match s {
"exact" => SubstitutionType::Exact,
"prefix" => SubstitutionType::Prefix,
"codepoint" => SubstitutionType::Codepoint,
_ => return Err("unknown substitution type"),
})
}
Expand Down Expand Up @@ -145,6 +147,7 @@ mod substitution {
f.write_str(match self {
Self::Exact => "exact",
Self::Prefix => "prefix",
Self::Codepoint => "codepoint",
})
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ use tracing::{error, info, warn, Level};
use tracing_subscriber::prelude::*;
use walkdir::WalkDir;

use self::cli::{Command, IoPath, Source};
use self::cli::{Command, IoPath, NfVersion, Source};
use self::prompt::YesOrNo;
use self::runtime::{CheckerContext, Runtime};
use self::utils::{LogStatus, ResultExt as _};

static ICONS: &str = include_str!("./icons.json");
static SUBSTITUTIONS: &str = include_str!("./substitutions.json");
static PATCH_NF_3_3_0: &str = include_str!("./patch-nf-3_3_0.json");

fn walk<'a>(
paths: impl 'a + IntoIterator<Item = (IoPath, Option<IoPath>)>,
Expand Down Expand Up @@ -97,11 +98,14 @@ fn main_impl() -> error::Result<()> {
if args.input.is_empty() {
rt.load_db(ICONS).unwrap();
rt.load_db(SUBSTITUTIONS).unwrap();
if args.nf_version == NfVersion::V3_3_0 {
rt.load_db(PATCH_NF_3_3_0).unwrap();
}
}
for input in args.input.iter() {
rt.load_db_from(input)?;
}
rt.with_substitutions(args.sub);
rt.with_substitutions(args.sub)?;

match args.cmd {
Command::Cache { .. } => warn!("`cache` is deprecated, use `dump` instead"),
Expand Down Expand Up @@ -195,6 +199,19 @@ fn main_impl() -> error::Result<()> {
Command::Search {} => {
rt.build().prompt_input_icon(None).ok();
}
Command::Query { codepoint, name } => {
let rt = rt.build();
let icon = if let Some(c) = codepoint {
rt.get_icon_by_codepoint(c)
} else if let Some(name) = name {
rt.get_icon_by_name(&name)
} else {
None
};
if let Some(icon) = icon {
println!("{}", serde_json::to_string(icon)?);
}
}
Command::Completions { shell } => {
clap_complete::generate(
shell,
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::icon::{Database, Icon};
pub fn parse(s: &str) -> error::Result<Database> {
let s = s.trim_start();
Ok(if s.starts_with('{') {
serde_json::from_str::<Database>(s)?
crate::utils::parse_jsonc::<Database>(s)?
} else {
Database {
icons: parse_cheat_sheet(s)?,
Expand Down
Loading

0 comments on commit baedef9

Please sign in to comment.