Skip to content

Commit

Permalink
feat: table of content
Browse files Browse the repository at this point in the history
  • Loading branch information
fromtheeast710 committed Dec 3, 2024
1 parent 4990dd2 commit fdf6d71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct CLI {
#[arg(short, long, default_value_t = false)]
pub preview: bool,

#[arg(short = 'F', long, default_value = "+ **[$name/$owner]($url)** `$star`")]
#[arg(short = 'F', long, default_value = "+ **[{owner}/{name}]({url})** `{star}`")]
pub format: Option<String>,

#[arg(short, long, default_value_t = false)]
Expand Down
35 changes: 25 additions & 10 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Repo {
.send()
.await?;
let repos = ocrab.all_pages(pages).await?;
// let repo = repos.clone().into_iter().next().expect(&DATA_ERR);

Ok(Self { repos })
}
Expand All @@ -42,15 +43,15 @@ impl Repo {
.map_or("N/A".to_string(), |l| l.to_string())
.replace('\"', "")
.replace(" ", "");
let owner = &repo.owner.as_ref().expect(&DATA_ERR).login;
let name = &repo.name;
let star = &repo.stargazers_count.expect(&DATA_ERR);
let url = repo.html_url.as_ref().expect(&DATA_ERR);

// TODO: custom formatting
let form = format!(
r#"+ **[{}/{}]({})** `{}`"#,
repo.owner.as_ref().expect(&DATA_ERR).login,
repo.name,
repo.html_url.as_ref().expect(&DATA_ERR),
repo.stargazers_count.expect(&DATA_ERR),
);
let form = format!(r#"+ **[{owner}/{name}]({url})** `{star}`"#);

// TODO: move N/A to the end
col
.entry(lang)
.and_modify(|c| c.push(form.clone()))
Expand All @@ -60,16 +61,30 @@ impl Repo {
col.into_iter().collect()
}

// TODO: compress into a table
pub fn format_table(&self) -> String {
let langs: Vec<String> = self.iter_repo().keys().cloned().collect();

let mut table = String::new();

table.push_str("## Table of Contents\n");

for (i, lang) in langs.iter().enumerate() {
table.push_str(&format!(" {i:>2}. [{lang}](#{lang})\n"));
if langs.iter().count() > 9 {
for (i, lang) in langs.iter().enumerate() {
if i == 4 {
table.push_str("|\n|-|-|-|-|\n")
}

if i % 4 == 0 && i != 0 && i != 4 {
table.push_str("|\n");
}

table.push_str(&format!("|**{}. [{lang}](#{lang})**", i + 1));
}
table.push_str("\n");
} else {
for (i, lang) in langs.iter().enumerate() {
table.push_str(&format!("**{}. [{lang}](#{lang})**\n", i + 1))
}
}

table
Expand Down

0 comments on commit fdf6d71

Please sign in to comment.