use cargo::ops;
use cargo::util::{CliResult, Config};
+use std::cmp;
+
#[derive(RustcDecodable)]
pub struct Options {
flag_host: Option<String>,
flag_verbose: Option<bool>,
flag_quiet: Option<bool>,
flag_color: Option<String>,
+ flag_limit: Option<u32>,
arg_query: String
}
-v, --verbose Use verbose output
-q, --quiet No output printed to stdout
--color WHEN Coloring: auto, always, never
+ --limit LIMIT Limit the number of results (default: 10, max: 100)
";
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
&options.flag_color));
let Options {
flag_host: host,
+ flag_limit: limit,
arg_query: query,
..
} = options;
- try!(ops::search(&query, config, host));
+ try!(ops::search(&query, config, host, cmp::min(100, limit.unwrap_or(10)) as u8));
Ok(None)
}
Ok(())
}
-pub fn search(query: &str, config: &Config, index: Option<String>) -> CargoResult<()> {
+pub fn search(query: &str,
+ config: &Config,
+ index: Option<String>,
+ limit: u8) -> CargoResult<()> {
fn truncate_with_ellipsis(s: &str, max_length: usize) -> String {
if s.len() < max_length {
s.to_string()
}
let (mut registry, _) = try!(registry(config, None, index));
- let crates = try!(registry.search(query).map_err(|e| {
+ let crates = try!(registry.search(query, limit).map_err(|e| {
human(format!("failed to retrieve search results from the registry: {}", e))
}));
Ok(())
}
- pub fn search(&mut self, query: &str) -> Result<Vec<Crate>> {
- let body = try!(self.req(format!("/crates?q={}", query), None, Get,
+ pub fn search(&mut self, query: &str, limit: u8) -> Result<Vec<Crate>> {
+ let body = try!(self.req(format!("/crates?q={}&per_page={}", query, limit), None, Get,
Auth::Unauthorized));
Ok(json::decode::<Crates>(&body).unwrap().crates)
// from source there anyway!
File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap();
if !cfg!(windows) {
- File::create(&base.with_file_name("crates?q=postgres")).unwrap()
+ File::create(&base.with_file_name("crates?q=postgres&per_page=10")).unwrap()
.write_all(contents.as_bytes()).unwrap();
}