Skip to content

Commit

Permalink
go back to clap
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Aug 25, 2021
1 parent ed6f566 commit 561543d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 60 deletions.
110 changes: 64 additions & 46 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
bump_alloc = "0.1.0"
clap = "=3.0.0-beta.2"
color-eyre = "0.5.7"
rayon = "1.5.1"
strsim = "0.10.0"
structopt = "0.3.22"
28 changes: 15 additions & 13 deletions src/main.rs
@@ -1,21 +1,12 @@
use color_eyre::eyre::{Result, WrapErr};
use clap::{crate_authors, crate_version, App, Arg};
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
use rayon::prelude::*;
use std::io::{self, stdin, stdout, BufRead, BufWriter, Write};
use strsim::levenshtein;
use structopt::StructOpt;

#[global_allocator]
static A: bump_alloc::BumpAlloc = bump_alloc::BumpAlloc::new();

// works like `sort`, but sorts according to Levenshtein distance instead of
// alphanumerically.
#[derive(StructOpt)]
#[structopt(name = "similar-sort")]
struct Opts {
/// sort according to distance from this string
target: String,
}

fn main() {
if let Err(err) = try_main() {
eprintln!("{:?}", err);
Expand All @@ -26,7 +17,18 @@ fn main() {
fn try_main() -> Result<()> {
color_eyre::install()?;

let opts = Opts::from_args();
let matches = App::new("similar-sort")
.version(crate_version!())
.author(crate_authors!())
.about(
"works like `sort`, but sorts according to edit distance instead of alphanumerically.",
)
.arg(Arg::new("target").about("sort according to distance from this string"))
.get_matches();

let target = matches
.value_of("target")
.context("could not retrieve target from args. Internal error; please report!")?;

let lines: Vec<String> = stdin()
.lock()
Expand All @@ -36,7 +38,7 @@ fn try_main() -> Result<()> {

let mut distances: Vec<(usize, &String)> = lines
.iter()
.map(|candidate| (levenshtein(&opts.target, candidate), candidate))
.map(|candidate| (levenshtein(target, candidate), candidate))
.collect();

distances.par_sort_unstable_by_key(|x| x.0);
Expand Down

0 comments on commit 561543d

Please sign in to comment.