aboutsummaryrefslogtreecommitdiff
path: root/src/detect/agents/cursor.rs
blob: a62e9382cd3d836b1c8e7b8e42515276f91b5a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::super::AgentState;

pub(super) fn detect(content: &str) -> AgentState {
    let lower = content.to_lowercase();

    // Blocked
    if has_cursor_blocked_prompt(content, &lower) {
        return AgentState::Blocked;
    }

    // Working
    if lower.contains("ctrl+c to stop") {
        return AgentState::Working;
    }
    if has_cursor_spinner(content) {
        return AgentState::Working;
    }

    AgentState::Idle
}

fn has_cursor_blocked_prompt(content: &str, lower: &str) -> bool {
    if lower.contains("waiting for approval") || lower.contains("run this command?") {
        return true;
    }

    if lower.contains("(y) (enter)")
        || lower.contains("keep (n)")
        || lower.contains("skip (esc or n)")
    {
        return true;
    }

    content.lines().any(|line| {
        let line = line.trim().to_lowercase();
        let has_yes_action = line.contains("(y)");
        has_yes_action
            && (line.contains("allow")
                || line.contains("run (once)")
                || line.contains("→ run")
                || line.starts_with("run "))
    })
}

/// Cursor status line: spinner glyphs followed by a live action label.
pub(in crate::detect) fn has_cursor_spinner(content: &str) -> bool {
    content.lines().any(|line| {
        let trimmed = line.trim_start();
        let mut chars = trimmed.chars();
        let Some(first) = chars.next() else {
            return false;
        };
        let rest = chars.as_str().trim_start();

        if matches!(first, '⬡' | '⬢') {
            return cursor_status_word_is_active(rest);
        }

        if ('\u{2800}'..='\u{28FF}').contains(&first) {
            let rest = rest.trim_start_matches(|c| ('\u{2800}'..='\u{28FF}').contains(&c));
            return cursor_status_word_is_active(rest.trim_start());
        }

        false
    })
}

fn cursor_status_word_is_active(rest: &str) -> bool {
    let Some(word) = rest.split_whitespace().next() else {
        return false;
    };
    word.trim_end_matches(|c: char| !c.is_alphabetic())
        .to_ascii_lowercase()
        .ends_with("ing")
}