From 2eb6f73afef465995e1f6d5b0d6db2b9f87f9f38 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Fri, 25 Oct 2024 11:53:38 -0400 Subject: feat: `repo readme` --- src/main.rs | 18 ++++++++++++++++-- src/repo.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 06a9965..495c971 100644 --- a/src/main.rs +++ b/src/main.rs @@ -381,6 +381,21 @@ impl SpecialRender { } } +fn max_line_length() -> usize { + let (terminal_width, _) = crossterm::terminal::size().unwrap_or((80, 24)); + (terminal_width as usize - 2).min(80) +} + +fn render_text(text: &str) -> String { + let mut ansi_printer = AnsiPrinter::new(max_line_length()); + + ansi_printer.pause_style(); + ansi_printer.prefix(); + ansi_printer.resume_style(); + ansi_printer.text(text); + ansi_printer.out +} + fn markdown(text: &str) -> String { let SpecialRender { fancy, @@ -427,8 +442,7 @@ fn markdown(text: &str) -> String { let mut list_numbers = Vec::new(); - let (terminal_width, _) = crossterm::terminal::size().unwrap_or((80, 24)); - let max_line_len = (terminal_width as usize - 2).min(80); + let max_line_len = max_line_length(); let mut links = Vec::new(); diff --git a/src/repo.rs b/src/repo.rs index a28e843..3780642 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -368,6 +368,13 @@ pub enum RepoCommand { #[clap(long, short = 'R')] remote: Option, }, + /// View a repo's README + Readme { + #[clap(id = "[HOST/]OWNER/REPO")] + name: Option, + #[clap(long, short = 'R')] + remote: Option, + }, /// Clone a repo's code locally Clone { #[clap(id = "[HOST/]OWNER/REPO")] @@ -474,6 +481,15 @@ impl RepoCommand { .ok_or_eyre("couldn't get repo name, please specify")?; view_repo(&api, repo).await? } + RepoCommand::Readme { name, remote } => { + let repo = + RepoInfo::get_current(host_name, name.as_ref(), remote.as_deref(), &keys)?; + let api = keys.get_api(repo.host_url()).await?; + let repo = repo + .name() + .ok_or_eyre("couldn't get repo name, please specify")?; + view_repo_readme(&api, repo).await? + } RepoCommand::Clone { repo, path } => { let repo = RepoInfo::get_current(host_name, Some(&repo), None, &keys)?; let api = keys.get_api(repo.host_url()).await?; @@ -921,6 +937,29 @@ async fn view_repo(api: &Forgejo, repo: &RepoName) -> eyre::Result<()> { Ok(()) } +async fn view_repo_readme(api: &Forgejo, repo: &RepoName) -> eyre::Result<()> { + let query = forgejo_api::structs::RepoGetRawFileQuery { r#ref: None }; + let file = api + .repo_get_raw_file(repo.owner(), repo.name(), "README.md", query) + .await; + if let Ok(readme) = file { + let readme_str = String::from_utf8_lossy(&readme); + println!("{}", crate::markdown(&readme_str)); + return Ok(()); + } else { + let query = forgejo_api::structs::RepoGetRawFileQuery { r#ref: None }; + let file = api + .repo_get_raw_file(repo.owner(), repo.name(), "README.txt", query) + .await; + if let Ok(readme) = file { + let readme_str = String::from_utf8_lossy(&readme); + println!("{}", crate::render_text(&readme_str)); + return Ok(()); + } + } + eyre::bail!("Repo does not have README.md or README.txt"); +} + async fn cmd_clone_repo( api: &Forgejo, name: &RepoName, -- cgit v1.2.3