1.6 KiB · text History 1294137
//! Wiki pages. The API hands back both markdown and rendered HTML; a
//! terminal wants the markdown, so that's what prints.

use anyhow::Result;

use crate::cli::WikiCommand;
use crate::commands::Ctx;
use crate::output::{Table, bytes, heading, note};

pub async fn run(ctx: &Ctx, command: &WikiCommand) -> Result<()> {
    let repo = ctx.repo()?;

    match command {
        WikiCommand::List => {
            let res = ctx.api.wiki(&repo).await?;
            if ctx.emitted(&res.raw)? {
                return Ok(());
            }
            if !res.initialized {
                note(&format!("{repo} has no wiki yet."));
                return Ok(());
            }
            if res.pages.is_empty() {
                note("The wiki is initialized but has no pages.");
                return Ok(());
            }

            let mut table = Table::new(["SLUG", "TITLE", "SIZE"]);
            for page in &res.pages {
                table.row([
                    page.slug.clone(),
                    page.title.clone().unwrap_or_default(),
                    page.size.map(bytes).unwrap_or_else(|| "—".into()),
                ]);
            }
            table.print();
            Ok(())
        }

        WikiCommand::Show { slug } => {
            let res = ctx.api.wiki_page(&repo, slug).await?;
            if ctx.emitted(&res.raw)? {
                return Ok(());
            }
            heading(res.title.as_deref().unwrap_or(&res.slug));
            println!();
            println!("{}", res.body.as_deref().unwrap_or("(empty page)"));
            Ok(())
        }
    }
}