summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2024-10-25 17:53:38 +0200
committerCyborus <cyborus@cyborus.xyz>2024-10-25 17:54:06 +0200
commit2eb6f73afef465995e1f6d5b0d6db2b9f87f9f38 (patch)
tree5dd175e4672e6ff70f77b27da3fc81a6066da7e1 /src
parentMerge pull request 'add `repo migrate`' (#133) from migrate into main (diff)
downloadforgejo-cli-2eb6f73afef465995e1f6d5b0d6db2b9f87f9f38.tar.xz
forgejo-cli-2eb6f73afef465995e1f6d5b0d6db2b9f87f9f38.zip
feat: `repo readme`
Diffstat (limited to 'src')
-rw-r--r--src/main.rs18
-rw-r--r--src/repo.rs39
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<String>,
},
+ /// View a repo's README
+ Readme {
+ #[clap(id = "[HOST/]OWNER/REPO")]
+ name: Option<RepoArg>,
+ #[clap(long, short = 'R')]
+ remote: Option<String>,
+ },
/// 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,