Skip to content

Commit

Permalink
add --help and --version
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Nov 26, 2021
1 parent 71a526b commit a712775
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "caniuse-rs"
description = "A command line client for caniuse.com, written in Rust."
version = "0.1.11"
version = "0.2.0"
edition = "2021"
license = "MIT"
homepage = "https://github.com/mrjones2014/caniuse-rs"
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ pressing enter will search for the query you've typed by opening `https://canius

## Options

To dump the data instead of fuzzy finding, you can run `caniuse --dump` which will simply output the
currently cached JSON data that is being used. To pretty-print it, you can run `caniuse --dump --pretty`.

To force update the cached data, you can run `caniuse --update`.
```
caniuse [options]
OPTIONS:
--dump Dump the currently cached JSON data and exit
--pretty Pretty-print the JSON output, must be combined with --dump or --query [query]
--query [query] Output JSON instead of using fuzzy-finder, used for Alfred integration
--update Force update the currently cached JSON data and exit
--version Print the version and exit
--help Show this help text
```
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<key>script</key>
<string>query=$1
./caniuse --alfred $query</string>
./caniuse --query $query</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
Expand Down
11 changes: 9 additions & 2 deletions src/alfred_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ impl From<Feature> for AlfredItem {
}
}

pub fn get_json(features: &[Feature], query: &str) -> Result<String, serde_json::Error> {
pub fn get_json(
features: &[Feature],
query: &str,
pretty: &bool,
) -> Result<String, serde_json::Error> {
let alfred_items = AlfredItemList {
items: features
.iter()
Expand All @@ -39,5 +43,8 @@ pub fn get_json(features: &[Feature], query: &str) -> Result<String, serde_json:
.collect(),
};

serde_json::to_string(&alfred_items)
match pretty {
true => serde_json::to_string_pretty(&alfred_items),
false => serde_json::to_string(&alfred_items),
}
}
13 changes: 13 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
use std::env;

pub const HELP_TEXT: &str = r#"
caniuse [options]
OPTIONS:
--dump Dump the currently cached JSON data and exit
--pretty Pretty-print the JSON output, must be combined with --dump or --query [query]
--query [query] Output JSON instead of using fuzzy-finder, used for Alfred integration
--update Force update the currently cached JSON data and exit
--version Print the version and exit
--help Show this help text
"#;

lazy_static! {
pub static ref CACHE_PATH: String = match env::var("HOME") {
Ok(home) => format!("{}/.cache/caniuse-rs/data.json", home),
Expand Down
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,40 @@ mod url;

fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().map(|arg| arg.to_lowercase()).collect();

let version = env!("CARGO_PKG_VERSION");
if args.contains(&String::from("--version")) {
println!("caniuse v{}", version);
return Ok(());
}

let update_cache = args.contains(&String::from("--update"));
api::ensure_cached_data(update_cache)?;
if update_cache {
println!("Updated cache written to {}", &*constants::CACHE_PATH);
return Ok(());
}

if args.contains(&String::from("--help")) {
println!("{}", constants::HELP_TEXT);
return Ok(());
}

let features = api::get_data()?;

if args.contains(&String::from("--alfred")) {
if args.contains(&String::from("--query")) {
if args.len() < 2 {
panic!("--alfred must be the only option and must immediately be followed by a query");
panic!("--query must must immediately be followed by a query");
}
let query = args[2..].join(" ").to_lowercase();
println!("{}", alfred_integration::get_json(&features, &query)?);
let query = &args[2];
println!(
"{}",
alfred_integration::get_json(
&features,
&query,
&args.contains(&String::from("--pretty"))
)?
);
return Ok(());
}

Expand Down

0 comments on commit a712775

Please sign in to comment.