Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7781a830c | ||
|
|
3abe8755c8 | ||
|
|
b6d0b55d4b | ||
|
|
3fcb0ddc0a |
@@ -33,7 +33,7 @@
|
||||
|
||||
### Color and Output
|
||||
- Use `color_text()` helper for colored output — it respects `should_use_colors()` (terminal detection)
|
||||
- Color codes are defined in `get_color()`: red (31), cyan (36), orange (33), green (32), grey (90)
|
||||
- Color codes are defined in `get_color()`: red (31), cyan (36), orange (33), green (32), grey (38;5;8), magenta (1;35)
|
||||
- Reset color with `\x1b[0m` after colored text
|
||||
- Bold uses `\x1b[1;35m` (magenta bold)
|
||||
- Always test color output works correctly in both terminal and pipe modes
|
||||
@@ -43,7 +43,8 @@
|
||||
- Status → orange (33)
|
||||
- Timestamp → cyan (36)
|
||||
- Title → green (32)
|
||||
- Unknown values (`_None_`, `_Not set_`) → grey (90)
|
||||
- Unknown values (`_None_`, `_Not set_`) → grey (38;5;8)
|
||||
- Grey is indexed colour 8 in the 256-colour form, not SGR 90 or 37. Both of those failed on a real palette: 90 leans on the bright-black slot and 37 can sit on top of the default foreground. `\x1b[38;5;8m` is byte for byte what crossterm emits for ratatui's `Color::DarkGray`, the grey frig and pantop use for dimmed rows
|
||||
|
||||
### Code Organization
|
||||
- Colorization functions (`colorize_line`, `colorize_metadata`, `colorize_ordered`, `content_trim_colored`) handle markdown formatting
|
||||
@@ -68,4 +69,6 @@
|
||||
- No bump for docs-only, tests-only or `--fixup` commits
|
||||
- When one body of work spans several commits, bump once on the final commit rather than on each
|
||||
- Bump `Cargo.toml` and the `worklane` entry in `Cargo.lock` together, in the commit that carries the bump
|
||||
- Every bump gets a matching annotated tag, `v<version>`, on the bump commit — a bumped version is never left untagged
|
||||
- Tag locally only; never push a tag unless asked
|
||||
- `worklane --version` reports the crate version alongside the build sha, so the bump is what makes one release distinguishable from another
|
||||
|
||||
Generated
+1
-1
@@ -1029,7 +1029,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "worklane"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "worklane"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
+182
-22
@@ -3,6 +3,7 @@ use chrono::NaiveDateTime;
|
||||
use std::fs;
|
||||
use std::io::IsTerminal;
|
||||
use std::path::Path;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// How timestamps are written into tickets and comments, and therefore how they
|
||||
/// are read back. Shared so the writer and the reader cannot drift apart.
|
||||
@@ -15,8 +16,40 @@ pub const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
|
||||
/// Fixed width, so the lexicographic sort in `read_comments` stays chronological.
|
||||
pub const COMMENT_FILE_FORMAT: &str = "%Y%m%d_%H%M%S_%6f";
|
||||
|
||||
/// Whether colour is wanted, from the three inputs that decide it.
|
||||
///
|
||||
/// Split out from the caching wrapper so it can be tested without a pty and
|
||||
/// without touching the process environment.
|
||||
///
|
||||
/// `NO_COLOR` follows the no-color.org convention: set and non-empty disables,
|
||||
/// whatever the value. An empty value is treated as unset, which is what lets a
|
||||
/// wrapper script clear an inherited one.
|
||||
fn colors_wanted(is_terminal: bool, no_color: Option<&str>, term: Option<&str>) -> bool {
|
||||
if no_color.is_some_and(|value| !value.is_empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if matches!(term, Some("dumb") | Some("")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
is_terminal
|
||||
}
|
||||
|
||||
pub fn should_use_colors() -> bool {
|
||||
std::io::stdout().is_terminal()
|
||||
// Decided once: it cannot change mid-run, and color_text asks on every call.
|
||||
static ENABLED: OnceLock<bool> = OnceLock::new();
|
||||
|
||||
*ENABLED.get_or_init(|| {
|
||||
let no_color = std::env::var("NO_COLOR").ok();
|
||||
let term = std::env::var("TERM").ok();
|
||||
|
||||
colors_wanted(
|
||||
std::io::stdout().is_terminal(),
|
||||
no_color.as_deref(),
|
||||
term.as_deref(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_color(color: &str) -> &'static str {
|
||||
@@ -25,7 +58,15 @@ pub fn get_color(color: &str) -> &'static str {
|
||||
"cyan" => "\x1b[36m",
|
||||
"orange" => "\x1b[33m",
|
||||
"green" => "\x1b[32m",
|
||||
"grey" => "\x1b[90m",
|
||||
// Indexed colour 8 in the 256-colour form, which is byte for byte what
|
||||
// crossterm emits for ratatui's Color::DarkGray, the grey frig and pantop
|
||||
// use for dimmed rows. Neither SGR 90 nor 37 renders as grey on every
|
||||
// palette: 90 leans on the bright-black slot and 37 can sit right on top
|
||||
// of the default foreground.
|
||||
"grey" => "\x1b[38;5;8m",
|
||||
// Bold, per the guideline in AGENTS.md. Its absence meant colorize_line
|
||||
// asked for a colour that did not exist and got an empty prefix.
|
||||
"magenta" => "\x1b[1;35m",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
@@ -346,37 +387,53 @@ pub fn is_unknown_value(value: &str) -> bool {
|
||||
}
|
||||
|
||||
pub fn colorize_line(line: &str) -> String {
|
||||
let mut result = String::new();
|
||||
colorize_bold(line, should_use_colors())
|
||||
}
|
||||
|
||||
/// Replace `**bold**` with colour, or leave the line exactly as it is.
|
||||
///
|
||||
/// Taking `enabled` rather than asking makes both paths testable, and the
|
||||
/// disabled path matters: stripping the markers when there is no colour to
|
||||
/// replace them with loses the emphasis altogether, which is what happens to
|
||||
/// every piped `show` and to everything the opencode tools read.
|
||||
fn colorize_bold(line: &str, enabled: bool) -> String {
|
||||
if !enabled {
|
||||
return line.to_string();
|
||||
}
|
||||
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
let len = chars.len();
|
||||
let mut result = String::new();
|
||||
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(&color_text(&bold_text, "magenta"));
|
||||
i = end + 2;
|
||||
while i < chars.len() {
|
||||
if !is_delimiter(&chars, i) {
|
||||
result.push(chars[i]);
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push(chars[i]);
|
||||
i += 1;
|
||||
match (i + 2..chars.len()).find(|&at| is_delimiter(&chars, at)) {
|
||||
Some(close) => {
|
||||
let bold: String = chars[i + 2..close].iter().collect();
|
||||
result.push_str(&color_text(&bold, "magenta"));
|
||||
i = close + 2;
|
||||
}
|
||||
// Unterminated: the rest is literal. The old scan walked off the end
|
||||
// here and dropped the final character.
|
||||
None => {
|
||||
result.extend(&chars[i..]);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn is_delimiter(chars: &[char], at: usize) -> bool {
|
||||
chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*')
|
||||
}
|
||||
|
||||
pub fn parse_ticket_field(content: &str, field: &str) -> String {
|
||||
let pattern = format!("**{}**:", field);
|
||||
content
|
||||
@@ -441,9 +498,24 @@ pub fn status_dirs() -> Vec<&'static str> {
|
||||
|
||||
pub type TicketItem = (String, String, String, String, String);
|
||||
|
||||
/// Grey for a value that is not set, per the colour guideline.
|
||||
///
|
||||
/// `show` already did this through `colorize_metadata`, so the listing was the
|
||||
/// one place that ignored the rule.
|
||||
fn dim_if_unset(value: &str) -> String {
|
||||
if is_unknown_value(value) {
|
||||
color_text(value, "grey")
|
||||
} else {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display_ticket_items(items: &Vec<TicketItem>) {
|
||||
for (title, owner, due, uuid, ts) in items {
|
||||
let uuid_colored = color_text(uuid, "red");
|
||||
let owner = dim_if_unset(owner);
|
||||
let due = dim_if_unset(due);
|
||||
|
||||
if ts.is_empty() {
|
||||
println!(
|
||||
" ({}) {} | Owner: {} | Due: {}",
|
||||
@@ -495,6 +567,94 @@ pub fn collect_ticket_items(status_path: impl AsRef<Path>) -> Result<Vec<TicketI
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn no_color_disables_colour_on_a_terminal() {
|
||||
assert!(!colors_wanted(true, Some("1"), Some("xterm-256color")));
|
||||
// Any value at all, per the convention
|
||||
assert!(!colors_wanted(true, Some("0"), Some("xterm-256color")));
|
||||
assert!(!colors_wanted(true, Some("false"), Some("xterm-256color")));
|
||||
}
|
||||
|
||||
/// An empty value counts as unset, so a wrapper script can clear one it
|
||||
/// inherited.
|
||||
#[test]
|
||||
fn an_empty_no_color_is_ignored() {
|
||||
assert!(colors_wanted(true, Some(""), Some("xterm-256color")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dumb_terminal_gets_no_colour() {
|
||||
assert!(!colors_wanted(true, None, Some("dumb")));
|
||||
assert!(!colors_wanted(true, None, Some("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colour_needs_a_terminal() {
|
||||
assert!(colors_wanted(true, None, Some("xterm-256color")));
|
||||
assert!(!colors_wanted(false, None, Some("xterm-256color")));
|
||||
// No TERM at all is fine as long as it is a terminal
|
||||
assert!(colors_wanted(true, None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unset_values_are_dimmed_and_set_ones_are_not() {
|
||||
// Colour is off in a test process, so this asserts the decision, not the escape
|
||||
for unset in ["_Not set_", "_None_"] {
|
||||
assert!(is_unknown_value(unset), "{unset} should count as unset");
|
||||
assert_eq!(dim_if_unset(unset), unset);
|
||||
}
|
||||
|
||||
for set in ["alice", "2026-12-31", "bug,frontend"] {
|
||||
assert!(!is_unknown_value(set));
|
||||
assert_eq!(dim_if_unset(set), set);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_colour_the_code_asks_for_exists() {
|
||||
for colour in ["red", "cyan", "orange", "green", "grey", "magenta"] {
|
||||
assert!(!get_color(colour).is_empty(), "{colour} is not defined");
|
||||
}
|
||||
}
|
||||
|
||||
/// Pinned deliberately. Both SGR spellings of grey failed on a real palette,
|
||||
/// 90 reading nearly as light as the foreground and 37 sitting on top of it.
|
||||
/// This is the exact sequence crossterm emits for ratatui's Color::DarkGray,
|
||||
/// so worklane's grey is the one frig and pantop already render.
|
||||
#[test]
|
||||
fn grey_matches_the_indexed_colour_frig_uses() {
|
||||
assert_eq!(get_color("grey"), "\x1b[38;5;8m");
|
||||
}
|
||||
|
||||
/// With no colour to carry the emphasis, the markers are the emphasis.
|
||||
#[test]
|
||||
fn bold_markers_survive_when_colour_is_off() {
|
||||
let line = "Some **bold** text";
|
||||
assert_eq!(colorize_bold(line, false), line);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_markers_are_replaced_when_colour_is_on() {
|
||||
let out = colorize_bold("Some **bold** text", true);
|
||||
assert!(out.contains("bold"), "was {out:?}");
|
||||
assert!(!out.contains("**"), "markers left behind in {out:?}");
|
||||
}
|
||||
|
||||
/// The old scan ran past the end here and dropped the last character.
|
||||
#[test]
|
||||
fn an_unterminated_marker_keeps_the_whole_line() {
|
||||
for line in ["a **bold", "**", "***", "trailing **"] {
|
||||
assert_eq!(colorize_bold(line, true), line, "for {line:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn several_bold_runs_on_one_line_all_survive() {
|
||||
let out = colorize_bold("**one** and **two** and rest", true);
|
||||
assert!(out.contains("one") && out.contains("two") && out.ends_with("and rest"));
|
||||
assert!(!out.contains("**"), "was {out:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_id_shortens_a_full_uuid() {
|
||||
assert_eq!(short_id("c230d2c6-d78a-44a5-8f5f-88fb28f84493"), "c230d2c");
|
||||
|
||||
Reference in New Issue
Block a user