Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/commands/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ pub fn list() -> anyhow::Result<()> {
if templates.is_empty() {
println!("No issue templates found.");
} else {
for template in templates {
println!("{}", template);
for (name, description_opt) in templates {
match description_opt {
Some(description) => println!("> {:<12} {}", name, description),
None => println!("{}", name),
}
}
}
Ok(())
Expand Down
39 changes: 39 additions & 0 deletions src/utils/get_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// extracts first line comment from a file content based on its extension
pub fn extract_comment(content: &str, ext: &str) -> Option<String> {
match ext {
"rs" | "c" | "cpp" | "js" | "ts" | "java" => {
// Handle block comments /* ... */
if let Some(start) = content.find("/*") {
if let Some(end) = content.find("*/") {
let comment = &content[start + 2..end];
return Some(comment.trim().to_string());
}
}
// Handle line comments //
if let Some(start) = content.find("//") {
let comment = &content[start + 2..];
return Some(comment.trim().to_string());
}
None
}
"yml" | "py" | "sh" | "rb" => {
// Handle line comments #
if let Some(start) = content.find('#') {
let comment = &content[start + 1..];
return Some(comment.trim().to_string());
}
None
}
"md" | "html" => {
// Handle HTML comments <!-- ... -->
if let Some(start) = content.find("<!--") {
if let Some(end) = content.find("-->") {
let comment = &content[start + 4..end];
return Some(comment.trim().to_string());
}
}
None
}
_ => None,
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod get_comment;
pub mod pretty_print;
pub mod remote;
33 changes: 28 additions & 5 deletions src/utils/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::Path;

use crate::utils::get_comment;
use crate::utils::pretty_print;

const REMOTE_BASE_URL: &str =
Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn fetch_template(
Ok(())
}

pub fn fetch_template_list(category: &str) -> anyhow::Result<Vec<String>> {
pub fn fetch_template_list(category: &str) -> anyhow::Result<Vec<(String, Option<String>)>> {
let url = format!("{}/{}-templates", REMOTE_API_URL, category);

let client = reqwest::blocking::Client::new();
Expand Down Expand Up @@ -87,11 +88,33 @@ pub fn fetch_template_list(category: &str) -> anyhow::Result<Vec<String>> {
for entry in array {
if let Some(name) = entry.get("name").and_then(|n| n.as_str()) {
// Remove the extension if present
let name_without_ext = match name.rfind('.') {
Some(idx) => &name[..idx],
None => name,
let (name_without_ext, extension) = match name.rfind('.') {
Some(idx) => (&name[..idx], &name[idx + 1..]),
None => (name, ""),
};
templates.push(name_without_ext.to_string());

// Fetch the template file to read the first line (comment)
let file_url = format!(
"{}/{}-templates/{}.{}",
REMOTE_BASE_URL, category, name_without_ext, extension
);

let comment = match reqwest::blocking::get(&file_url) {
Ok(response) if response.status().is_success() => {
if let Ok(text) = response.text() {
if let Some(first_line) = text.lines().next() {
Some(get_comment::extract_comment(first_line, extension))
} else {
None
}
} else {
None
}
}
_ => None,
};

templates.push((name_without_ext.to_string(), comment.flatten()));
}
}
}
Expand Down
Loading