(523c9fe) Split monolithic main.rs file
This commit is contained in:
+401
@@ -0,0 +1,401 @@
|
||||
use anyhow::{Result, anyhow};
|
||||
use std::fs;
|
||||
use std::io::IsTerminal;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn should_use_colors() -> bool {
|
||||
std::io::stdout().is_terminal()
|
||||
}
|
||||
|
||||
pub fn get_color(color: &str) -> &'static str {
|
||||
match color {
|
||||
"red" => "\x1b[31m",
|
||||
"cyan" => "\x1b[36m",
|
||||
"orange" => "\x1b[33m",
|
||||
"green" => "\x1b[32m",
|
||||
"grey" => "\x1b[90m",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn color_text(text: &str, color: &str) -> String {
|
||||
if should_use_colors() {
|
||||
format!("{}{}{}", get_color(color), text, "\x1b[0m")
|
||||
} else {
|
||||
text.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_worklane_dir(start: &Path) -> Result<std::path::PathBuf> {
|
||||
let mut current = start.canonicalize()?;
|
||||
|
||||
loop {
|
||||
let worklane_dir = current.join(".worklane");
|
||||
if worklane_dir.exists() {
|
||||
return Ok(worklane_dir);
|
||||
}
|
||||
|
||||
if !current.pop() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Err(anyhow!(
|
||||
"No .worklane directory found. Run `worklane init` first."
|
||||
))
|
||||
}
|
||||
|
||||
pub fn slug(title: &str) -> String {
|
||||
title
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_')
|
||||
.map(|c| if c == ' ' { '-' } else { c })
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_git_user() -> Option<String> {
|
||||
std::process::Command::new("git")
|
||||
.args(&["config", "user.name"])
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|out| {
|
||||
if out.status.success() {
|
||||
String::from_utf8(out.stdout)
|
||||
.ok()
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn resolve_ticket(search: &str, worklane_dir: &Path) -> Result<(String, String, String)> {
|
||||
let statuses = status_dirs();
|
||||
let mut matches: Vec<(String, String, String)> = Vec::new();
|
||||
|
||||
for status in &statuses {
|
||||
let status_path = worklane_dir.join(status);
|
||||
if !status_path.exists() {
|
||||
continue;
|
||||
}
|
||||
let entries = fs::read_dir(&status_path)?;
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let path = entry.path();
|
||||
if !path.extension().map_or(false, |e| e == "md") {
|
||||
continue;
|
||||
}
|
||||
let content = match fs::read_to_string(&path) {
|
||||
Ok(c) => c,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let title = content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("# "))
|
||||
.map(|l| l[2..].trim())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let filename = path
|
||||
.file_stem()
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_default();
|
||||
let id = filename.split('_').next().unwrap_or("");
|
||||
|
||||
let search_lower = search.to_lowercase();
|
||||
let title_lower = title.to_lowercase();
|
||||
let filename_lower = filename.to_lowercase();
|
||||
|
||||
let id_match = id.to_lowercase().starts_with(&search_lower) && id.len() >= 8;
|
||||
let title_match = title_lower.contains(&search_lower);
|
||||
let filename_match = filename_lower.contains(&search_lower);
|
||||
|
||||
if id_match || title_match || filename_match {
|
||||
matches.push((status.to_string(), title, content));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if matches.is_empty() {
|
||||
return Err(anyhow!("No ticket found matching '{}'", search));
|
||||
}
|
||||
|
||||
if matches.len() > 1 {
|
||||
println!("Multiple tickets found matching '{}':", search);
|
||||
for (i, (status, title, _)) in matches.iter().enumerate() {
|
||||
println!(" {}. {} ({})", i + 1, title, status);
|
||||
}
|
||||
println!("\nTry a more specific search term.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
Ok(matches.remove(0))
|
||||
}
|
||||
pub fn format_comments(comments: &[(String, String, String, String)]) -> String {
|
||||
if comments.is_empty() {
|
||||
return color_text("No comments yet.", "grey");
|
||||
}
|
||||
|
||||
let mut result = String::new();
|
||||
result.push_str("\n");
|
||||
result.push_str(&color_text("COMMENTS", "orange"));
|
||||
result.push_str(&format!("\n{}\n", "─".repeat(20)));
|
||||
|
||||
for (_filename, author, time, body) in comments {
|
||||
result.push_str("\n");
|
||||
result.push_str(&color_text(&format!("From {} ({})", author, time), "cyan"));
|
||||
result.push_str("\n");
|
||||
if !body.is_empty() {
|
||||
result.push_str(&" ".to_string());
|
||||
result.push_str(body);
|
||||
}
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
pub fn content_trim_colored(content: &str) -> String {
|
||||
let lines: Vec<&str> = content.trim().lines().collect();
|
||||
let mut result = String::new();
|
||||
let mut current_section = "";
|
||||
|
||||
for line in &lines {
|
||||
// Track current section
|
||||
if line.starts_with("## ") {
|
||||
current_section = line;
|
||||
let section_name = &line[3..];
|
||||
result.push_str(&format!("\x1b[33m{}\x1b[0m", section_name));
|
||||
result.push('\n');
|
||||
result.push_str(&"─".repeat(line.len()));
|
||||
result.push('\n');
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
// First line is the title header - color it in green
|
||||
if line.starts_with("# ") && current_section.is_empty() {
|
||||
let header = &line[2..]; // Remove "# "
|
||||
result.push_str(&format!("\x1b[32m{}\x1b[0m", header));
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Implementation Plan - color numbers in cyan
|
||||
if current_section == "## Implementation Plan" {
|
||||
result.push_str(&colorize_ordered(line));
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Metadata - color field names in RED
|
||||
if current_section == "## Metadata" {
|
||||
result.push_str(&colorize_metadata(line));
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Acceptance Criteria checkboxes
|
||||
if line.starts_with("- [ ]") {
|
||||
result.push_str("\x1b[90m- [ ]\x1b[0m");
|
||||
result.push_str(&line[5..]);
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Standard markdown link colorization in cyan
|
||||
if line.contains("[") && line.contains("(") && line.contains("]") {
|
||||
if let Some(start) = line.find("[") {
|
||||
let bracket_text = &line[start..];
|
||||
if let Some(end_bracket) = bracket_text.find("](") {
|
||||
let link_start = start + end_bracket + 1;
|
||||
if let Some(end) = line[link_start..].find(']') {
|
||||
let absolute_end = link_start + end + 1;
|
||||
result.push_str("\x1b[36m");
|
||||
result.push_str(&line[start..absolute_end]);
|
||||
result.push_str("\x1b[0m");
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Regular content - colorize **bold** in magenta
|
||||
result.push_str(&colorize_line(line));
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn colorize_ordered(line: &str) -> String {
|
||||
if line.chars().next().map_or(true, |c| !c.is_ascii_digit()) {
|
||||
return line.to_string();
|
||||
}
|
||||
|
||||
// Match "1. " pattern and color the number in cyan
|
||||
let mut result = String::new();
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
|
||||
for (i, c) in chars.iter().enumerate() {
|
||||
if c.is_ascii_digit() {
|
||||
// still scanning digits
|
||||
} else if *c == '.' && i + 1 < chars.len() && chars[i + 1] == ' ' {
|
||||
let number = &line[..i + 1];
|
||||
result.push_str(&format!("\x1b[36m{}\x1b[0m", number));
|
||||
result.push_str(&line[i + 1..]);
|
||||
return result;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
line.to_string()
|
||||
}
|
||||
|
||||
pub fn colorize_metadata(line: &str) -> String {
|
||||
if !line.contains("**") {
|
||||
return line.to_string();
|
||||
}
|
||||
|
||||
let content = line;
|
||||
let open_pos = match content.find("**") {
|
||||
Some(p) => p,
|
||||
None => return content.to_string(),
|
||||
};
|
||||
|
||||
let after_open = open_pos + 2;
|
||||
let remaining = &content[after_open..];
|
||||
let close_pos = match remaining.find("**") {
|
||||
Some(p) => p,
|
||||
None => return content.to_string(),
|
||||
};
|
||||
|
||||
let field_name = &content[after_open..after_open + close_pos];
|
||||
let after_field = &content[after_open + close_pos + 2..];
|
||||
|
||||
format!(
|
||||
"{}{}{}{}",
|
||||
&content[..open_pos],
|
||||
"\x1b[31m",
|
||||
field_name,
|
||||
"\x1b[0m"
|
||||
) + after_field
|
||||
}
|
||||
|
||||
pub fn colorize_line(line: &str) -> String {
|
||||
let mut result = String::new();
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
let len = chars.len();
|
||||
let mut i = 0;
|
||||
|
||||
while i < len {
|
||||
// Check for **bold** markdown
|
||||
if i + 1 < len && chars[i] == '*' && chars[i + 1] == '*' {
|
||||
// Find closing **
|
||||
let start = i + 2;
|
||||
let mut end = start;
|
||||
while end + 1 < len && !(chars[end] == '*' && chars[end + 1] == '*') {
|
||||
if chars[end] == '\n' {
|
||||
break;
|
||||
}
|
||||
end += 1;
|
||||
}
|
||||
|
||||
let bold_text: String = chars[start..end].iter().collect();
|
||||
result.push_str(&format!("\x1b[1;35m{}\x1b[0m", bold_text));
|
||||
i = end + 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push(chars[i]);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn parse_ticket_field(content: &str, field: &str) -> String {
|
||||
let pattern = format!("**{}**:", field);
|
||||
content
|
||||
.lines()
|
||||
.find(|line| line.contains(&pattern))
|
||||
.map(|line| {
|
||||
line.splitn(2, &pattern)
|
||||
.nth(1)
|
||||
.unwrap_or("_Not set_")
|
||||
.trim()
|
||||
.to_string()
|
||||
})
|
||||
.unwrap_or("_Not set_".to_string())
|
||||
}
|
||||
|
||||
// Format created_at for display with proper spacing and formatting.
|
||||
pub fn format_created_at(created_at: &str) -> String {
|
||||
if created_at == "_Not set_" || created_at.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
if created_at.len() < 16 {
|
||||
return created_at.to_string();
|
||||
}
|
||||
// Parse "YYYY-MM-DD HH:MM:SS" -> "YY-MM-DD:HH"
|
||||
let year = &created_at[2..4];
|
||||
let month = &created_at[5..7];
|
||||
let day = &created_at[8..10];
|
||||
let hour = &created_at[11..13];
|
||||
format!("{}-{}-{}:{}", year, month, day, hour)
|
||||
}
|
||||
|
||||
pub fn status_dirs() -> Vec<&'static str> {
|
||||
vec!["backlog", "committed", "reviewing", "done"]
|
||||
}
|
||||
|
||||
pub type TicketItem = (String, String, String, String, String);
|
||||
|
||||
pub fn display_ticket_items(items: &Vec<TicketItem>) {
|
||||
for (title, owner, due, uuid, ts) in items {
|
||||
let uuid_colored = color_text(uuid, "red");
|
||||
if ts.is_empty() {
|
||||
println!(
|
||||
" ({}) {} | Owner: {} | Due: {}",
|
||||
uuid_colored, title, owner, due
|
||||
);
|
||||
} else {
|
||||
let ts_colored = color_text(ts, "cyan");
|
||||
println!(
|
||||
" {} ({}) {} | Owner: {} | Due: {}",
|
||||
ts_colored, uuid_colored, title, owner, due
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collect_ticket_items(status_path: impl AsRef<Path>) -> Result<Vec<TicketItem>> {
|
||||
let entries: Vec<_> = fs::read_dir(status_path)?.filter_map(|e| e.ok()).collect();
|
||||
|
||||
let mut items: Vec<TicketItem> = entries
|
||||
.iter()
|
||||
.filter_map(|e| {
|
||||
let content = fs::read_to_string(e.path()).ok()?;
|
||||
let title = content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("# "))
|
||||
.map(|l| l[2..].trim().to_string())
|
||||
.unwrap_or_else(|| "_No title_".to_string());
|
||||
let owner = parse_ticket_field(&content, "Owner");
|
||||
let due = parse_ticket_field(&content, "Due");
|
||||
let filename = e.path().file_stem()?.to_string_lossy().to_string();
|
||||
let uuid_short = if filename.len() > 7 {
|
||||
&filename[..7]
|
||||
} else {
|
||||
&filename
|
||||
};
|
||||
let created_at = parse_ticket_field(&content, "Created At");
|
||||
let short_date = format_created_at(&created_at);
|
||||
|
||||
Some((title, owner, due, uuid_short.to_string(), short_date))
|
||||
})
|
||||
.collect();
|
||||
|
||||
items.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));
|
||||
Ok(items)
|
||||
}
|
||||
Reference in New Issue
Block a user