(101c2444) Add colours to ls command

This commit is contained in:
2026-05-15 02:18:04 +02:00
parent d668a24a3d
commit aef8002d23
3 changed files with 81 additions and 10 deletions
+40 -5
View File
@@ -2,12 +2,36 @@ use anyhow::{Result, anyhow};
use chrono::Local;
use clap::Parser;
use std::fs;
use std::io::IsTerminal;
use std::path::Path;
mod command;
use command::*;
fn should_use_colors() -> bool {
std::io::stdout().is_terminal()
}
fn get_color(color: &str) -> &'static str {
match color {
"red" => "\x1b[31m",
"cyan" => "\x1b[36m",
"orange" => "\x1b[33m",
"green" => "\x1b[32m",
"grey" => "\x1b[90m",
_ => "",
}
}
fn color_text(text: &str, color: &str) -> String {
if should_use_colors() {
format!("{}{}{}", get_color(color), text, "\x1b[0m")
} else {
text.to_string()
}
}
fn find_worklane_dir(start: &Path) -> Result<std::path::PathBuf> {
let mut current = start.canonicalize()?;
@@ -139,12 +163,17 @@ fn cmd_ls(status_filter: &Option<String>, worklane_dir: &Path) -> Result<()> {
println!("Tickets in '{}' status:\n", status);
for (title, owner, due, uuid, ts) in items {
let uuid_colored = color_text(&uuid, "red");
if ts.is_empty() {
println!(" ({}) {} | Owner: {} | Due: {}", uuid, title, owner, due);
println!(
" ({}) {} | Owner: {} | Due: {}",
uuid_colored, title, owner, due
);
} else {
let ts_colored = color_text(&ts, "cyan");
println!(
" {} ({}) {} | Owner: {} | Due: {}",
ts, uuid, title, owner, due
ts_colored, uuid_colored, title, owner, due
);
}
}
@@ -198,14 +227,20 @@ fn cmd_ls(status_filter: &Option<String>, worklane_dir: &Path) -> Result<()> {
})
.collect();
items.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));
println!("\n[{}]", status.to_uppercase());
let status_header = color_text(&format!("[{}]", status.to_uppercase()), "orange");
println!("\n{}", status_header);
for (title, owner, due, uuid, ts) in items {
let uuid_colored = color_text(&uuid, "red");
if ts.is_empty() {
println!(" ({}) {} | Owner: {} | Due: {}", uuid, title, owner, due);
println!(
" ({}) {} | Owner: {} | Due: {}",
uuid_colored, title, owner, due
);
} else {
let ts_colored = color_text(&ts, "cyan");
println!(
" {} ({}) {} | Owner: {} | Due: {}",
ts, uuid, title, owner, due
ts_colored, uuid_colored, title, owner, due
);
}
}