Skip to main content

stynx_code_commands/infrastructure/handlers/
diff.rs

1use crate::domain::CommandResult;
2
3pub async fn handle_diff() -> CommandResult {
4    let mut output = String::new();
5
6    match tokio::process::Command::new("git")
7        .args(["diff"])
8        .output()
9        .await
10    {
11        Ok(result) => {
12            let stdout = String::from_utf8_lossy(&result.stdout);
13            if !stdout.is_empty() {
14                output.push_str("  \x1b[1mUnstaged changes:\x1b[0m\n");
15                for line in stdout.lines().take(50) {
16                    output.push_str(&format!("  \x1b[2m{line}\x1b[0m\n"));
17                }
18                if stdout.lines().count() > 50 {
19                    output.push_str("  \x1b[2m... (truncated)\x1b[0m\n");
20                }
21            }
22        }
23        Err(e) => {
24            output.push_str(&format!("  \x1b[31m✗ git diff failed: {e}\x1b[0m\n"));
25        }
26    }
27
28    match tokio::process::Command::new("git")
29        .args(["diff", "--cached"])
30        .output()
31        .await
32    {
33        Ok(result) => {
34            let stdout = String::from_utf8_lossy(&result.stdout);
35            if !stdout.is_empty() {
36                if !output.is_empty() {
37                    output.push('\n');
38                }
39                output.push_str("  \x1b[1mStaged changes:\x1b[0m\n");
40                for line in stdout.lines().take(50) {
41                    output.push_str(&format!("  \x1b[2m{line}\x1b[0m\n"));
42                }
43                if stdout.lines().count() > 50 {
44                    output.push_str("  \x1b[2m... (truncated)\x1b[0m\n");
45                }
46            }
47        }
48        Err(e) => {
49            output.push_str(&format!("  \x1b[31m✗ git diff --cached failed: {e}\x1b[0m\n"));
50        }
51    }
52
53    if output.is_empty() {
54        output = "  \x1b[2mNo changes detected.\x1b[0m".into();
55    }
56
57    CommandResult::Output(output)
58}