You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
amethyst/src/operations/search.rs

139 lines
3.8 KiB
Rust

use crate::internal::commands::ShellCommand;
use crate::internal::error::SilentUnwrap;
use crate::internal::exit_code::AppExitCode;
use crate::internal::rpc::rpcsearch;
use crate::{log, Options};
use chrono::{Local, TimeZone};
use colored::Colorize;
use textwrap::wrap;
3 years ago
2 years ago
#[allow(clippy::module_name_repetitions)]
pub fn aur_search(query: &str, options: Options) -> String {
// Query AUR for package info
2 years ago
let res = rpcsearch(query);
3 years ago
// Get verbosity
let verbosity = options.verbosity;
// Format output
let mut results_vec = vec![];
for package in &res.results {
// Define wrapping options
let opts = textwrap::Options::new(crossterm::terminal::size().unwrap().0 as usize - 4)
.subsequent_indent(" ");
let result = format!(
"{}{} {} {}\n {}",
"aur/".cyan().bold(),
package.name.bold(),
package.version.green().bold(),
if package.out_of_date.is_some() {
format!(
"[out of date: since {}]",
Local
.timestamp(package.out_of_date.unwrap().try_into().unwrap(), 0)
.date_naive()
)
.red()
.bold()
} else {
"".bold()
},
wrap(
package
.description
.as_ref()
.unwrap_or(&"No description".to_string()),
opts
)
.join("\n"),
2 years ago
);
results_vec.push(result);
3 years ago
}
if verbosity > 1 {
log!(
"Found {} results for \"{}\" in the AUR",
res.results.len(),
query
);
}
results_vec.join("\n")
3 years ago
}
struct SearchResult {
repo: String,
name: String,
version: String,
description: String,
}
2 years ago
#[allow(clippy::module_name_repetitions)]
pub fn repo_search(query: &str, options: Options) -> String {
// Initialise variables
3 years ago
let verbosity = options.verbosity;
// Query pacman for package info
let output = ShellCommand::bash()
.args(&["-c", &format!("expac -Ss '%r\\\\%n\\\\%v\\\\%d' {}", query)])
.arg(query)
.wait_with_output()
.silent_unwrap(AppExitCode::PacmanError)
.stdout;
3 years ago
// Split output into lines
let lines = output.trim().split('\n');
// Initialise results vector
let mut results_vec: Vec<SearchResult> = vec![];
// Iterate over lines
for line in lines {
let parts: Vec<&str> = line.split('\\').collect();
let res = SearchResult {
repo: parts[0].to_string(),
name: parts[1].to_string(),
version: parts[2].to_string(),
description: parts[3].to_string(),
};
results_vec.push(res);
}
3 years ago
if verbosity >= 1 {
log!(
"Found {} results for \"{}\" in repos",
&results_vec.len(),
&query
);
3 years ago
}
// Format output
let results_vec = results_vec
.into_iter()
.map(|res| {
let opts = textwrap::Options::new(crossterm::terminal::size().unwrap().0 as usize - 4)
.subsequent_indent(" ");
format!(
"{}{}{} {}\n {}",
res.repo.purple().bold(),
"/".purple().bold(),
res.name.bold(),
res.version.green().bold(),
if res.description.is_empty() {
"No description".to_string()
} else {
wrap(&res.description, opts).join("\n")
},
)
})
.collect::<Vec<String>>();
if output.trim().is_empty() {
"".to_string()
} else {
results_vec.join("\n")
}
3 years ago
}