Add search --new to filter already installed modules
This commit is contained in:
@@ -136,6 +136,9 @@ pub struct Install {
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Search {
|
||||
/// Only show modules that aren't installed yet
|
||||
#[structopt(long="new")]
|
||||
pub new: bool,
|
||||
/// The search query
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
@@ -181,14 +181,22 @@ impl<'a> Engine<'a> {
|
||||
}
|
||||
|
||||
pub fn get(&self, name: &str) -> Result<&Module> {
|
||||
if let Some(module) = self.get_opt(name)? {
|
||||
Ok(module)
|
||||
} else {
|
||||
bail!("Module not found")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_opt(&self, name: &str) -> Result<Option<&Module>> {
|
||||
if let Some(modules) = self.modules.get(name) {
|
||||
if modules.len() != 1 {
|
||||
bail!("Ambiguous name: {:?}", modules)
|
||||
} else {
|
||||
Ok(&modules[0])
|
||||
Ok(Some(&modules[0]))
|
||||
}
|
||||
} else {
|
||||
bail!("Module not found")
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,11 +209,6 @@ impl<'a> Engine<'a> {
|
||||
modules
|
||||
}
|
||||
|
||||
pub fn is_installed(&self, canonical: &str) -> bool {
|
||||
// TODO: maybe also compare version
|
||||
self.modules.get(canonical).is_some()
|
||||
}
|
||||
|
||||
pub fn variants(&self) -> Vec<String> {
|
||||
self.modules.iter()
|
||||
.filter(|(_, values)| values.len() == 1)
|
||||
|
||||
@@ -97,11 +97,17 @@ pub fn run_search(engine: &Engine, search: &Search, config: &Config) -> Result<(
|
||||
|
||||
for module in &modules {
|
||||
let canonical = module.canonical();
|
||||
let installed = engine.get_opt(&canonical)?;
|
||||
|
||||
if search.new && installed.is_some() {
|
||||
continue;
|
||||
}
|
||||
|
||||
println!("{} ({}) - {} downloads{}{}", canonical.green(),
|
||||
module.latest.yellow(),
|
||||
module.downloads.separated_string(),
|
||||
(if module.featured { " [featured]" } else { "" }).cyan(),
|
||||
(if engine.is_installed(&canonical) { " [installed]" } else { "" }).green());
|
||||
(if installed.is_some() { " [installed]" } else { "" }).green());
|
||||
println!("\t{}", module.description);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user