Skip to content

Commit

Permalink
refactor(search): add description search and limit
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Nov 8, 2024
1 parent bfe004f commit 4bbe1f3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ pub enum Commands {
query: String,

/// Case sensitive search
#[arg(required = false, long)]
#[arg(required = false, long, alias = "exact")]
case_sensitive: bool,

/// Limit number of result
#[arg(required = false, long)]
limit: Option<usize>,
},

/// Query package info
Expand Down
4 changes: 4 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct Config {

/// Limit the number of parallel downloads
pub parallel_limit: Option<u32>,

/// Limit the number of search results to display
pub search_limit: Option<usize>,
}

/// Struct representing a repository configuration.
Expand Down Expand Up @@ -90,6 +93,7 @@ impl Default for Config {
}],
parallel: Some(true),
parallel_limit: Some(4),
search_limit: Some(20),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ pub async fn init() -> Result<()> {
Commands::Search {
query,
case_sensitive,
limit,
} => {
registry.search(&query, case_sensitive).await?;
registry.search(&query, case_sensitive, limit).await?;
}
Commands::Query { query } => {
registry.query(&query).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/package/appimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const SUPPORTED_DIMENSIONS: &[(u32, u32)] = &[
(512, 512),
];

async fn find_offset(file: &mut BufReader<File>) -> Result<u64> {
fn find_offset(file: &mut BufReader<File>) -> Result<u64> {
let mut magic = [0_u8; 4];
// Little-Endian v4.0
let kind = Kind::from_target("le_v4_0").unwrap();
Expand Down Expand Up @@ -144,7 +144,7 @@ pub async fn integrate_appimage(
package: &Package,
file_path: &Path,
) -> Result<bool> {
let offset = find_offset(file).await?;
let offset = find_offset(file)?;
let squashfs = FilesystemReader::from_reader_with_offset(file, offset)?;

let home_data = home_data_path();
Expand Down
16 changes: 14 additions & 2 deletions src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct PackageQuery {
}

pub fn parse_package_query(query: &str) -> PackageQuery {
let query = query.to_lowercase();
let (base_query, collection) = query
.rsplit_once('#')
.map(|(n, r)| (n.to_owned(), (!r.is_empty()).then(|| r.to_lowercase())))
Expand All @@ -131,8 +132,19 @@ pub fn ask_package_info(name: &str, path: &Path, size: u64) -> Result<ResolvedPa
let bin_name = interactive_ask("Binary Name: ", AskType::Normal)?;
if bin_name.is_empty() {
eprintln!("Binary name can't be empty.");
} else if !bin_name.chars().all(|c| c.is_alphanumeric()) {
eprintln!("Binary name must only contain letters and numbers.");
} else if !bin_name
.chars()
.all(|c| c.is_alphanumeric() || c == '-' || c == '_')
{
eprintln!(
"Binary name must only contain letters, numbers, hyphens (-), or underscores (_)."
);
} else if bin_name.starts_with('-')
|| bin_name.starts_with('_')
|| bin_name.ends_with('-')
|| bin_name.ends_with('_')
{
eprintln!("Binary name can't start or end with a hyphen (-) or underscore (_).");
} else {
break bin_name;
}
Expand Down
24 changes: 20 additions & 4 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,43 @@ impl PackageRegistry {
.await
}

pub async fn search(&self, package_name: &str, case_sensitive: bool) -> Result<()> {
pub async fn search(
&self,
package_name: &str,
case_sensitive: bool,
limit: Option<usize>,
) -> Result<()> {
let limit = limit.unwrap_or(CONFIG.search_limit.unwrap_or(20));
let installed_guard = self.installed_packages.lock().await;
let result = self.storage.search(package_name, case_sensitive).await;

if result.is_empty() {
Err(anyhow::anyhow!("No packages found"))
} else {
result.iter().for_each(|pkg| {
let displayed_results = result.iter().take(limit).collect::<Vec<_>>();
displayed_results.iter().for_each(|pkg| {
let installed = if installed_guard.is_installed(pkg) {
"+"
} else {
"-"
};
println!(
"[{}] [{}] {}: {}",
"[{}] [{}] {}: {} ({})",
installed,
pkg.collection.clone().color(Color::BrightGreen),
pkg.package.full_name('/').color(Color::Blue),
pkg.package.full_name('/').color(Color::BrightBlue),
pkg.package.description,
pkg.package.size.clone().color(Color::BrightMagenta)
);
});

if result.len() > limit {
println!(
"\x1b[5mShowing {} of {} results\x1b[0m",
limit,
result.len()
);
}
Ok(())
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/registry/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,17 @@ impl PackageStorage {
.flat_map(|(_, packages)| {
packages.iter().filter_map(|pkg| {
let mut score = 0;
let found_pkg_name = if case_sensitive {
pkg.pkg.clone()
let (found_pkg_name, found_pkg_description) = if case_sensitive {
(pkg.pkg.clone(), pkg.description.clone())
} else {
pkg.pkg.to_lowercase()
(pkg.pkg.to_lowercase(), pkg.description.to_lowercase())
};

if found_pkg_name == pkg_name {
score += 2;
score += 5;
} else if found_pkg_name.contains(&pkg_name) {
score += 3;
} else if found_pkg_description.contains(&pkg_name) {
score += 1;
} else {
return None;
Expand Down

0 comments on commit 4bbe1f3

Please sign in to comment.