codeberg-cli 0.5.5

CLI Tool for codeberg similar to gh and glab
Documentation
use crate::render::json::JsonToStdout;
use crate::types::context::BergContext;
use crate::types::output::OutputMode;
use crate::{actions::GlobalArgs, render::option::option_display};
use forgejo_api::structs::{NotificationThread, NotifyGetListQuery};

use clap::Parser;
use miette::IntoDiagnostic;

#[derive(Debug, Parser)]
pub struct ListNotificationArgs {
    /// just list everything
    #[arg(short, long, default_value_t = false)]
    pub all: bool,

    /// filter notifications by status
    #[arg(long, default_values_t = ["Unread", "Pinned"].map(String::from).to_vec())]
    pub status_types: Vec<String>,

    /// filter notifications by subject type
    #[arg(long)]
    pub subject_type: Option<String>,

    /// filter notifications by date
    #[arg(short, long)]
    pub dates: bool,

    /// control how many pages of notifications should be shown
    #[arg(short, long, default_value_t = 1)]
    pub page: usize,

    /// control how many notifications each page should hold
    #[arg(short, long, default_value_t = usize::MAX)]
    pub limit: usize,
}

impl ListNotificationArgs {
    pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
        let ctx = BergContext::new(self, global_args).await?;

        let (_, notification_threads_list) = ctx
            .client
            .notify_get_list(NotifyGetListQuery {
                ..Default::default()
            })
            .await
            .into_diagnostic()?;

        match ctx.global_args.output_mode {
            OutputMode::Pretty => {
                tracing::debug!("{notification_threads_list:?}");
                present_notification_threads(&ctx, notification_threads_list);
            }
            OutputMode::Json => notification_threads_list.print_json()?,
        }

        Ok(())
    }
}

fn present_notification_threads(
    ctx: &BergContext<ListNotificationArgs>,
    notification_threads_list: Vec<NotificationThread>,
) {
    let header = if notification_threads_list.is_empty() {
        "Notification Threads (empty)"
    } else {
        "Notification Threads"
    };

    let table = ctx.make_table().set_header(vec![header]).add_rows(
        notification_threads_list
            .into_iter()
            .map(|notification_thread| {
                vec![option_display(
                    &notification_thread
                        .subject
                        .as_ref()
                        .and_then(|subject| subject.title.as_ref()),
                )]
            }),
    );

    println!("{table}", table = table.show());
}