From 38da0e62856e75c8a3aac40ade6f01c5ec23d36f Mon Sep 17 00:00:00 2001 From: Cyborus Date: Tue, 18 Jun 2024 17:47:35 -0400 Subject: [PATCH] feat(user): user search --- src/user.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/user.rs b/src/user.rs index 51ed7d7..86af8be 100644 --- a/src/user.rs +++ b/src/user.rs @@ -14,6 +14,12 @@ pub struct UserCommand { #[derive(Subcommand, Clone, Debug)] pub enum UserSubcommand { + Search { + /// The name to search for + query: String, + #[clap(long, short)] + page: Option, + }, View { /// The name of the user to view user: Option, @@ -67,6 +73,7 @@ impl UserCommand { let repo = RepoInfo::get_current(host_name, None, self.remote.as_deref())?; let api = keys.get_api(repo.host_url()).await?; match self.command { + UserSubcommand::Search { query, page } => user_search(&api, &query, page).await?, UserSubcommand::View { user } => view_user(&api, user.as_deref()).await?, UserSubcommand::Browse { user } => { browse_user(&api, repo.host_url(), user.as_deref()).await? @@ -88,6 +95,62 @@ impl UserCommand { } } +async fn user_search(api: &Forgejo, query: &str, page: Option) -> eyre::Result<()> { + let page = page.unwrap_or(1); + if page == 0 { + println!("There is no page 0"); + } + let query = forgejo_api::structs::UserSearchQuery { + q: Some(query.to_owned()), + ..Default::default() + }; + let result = api.user_search(query).await?; + let users = result.data.ok_or_eyre("search did not return data")?; + let ok = result.ok.ok_or_eyre("search did not return ok")?; + if !ok { + println!("Search failed"); + return Ok(()); + } + if users.is_empty() { + println!("No users matched that query"); + } else { + let SpecialRender { + bullet, + dash, + bold, + reset, + .. + } = *crate::special_render(); + let page_start = (page - 1) * 20; + let pages_total = users.len().div_ceil(20); + if page_start >= users.len() { + if pages_total == 1 { + println!("There is only 1 page"); + } else { + println!("There are only {pages_total} pages"); + } + } else { + for user in users.iter().skip(page_start).take(20) { + let username = user + .login + .as_deref() + .ok_or_eyre("user does not have name")?; + println!("{bullet} {bold}{username}{reset}"); + } + println!( + "Showing {bold}{}{dash}{}{reset} of {bold}{}{reset} results ({page}/{pages_total})", + page_start + 1, + (page_start + 20).min(users.len()), + users.len() + ); + if users.len() > 20 { + println!("View more with the --page flag"); + } + } + } + Ok(()) +} + async fn view_user(api: &Forgejo, user: Option<&str>) -> eyre::Result<()> { let SpecialRender { bold,