codeberg_cli/actions/label/
list.rs1use forgejo_api::structs::{IssueListLabelsQuery, Label};
2
3use crate::actions::GlobalArgs;
4use crate::render::json::JsonToStdout;
5use crate::render::option::option_display;
6use crate::render::spinner::spin_until_ready;
7
8use crate::types::context::BergContext;
9use crate::types::git::OwnerRepo;
10use crate::types::output::OutputMode;
11
12use clap::Parser;
13
14#[derive(Parser, Debug)]
16pub struct ListLabelsArgs {
17 #[arg(short, long, value_name = "N", default_value_t = 5)]
19 pub count: usize,
20}
21
22impl ListLabelsArgs {
23 pub async fn run(self, global_args: GlobalArgs) -> anyhow::Result<()> {
24 let ctx = BergContext::new(self, global_args).await?;
25
26 let OwnerRepo { repo, owner } = ctx.owner_repo()?;
27 let (_, labels_list) = spin_until_ready(
28 ctx.client
29 .issue_list_labels(
30 owner.as_str(),
31 repo.as_str(),
32 IssueListLabelsQuery::default(),
33 )
34 .send(),
35 )
36 .await?;
37
38 match ctx.global_args.output_mode {
39 OutputMode::Pretty => {
40 present_labels_list(&ctx, labels_list);
41 }
42 OutputMode::Json => labels_list.print_json()?,
43 }
44
45 Ok(())
46 }
47}
48
49fn present_labels_list(ctx: &BergContext<ListLabelsArgs>, labels: Vec<Label>) {
50 let labels_empty = labels.is_empty();
51
52 let mut table = ctx.make_table();
53
54 table
55 .set_header(vec![format!(
56 "Labels{}",
57 if labels_empty {
58 " (empty)"
59 } else {
60 Default::default()
61 }
62 )])
63 .add_rows(
64 labels
65 .into_iter()
66 .map(|label| vec![option_display(&label.name)]),
67 );
68
69 println!("{table}", table = table.show());
70}