diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-02-10 11:01:07 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-02-10 11:01:07 -0800 |
commit | ea843b324ce3d453356bfc356fd9163336130c48 (patch) | |
tree | b3e54bfde9f5e9e23306361c654bba8b6d90fb4f /fatcat-cli/src/main.rs | |
parent | d4d8caf98bd3595f182d014e4606bbb79dafee05 (diff) | |
download | fatcat-cli-ea843b324ce3d453356bfc356fd9163336130c48.tar.gz fatcat-cli-ea843b324ce3d453356bfc356fd9163336130c48.zip |
default search output as a table
Diffstat (limited to 'fatcat-cli/src/main.rs')
-rw-r--r-- | fatcat-cli/src/main.rs | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/fatcat-cli/src/main.rs b/fatcat-cli/src/main.rs index 6e3f2c7..72cb169 100644 --- a/fatcat-cli/src/main.rs +++ b/fatcat-cli/src/main.rs @@ -224,9 +224,9 @@ enum Command { json: bool, }, Search { - entity_type: EntityType, + entity_type: SearchEntityType, - terms: Vec<String>, + query: Vec<String>, #[structopt(long = "--expand")] expand: Option<String>, @@ -234,11 +234,17 @@ enum Command { #[structopt(long = "--hide")] hide: Option<String>, + #[structopt(long = "--count")] + count: bool, + #[structopt(long, short = "-n", default_value = "20")] limit: i64, - #[structopt(long = "--search-schema")] - search_schema: bool, + #[structopt(long = "--entity-json")] + entity_json: bool, + + #[structopt(long = "--index-json")] + index_json: bool, }, Editgroup { #[structopt(subcommand)] @@ -509,34 +515,46 @@ fn run(opt: Opt) -> Result<()> { } Command::Search { entity_type, - terms, + query, limit, - search_schema, + count, + entity_json, + index_json, expand, hide, } => { - let limit: Option<u64> = match limit { - l if l <= 0 => None, - l => Some(l as u64), + // TODO: ensure that we don't try print more than ~1000 hits with tabwriter? + let limit: Option<u64> = match (count, limit) { + (true, _) => Some(0), + (false, l) if l <= 0 => None, + (false, l) => Some(l as u64), }; - let results = fatcat_cli::crude_search(&opt.search_host, entity_type, limit, terms) + let results = fatcat_cli::crude_search(&opt.search_host, entity_type, limit, query) .with_context(|| format!("searching for {:?}", entity_type))?; - eprintln!("Got {} hits in {}ms", results.count, results.took_ms); - for hit in results { - let hit = hit?; - match (search_schema, entity_type) { - (true, _) => writeln!(&mut std::io::stdout(), "{}", hit.to_string())?, - (false, EntityType::Release) => { - let specifier = - Specifier::Release(hit["ident"].as_str().unwrap().to_string()); - let entity = specifier.get_from_api( - &mut api_client, - expand.clone(), - hide.clone(), - )?; - writeln!(&mut std::io::stdout(), "{}", entity.to_json_string()?)? + if count { + println!("{}", results.count); + } else { + eprintln!("Got {} hits in {}ms", results.count, results.took_ms); + if !(index_json || entity_json) { + print_search_table(results, entity_type)?; + } else { + for hit in results { + let hit = hit?; + match (index_json, entity_json, entity_type) { + (false, false, _) => unreachable!("case handled above"), + (true, _, _) => writeln!(&mut std::io::stdout(), "{}", hit.to_string())?, + (false, true, SearchEntityType::Release) => { + let specifier = + Specifier::Release(hit["ident"].as_str().unwrap().to_string()); + let entity = specifier.get_from_api( + &mut api_client, + expand.clone(), + hide.clone(), + )?; + writeln!(&mut std::io::stdout(), "{}", entity.to_json_string()?)? + } + } } - (false, _) => unimplemented!("searching other entity types"), } } } |