mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2024-11-10 12:09:33 +01:00
feat(user): user search
This commit is contained in:
parent
2aa4b152ff
commit
38da0e6285
1 changed files with 63 additions and 0 deletions
63
src/user.rs
63
src/user.rs
|
@ -14,6 +14,12 @@ pub struct UserCommand {
|
||||||
|
|
||||||
#[derive(Subcommand, Clone, Debug)]
|
#[derive(Subcommand, Clone, Debug)]
|
||||||
pub enum UserSubcommand {
|
pub enum UserSubcommand {
|
||||||
|
Search {
|
||||||
|
/// The name to search for
|
||||||
|
query: String,
|
||||||
|
#[clap(long, short)]
|
||||||
|
page: Option<usize>,
|
||||||
|
},
|
||||||
View {
|
View {
|
||||||
/// The name of the user to view
|
/// The name of the user to view
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
|
@ -67,6 +73,7 @@ impl UserCommand {
|
||||||
let repo = RepoInfo::get_current(host_name, None, self.remote.as_deref())?;
|
let repo = RepoInfo::get_current(host_name, None, self.remote.as_deref())?;
|
||||||
let api = keys.get_api(repo.host_url()).await?;
|
let api = keys.get_api(repo.host_url()).await?;
|
||||||
match self.command {
|
match self.command {
|
||||||
|
UserSubcommand::Search { query, page } => user_search(&api, &query, page).await?,
|
||||||
UserSubcommand::View { user } => view_user(&api, user.as_deref()).await?,
|
UserSubcommand::View { user } => view_user(&api, user.as_deref()).await?,
|
||||||
UserSubcommand::Browse { user } => {
|
UserSubcommand::Browse { user } => {
|
||||||
browse_user(&api, repo.host_url(), user.as_deref()).await?
|
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<usize>) -> 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<()> {
|
async fn view_user(api: &Forgejo, user: Option<&str>) -> eyre::Result<()> {
|
||||||
let SpecialRender {
|
let SpecialRender {
|
||||||
bold,
|
bold,
|
||||||
|
|
Loading…
Reference in a new issue