From 19e1b9cc8e4b2582c8bf2dc33772ad058fa1ee61 Mon Sep 17 00:00:00 2001 From: rafaeljohn9 Date: Mon, 30 Jun 2025 13:14:50 +0300 Subject: [PATCH] feat: update remote:list to fetch with comments Signed-off-by: rafaeljohn9 --- src/commands/issue.rs | 7 +++++-- src/utils/get_comment.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + src/utils/remote.rs | 33 ++++++++++++++++++++++++++++----- 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/utils/get_comment.rs diff --git a/src/commands/issue.rs b/src/commands/issue.rs index 4350d76..14a33a0 100644 --- a/src/commands/issue.rs +++ b/src/commands/issue.rs @@ -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(()) diff --git a/src/utils/get_comment.rs b/src/utils/get_comment.rs new file mode 100644 index 0000000..2a59cc7 --- /dev/null +++ b/src/utils/get_comment.rs @@ -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 { + 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("") { + let comment = &content[start + 4..end]; + return Some(comment.trim().to_string()); + } + } + None + } + _ => None, + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index baf2589..6bc74bb 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,3 @@ +pub mod get_comment; pub mod pretty_print; pub mod remote; diff --git a/src/utils/remote.rs b/src/utils/remote.rs index aa71167..24769de 100644 --- a/src/utils/remote.rs +++ b/src/utils/remote.rs @@ -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 = @@ -54,7 +55,7 @@ pub fn fetch_template( Ok(()) } -pub fn fetch_template_list(category: &str) -> anyhow::Result> { +pub fn fetch_template_list(category: &str) -> anyhow::Result)>> { let url = format!("{}/{}-templates", REMOTE_API_URL, category); let client = reqwest::blocking::Client::new(); @@ -87,11 +88,33 @@ pub fn fetch_template_list(category: &str) -> anyhow::Result> { 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())); } } }