diff --git a/Cargo.lock b/Cargo.lock index 989cd63..db6587d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "block" version = "0.1.6" @@ -154,7 +160,7 @@ checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "ansi_term", "atty", - "bitflags", + "bitflags 1.3.2", "strsim 0.8.0", "term_size", "textwrap 0.11.0", @@ -169,7 +175,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d53da17d37dba964b9b3ecb5c5a1f193a2762c700e6829201e645b9381c99dc7" dependencies = [ "atty", - "bitflags", + "bitflags 1.3.2", "clap_derive", "clap_lex", "indexmap", @@ -253,7 +259,7 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1000,7 +1006,7 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cc", "cfg-if 0.1.10", "libc", @@ -1013,7 +1019,7 @@ version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83450fe6a6142ddd95fb064b746083fc4ef1705fe81f64a64e1d4b39f54a1055" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cc", "cfg-if 0.1.10", "libc", @@ -1025,7 +1031,7 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f17df307904acd05aa8e32e97bb20f2a0df1728bbc2d771ae8f9a90463441e9" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if 1.0.0", "libc", "memoffset", @@ -1127,11 +1133,11 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "openssl" -version = "0.10.55" +version = "0.10.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "345df152bc43501c5eb9e4654ff05f794effb78d4efe3d53abc158baddc0703d" +checksum = "79a4c6c3a2b158f7f8f2a2fc5a969fa3a068df6fc9dbb4a43845436e3af7c800" dependencies = [ - "bitflags", + "bitflags 2.4.1", "cfg-if 1.0.0", "foreign-types", "libc", @@ -1159,9 +1165,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.90" +version = "0.9.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "374533b0e45f3a7ced10fcaeccca020e66656bc03dac384f852e4e5a7a8104a6" +checksum = "3812c071ba60da8b5677cc12bcb1d42989a65553772897a7e0355545a819838f" dependencies = [ "cc", "libc", @@ -1444,7 +1450,7 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1584,7 +1590,7 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dc14f172faf8a0194a3aded622712b0de276821addc574fa54fc0a1167e10dc" dependencies = [ - "bitflags", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -2187,7 +2193,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab702fefbcd6d6f67fb5816e3a89a3b5a42a94290abbc015311c9a30d1068ae4" dependencies = [ - "bitflags", + "bitflags 1.3.2", "downcast-rs", "libc", "nix 0.17.0", @@ -2214,7 +2220,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3d6fc54b17b98b5083bc21ae3a30e6d75cb4b01647360e4c3a04648bcf8781d" dependencies = [ - "bitflags", + "bitflags 1.3.2", "wayland-client", "wayland-commons", "wayland-scanner", diff --git a/docs/README.md b/docs/README.md index 9a7b0e8..5e90532 100644 --- a/docs/README.md +++ b/docs/README.md @@ -66,3 +66,104 @@ cargo run -- ``` where `` are the command-line arguments you would pass the DidYouMean binary. Note that this is an unoptimized build contianing debug information so it runs much, much slower. +Adding New Language +=================== +Update the language code in the list at + + +Update the language wordlist +============================ +``` +$ cargo run -- --lang ta --update-langs --wordlist-url https://raw.githubusercontent.com/arcturusannamalai/wordlists/main/ + Finished dev [unoptimized + debuginfo] target(s) in 0.17s + Running `target/debug/dym --lang ta --update-langs --wordlist-url 'https://raw.githubusercontent.com/arcturusannamalai/wordlists/main/'` +Downloading English word list... +Accessing URL: https://raw.githubusercontent.com/arcturusannamalai/wordlists/main//en +[00:00:00] [############################################################################################################################] 4.12MiB/4.12MiB (0s) +Downloading Tamil word list... +Accessing URL: https://raw.githubusercontent.com/arcturusannamalai/wordlists/main//ta +[00:00:00] [############################################################################################################################] 1.73MiB/1.73MiB (0s) +``` + +Run did you mean: +================ +e.g. +``` +cargo run -- --lang ta கலஅ + Compiling didyoumean v1.1.4 (/Users/user/devel/rust-in-action/didyoumean) + Finished dev [unoptimized + debuginfo] target(s) in 1.18s + Running `target/debug/dym --lang ta 'கலஅ'` +Did you mean? +1. கலி +2. கலை +3. கல் +4. அ +5. அகல் + +``` + + +For more info see the help text and options, +``` +$ cargo run -- --help + +didyoumean user$ cargo run -- --help + Compiling didyoumean v1.1.4 (/Users/user/devel/rust-in-action/didyoumean) + Finished dev [unoptimized + debuginfo] target(s) in 2.24s + Running `target/debug/dym --help` +didyoumean 1.1.4 +Hisbaan Noorani +Did You Mean: A cli spelling corrector + +USAGE: + dym [OPTIONS] [SEARCH_TERM] + +ARGS: + + + +OPTIONS: + -c, --clean-output + Print a clean version of the output without the title, numbers or colour. + + -h, --help + Print help information + + -l, --lang + Select the desired language using its locale code. For example, English would have the + locale code en and French would have the locale code fr. See --print-langs for a list of + locale codes and the corresponding languages. + + [default: en] + + -n, --number + Change the number of words the program will print. The default value is five. + + [default: 5] + + --print-langs + Display a list of supported languages and their respective locale codes. + + --update-langs + Update all language files from the repository specified by CLI @wordlist-url@. + + -v, --verbose + Print verbose output including the edit distance of the found word to the queried word. + + -V, --version + Print version information + + -w, --wordlist-url + Wordlist repository URL. The default value is + 'https://raw.githubusercontent.com/hisbaan/wordlists/main' + + [default: https://raw.githubusercontent.com/hisbaan/wordlists/main] + + -y, --yank + Yank (copy) the selected word to the system clipboard. If no word is selected, the + clipboard will not be altered. + +``` + + + diff --git a/src/cli.rs b/src/cli.rs index eaacca9..bdae8bc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -51,7 +51,15 @@ pub struct Cli { #[clap( long = "update-langs", help = "Update all language files", - long_help = "Update all language files from the repository https://github.com/hisbaan/wordlists." + long_help = "Update all language files from the repository specified by CLI @wordlist-url@." )] pub update_langs: bool, + #[clap( + short = 'w', + long = "wordlist-url", + help = "Wordlist repository URL", + long_help = "Wordlist repository URL. The default value is 'https://raw.githubusercontent.com/hisbaan/wordlists/main'", + default_value = "https://raw.githubusercontent.com/hisbaan/wordlists/main" + )] + pub wordlist_url: String, } diff --git a/src/langs.rs b/src/langs.rs index 4114c03..ed5b73d 100644 --- a/src/langs.rs +++ b/src/langs.rs @@ -64,6 +64,7 @@ pub static LOCALES: phf::Map<&'static str, &'static str> = phf_map! { "st" => "Sesotho", "sv" => "Swedish", "sw" => "Swahili", + "ta" => "Tamil", "tg" => "Tajik", "th" => "Thai", "tk" => "Turkmen", @@ -129,6 +130,7 @@ pub static SUPPORTED_LANGS: phf::Map<&'static str, &'static str> = phf_map! { "st" => "Sesotho", "sv" => "Swedish", "sw" => "Swahili", + "ta" => "Tamil", "tg" => "Tajik", "tk" => "Turkmen", "tl" => "Tagalog", diff --git a/src/lib.rs b/src/lib.rs index 45f1000..2a11137 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ pub fn yank(string: &str) { "freebsd", "netbsd", "dragonfly", + "mac osx", "netbsd", "openbsd", "solaris", diff --git a/src/main.rs b/src/main.rs index f976982..4e37c26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,11 @@ fn run_app() -> std::result::Result<(), Error> { // Update all downloaded languages. if args.update_langs { - update_langs(); + assert!( + !args.wordlist_url.ends_with("/"), + "URL should end with branch name in github without trailing /" + ); + update_langs(args.wordlist_url); std::process::exit(0); } @@ -88,7 +92,7 @@ fn run_app() -> std::result::Result<(), Error> { } if SUPPORTED_LANGS.contains_key(args.lang.as_str()) { - fetch_word_list(args.lang.to_owned()); + fetch_word_list(args.lang.to_owned(), args.wordlist_url.to_owned()); } else { // Not supported // Initialize new command. @@ -110,14 +114,13 @@ fn run_app() -> std::result::Result<(), Error> { // Exit with error. clap::Error::exit(&error); } - // Get word list. The program will only get here if/when this is a valid word list. let word_list = read_to_string(dirs::data_dir().unwrap().join("didyoumean").join(args.lang)) .expect("Error reading file"); // Get dictionary of words from words.txt. let dictionary = word_list.split('\n'); - + //assert!(dictionary.clone().count()>20,"Size of wordlist > 20 words"); // Create mutable vecs for storing the top n words. let mut top_n_words = vec![""; args.number]; let mut top_n_dists = vec![search_term.len() * 10; args.number]; @@ -224,7 +227,7 @@ fn run_app() -> std::result::Result<(), Error> { /// /// * `lang` - A locale code string to define the word list file to fetch. #[tokio::main] -async fn fetch_word_list(lang: String) { +async fn fetch_word_list(lang: String, wordlist_url: String) { // Get data directory. let data_dir = dirs::data_dir().unwrap().join("didyoumean"); @@ -243,11 +246,8 @@ async fn fetch_word_list(lang: String) { LOCALES.get(&lang).unwrap().to_string().blue() ); - let url = format!( - "https://raw.githubusercontent.com/hisbaan/wordlists/main/{}", - &lang - ); - + let url = format!("{}/{}", wordlist_url, &lang); + println!("Accessing URL: {}", url); // Setup reqwest. let response = get(&url).await.expect("Request failed"); let total_size = response.content_length().unwrap(); @@ -280,7 +280,7 @@ async fn fetch_word_list(lang: String) { } /// Update the word list files by deleting and downloading the files from the repository. -fn update_langs() { +fn update_langs(wordlist_url: String) { let data = data_dir().unwrap().join("didyoumean"); // Create data directory if it doesn't exist. @@ -299,7 +299,7 @@ fn update_langs() { // Only delete and download if the language is supported. if SUPPORTED_LANGS.contains_key(string) { remove_file(data.join(&string)).expect("Failed to update file (deletion failed)"); - fetch_word_list(string.to_string()); + fetch_word_list(string.to_string(), wordlist_url.to_string()); } } }