use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Activity {
Busy,
Waiting,
Working,
NeedsDecision,
Blocked,
Paused,
Done,
Failed,
Interactive,
#[default]
Unknown,
}
impl Activity {
pub fn glyph(self) -> &'static str {
match self {
Activity::Busy => "\u{1F528}", Activity::Waiting => "\u{1F4A4}", Activity::Working => "\u{1F6A7}", Activity::NeedsDecision => "\u{2753}", Activity::Blocked => "\u{1F6D1}", Activity::Paused => "\u{23F3}", Activity::Done => "\u{2705}", Activity::Failed => "\u{274C}", Activity::Interactive => "\u{1F464}", Activity::Unknown => "\u{1F916}", }
}
pub fn once_label(self) -> &'static str {
match self {
Activity::Busy => "busy",
Activity::Waiting => "wait",
Activity::Working => "work",
Activity::NeedsDecision => "ask",
Activity::Blocked => "blocked",
Activity::Paused => "paused",
Activity::Done => "done",
Activity::Failed => "failed",
Activity::Interactive => "human",
Activity::Unknown => "-",
}
}
pub fn ascii(self) -> &'static str {
match self {
Activity::Busy => "*",
Activity::Waiting => "z",
Activity::Working => "w",
Activity::NeedsDecision => "?",
Activity::Blocked => "!",
Activity::Paused => "~",
Activity::Done => "+",
Activity::Failed => "x",
Activity::Interactive => "@",
Activity::Unknown => ".",
}
}
}
pub fn fit_state(activity: Activity, width: usize) -> String {
match width {
0 => String::new(),
1 => activity.ascii().to_string(),
_ => activity.glyph().to_string(),
}
}
pub fn parse_status_verb(content: &str) -> Option<String> {
let line = content.lines().rev().find(|l| !l.trim().is_empty())?;
let head = line.split(':').next()?;
let verb = head.split_whitespace().next()?;
Some(verb.to_string())
}
pub fn parse_busy_state(record: &str, armed_gen: Option<&str>) -> Option<bool> {
let armed = armed_gen?.trim();
if armed.is_empty() {
return None;
}
let mut lines = record.lines().filter(|l| !l.trim().is_empty());
let line = lines.next()?;
if lines.next().is_some() {
return None; }
let mut fields = line.split_whitespace();
if fields.next() != Some("v1") {
return None;
}
let mut gen = None;
let mut state = None;
for f in fields {
if let Some(v) = f.strip_prefix("gen=") {
gen = Some(v);
} else if let Some(v) = f.strip_prefix("state=") {
state = Some(v);
}
}
if gen? != armed {
return None;
}
match state? {
"busy" => Some(true),
"idle" => Some(false),
_ => None,
}
}
pub fn classify(verb: Option<&str>, busy: Option<bool>) -> Activity {
match verb {
Some("failed") => Activity::Failed,
Some("done") => Activity::Done,
Some("blocked") => Activity::Blocked,
Some("needs-decision") => Activity::NeedsDecision,
Some("paused") => Activity::Paused,
_ => match busy {
Some(true) => Activity::Busy,
Some(false) => Activity::Waiting,
None => Activity::Working,
},
}
}
pub fn load_activity(fm_home: &Path, stem: &str) -> Activity {
let state = fm_home.join("state");
let status = fs::read_to_string(state.join(format!("{stem}.status"))).ok();
let verb = status.as_deref().and_then(parse_status_verb);
let record = fs::read_to_string(state.join(format!("{stem}.busy-state"))).ok();
let gen = fs::read_to_string(state.join(format!("{stem}.busy-gen"))).ok();
let busy = match (record.as_deref(), gen.as_deref()) {
(Some(r), g) => parse_busy_state(r, g.map(str::trim)),
_ => None,
};
classify(verb.as_deref(), busy)
}
#[cfg(test)]
mod tests {
use super::*;
const GEN: &str = "g1786790701.2685732.1306";
fn record(state: &str) -> String {
format!("v1 gen={GEN} seq=2 state={state} source=claude-hook event=stop ts=1786790704\n")
}
#[test]
fn glyphs_are_single_wide_scalars() {
let all = [
Activity::Busy,
Activity::Waiting,
Activity::Working,
Activity::NeedsDecision,
Activity::Blocked,
Activity::Paused,
Activity::Done,
Activity::Failed,
Activity::Interactive,
Activity::Unknown,
];
for a in all {
let g = a.glyph();
assert_eq!(g.chars().count(), 1, "{a:?} glyph must be one scalar");
let c = g.chars().next().unwrap();
assert!(
!matches!(c, '\u{FE0F}' | '\u{200D}'),
"{a:?} glyph must not be a presentation/joiner char"
);
assert_eq!(a.ascii().chars().count(), 1);
assert!(a.ascii().is_ascii());
}
}
#[test]
fn fit_state_degrades_emoji_ascii_empty() {
assert_eq!(fit_state(Activity::Busy, 2), "🔨");
assert_eq!(fit_state(Activity::Busy, 5), "🔨");
assert_eq!(fit_state(Activity::Busy, 1), "*");
assert_eq!(fit_state(Activity::Busy, 0), "");
}
#[test]
fn status_verb_takes_last_line() {
let log = "working: setting up\nworking: building\ndone: all green\n";
assert_eq!(parse_status_verb(log).as_deref(), Some("done"));
}
#[test]
fn status_verb_strips_decision_key() {
let log = "working: x\nneeds-decision [key=api-shape]: REST or RPC?\n";
assert_eq!(parse_status_verb(log).as_deref(), Some("needs-decision"));
}
#[test]
fn status_verb_empty_is_none() {
assert_eq!(parse_status_verb(""), None);
assert_eq!(parse_status_verb("\n\n"), None);
}
#[test]
fn busy_record_matching_gen_parses() {
assert_eq!(parse_busy_state(&record("busy"), Some(GEN)), Some(true));
assert_eq!(parse_busy_state(&record("idle"), Some(GEN)), Some(false));
}
#[test]
fn busy_record_unknown_state_is_none() {
assert_eq!(parse_busy_state(&record("unknown"), Some(GEN)), None);
assert_eq!(parse_busy_state(&record("bogus"), Some(GEN)), None);
}
#[test]
fn busy_record_stale_gen_is_none() {
assert_eq!(parse_busy_state(&record("busy"), Some("g999.1.1")), None);
}
#[test]
fn busy_record_missing_gen_or_bad_version_is_none() {
assert_eq!(parse_busy_state(&record("busy"), None), None);
assert_eq!(parse_busy_state(&record("busy"), Some("")), None);
let v2 = record("busy").replace("v1 ", "v2 ");
assert_eq!(parse_busy_state(&v2, Some(GEN)), None);
}
#[test]
fn busy_record_multiline_or_garbage_is_none() {
let two = format!("{}{}", record("busy"), record("busy"));
assert_eq!(parse_busy_state(&two, Some(GEN)), None);
assert_eq!(parse_busy_state("not a record", Some(GEN)), None);
}
#[test]
fn lifecycle_beats_turn_state() {
assert_eq!(classify(Some("done"), Some(true)), Activity::Done);
assert_eq!(classify(Some("failed"), Some(false)), Activity::Failed);
assert_eq!(classify(Some("blocked"), Some(true)), Activity::Blocked);
assert_eq!(
classify(Some("needs-decision"), Some(true)),
Activity::NeedsDecision
);
assert_eq!(classify(Some("paused"), Some(true)), Activity::Paused);
}
#[test]
fn working_splits_on_turn_state() {
assert_eq!(classify(Some("working"), Some(true)), Activity::Busy);
assert_eq!(classify(Some("working"), Some(false)), Activity::Waiting);
assert_eq!(classify(Some("working"), None), Activity::Working);
assert_eq!(classify(Some("resolved"), Some(true)), Activity::Busy);
assert_eq!(classify(None, Some(false)), Activity::Waiting);
assert_eq!(classify(None, None), Activity::Working);
}
}