Skip to main content

codeberg_cli/actions/user/
info.rs

1use forgejo_api::structs::UserCurrentListReposQuery;
2use itertools::Itertools;
3use miette::IntoDiagnostic;
4
5use crate::actions::GlobalArgs;
6use crate::render::json::JsonToStdout;
7use crate::render::option::option_display;
8use crate::render::spinner::spin_until_ready;
9use crate::types::context::BergContext;
10use crate::types::output::OutputMode;
11
12use clap::Parser;
13
14/// Display short summary of the authenticated user account
15#[derive(Parser, Debug)]
16pub struct UserInfoArgs {}
17
18impl UserInfoArgs {
19    pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
20        let ctx = BergContext::new(self, global_args).await?;
21
22        present_user_info(&ctx).await?;
23        Ok(())
24    }
25}
26
27async fn present_user_info(ctx: &BergContext<UserInfoArgs>) -> miette::Result<()> {
28    let user = spin_until_ready(ctx.client.user_get_current().send())
29        .await
30        .into_diagnostic()?;
31    let (_, repos) = ctx
32        .client
33        .user_current_list_repos(UserCurrentListReposQuery::default())
34        .await
35        .into_diagnostic()?;
36    let top_repos = repos
37        .iter()
38        .sorted_by_key(|repo| repo.stars_count.unwrap_or_default())
39        .rev()
40        .take(5)
41        .collect::<Vec<_>>();
42
43    match ctx.global_args.output_mode {
44        OutputMode::Pretty => {
45            let table = ctx
46                .make_table()
47                .set_header(vec!["User Information"])
48                .add_row(vec![String::from("Username"), option_display(&user.login)])
49                .add_row(vec![
50                    String::from("Followers"),
51                    option_display(&user.followers_count),
52                ])
53                .add_row(vec![
54                    String::from("Following"),
55                    option_display(&user.following_count),
56                ])
57                .add_row(vec![String::from("Repos"), repos.len().to_string()])
58                .add_row(vec![
59                    String::from("Top Repos"),
60                    top_repos
61                        .into_iter()
62                        .map(|repo| {
63                            format!(
64                                "{}({}★)",
65                                option_display(&repo.name),
66                                option_display(&repo.stars_count)
67                            )
68                        })
69                        .join(", "),
70                ]);
71
72            println!("{table}", table = table.show());
73        }
74        OutputMode::Json => {
75            user.print_json()?;
76        }
77    }
78
79    Ok(())
80}