dev-pulse 0.1.0

Project health dashboard for your terminal
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::collections::BTreeMap;
use std::path::Path;

use serde::Serialize;

use crate::summary::Summary;
use crate::types::ProjectStatus;

/// A group of projects sharing the same parent directory.
#[derive(Debug, Serialize)]
pub struct ProjectGroup {
    /// Display name for this group (the parent directory path).
    pub label: String,
    /// Projects in this group.
    pub projects: Vec<ProjectStatus>,
    /// Summary statistics for this group.
    pub summary: Summary,
}

/// Group projects by their immediate parent directory.
///
/// Each project's path is inspected to determine its parent directory.
/// Projects sharing the same parent are collected into a single group.
/// Groups are sorted alphabetically by their parent path label.
pub fn group_by_parent(statuses: Vec<ProjectStatus>) -> Vec<ProjectGroup> {
    let mut buckets: BTreeMap<String, Vec<ProjectStatus>> = BTreeMap::new();

    for status in statuses {
        let parent_label = status
            .path
            .parent()
            .map(normalize_label)
            .unwrap_or_else(|| "/".to_string());

        buckets.entry(parent_label).or_default().push(status);
    }

    buckets
        .into_iter()
        .map(|(label, projects)| {
            let summary = Summary::from_statuses(&projects);
            ProjectGroup {
                label,
                projects,
                summary,
            }
        })
        .collect()
}

/// Normalize a parent path into a display label.
/// Replaces the home directory prefix with `~` for readability.
fn normalize_label(path: &Path) -> String {
    let display = path.display().to_string();

    if let Some(home) = home_dir()
        && let Some(rest) = display.strip_prefix(&home)
    {
        if rest.is_empty() {
            return "~".to_string();
        }
        return format!("~{rest}");
    }

    display
}

/// Get the home directory path as a string, if available.
fn home_dir() -> Option<String> {
    std::env::var("HOME")
        .ok()
        .or_else(|| dirs::home_dir().map(|p| p.display().to_string()))
}

/// Format grouped output as JSON.
///
/// Produces: `{"groups": {"path": {"projects": [...], "summary": {...}}}}`
pub fn format_grouped_json(groups: &[ProjectGroup]) -> anyhow::Result<String> {
    let mut group_map = serde_json::Map::new();

    for group in groups {
        let value = serde_json::json!({
            "projects": group.projects,
            "summary": group.summary,
        });
        group_map.insert(group.label.clone(), value);
    }

    let overall_projects: Vec<&ProjectStatus> =
        groups.iter().flat_map(|g| g.projects.iter()).collect();
    let overall_summary = Summary::from_statuses(
        &overall_projects
            .iter()
            .map(|p| (*p).clone())
            .collect::<Vec<_>>(),
    );

    let output = serde_json::json!({
        "groups": group_map,
        "summary": overall_summary,
    });

    serde_json::to_string_pretty(&output).map_err(|e| anyhow::anyhow!("JSON error: {e}"))
}

/// Format grouped output as CSV.
///
/// Adds a "Group" column as the first field.
pub fn format_grouped_csv(groups: &[ProjectGroup]) -> anyhow::Result<String> {
    let mut out = String::new();
    out.push_str("Group,Project,Branch,Status,Changed,Last Commit,Ahead,Behind,Stash,Remote URL,Last Commit Message\n");

    for group in groups {
        for s in &group.projects {
            let status_str = if s.is_clean { "clean" } else { "dirty" };
            let last_commit_str = match s.last_commit {
                Some(dt) => dt.to_rfc3339(),
                None => String::new(),
            };
            let remote = s.remote_url.as_deref().unwrap_or("");
            let message = s.last_commit_message.as_deref().unwrap_or("");

            let row = [
                csv_escape(&group.label),
                csv_escape(&s.name),
                csv_escape(&s.branch),
                status_str.to_string(),
                s.changed_files.to_string(),
                last_commit_str,
                s.ahead.to_string(),
                s.behind.to_string(),
                s.stash_count.to_string(),
                csv_escape(remote),
                csv_escape(message),
            ];
            out.push_str(&row.join(","));
            out.push('\n');
        }
    }

    Ok(out)
}

/// Format grouped output as Markdown with sub-headers per group.
pub fn format_grouped_markdown(groups: &[ProjectGroup]) -> anyhow::Result<String> {
    let mut out = String::new();
    let now = chrono::Utc::now();

    for (i, group) in groups.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        out.push_str(&format!("### {}\n\n", group.label));
        out.push_str(
            "| Project | Branch | Status | Changed | Last Commit | Ahead/Behind | Stash | Message |\n",
        );
        out.push_str(
            "|---------|--------|--------|--------:|-------------|-------------:|------:|---------|\n",
        );

        for s in &group.projects {
            let status_str = if s.is_clean {
                "✅ clean"
            } else {
                "⚠️ dirty"
            };
            let last_commit_str = match s.last_commit {
                Some(dt) => format_relative_time(now, dt),
                None => "no commits".to_string(),
            };
            let ahead_behind = if s.ahead == 0 && s.behind == 0 {
                "".to_string()
            } else {
                format!("{}{}", s.ahead, s.behind)
            };
            let stash = if s.stash_count == 0 {
                "".to_string()
            } else {
                s.stash_count.to_string()
            };
            let message = match &s.last_commit_message {
                Some(msg) => crate::git::truncate_message(msg, 50),
                None => "".to_string(),
            };

            out.push_str(&format!(
                "| {} | {} | {} | {} | {} | {} | {} | {} |\n",
                md_escape(&s.name),
                md_escape(&s.branch),
                status_str,
                s.changed_files,
                last_commit_str,
                ahead_behind,
                stash,
                md_escape(&message),
            ));
        }

        out.push_str(&format!(
            "\n**{}** projects · **{}** dirty · **{}** stale · **{}** unpushed\n",
            group.summary.total, group.summary.dirty, group.summary.stale, group.summary.unpushed,
        ));
    }

    // Overall summary if multiple groups
    if groups.len() > 1 {
        let all_projects: Vec<&ProjectStatus> =
            groups.iter().flat_map(|g| g.projects.iter()).collect();
        let total = all_projects.len();
        let dirty = all_projects.iter().filter(|s| !s.is_clean).count();
        out.push_str(&format!(
            "\n---\n\n**Overall:** {} projects · {} dirty\n",
            total, dirty,
        ));
    }

    Ok(out)
}

/// Escape a CSV field.
fn csv_escape(field: &str) -> String {
    if field.contains(',') || field.contains('"') || field.contains('\n') {
        format!("\"{}\"", field.replace('"', "\"\""))
    } else {
        field.to_string()
    }
}

/// Escape pipe characters for markdown tables.
fn md_escape(s: &str) -> String {
    s.replace('|', "\\|")
}

/// Format relative time (duplicated from export.rs to keep module self-contained).
fn format_relative_time(
    now: chrono::DateTime<chrono::Utc>,
    then: chrono::DateTime<chrono::Utc>,
) -> String {
    let duration = now - then;
    let minutes = duration.num_minutes();
    let hours = duration.num_hours();
    let days = duration.num_days();

    if minutes < 1 {
        "just now".to_string()
    } else if minutes < 60 {
        format!("{}m ago", minutes)
    } else if hours < 24 {
        format!("{}h ago", hours)
    } else if days < 30 {
        format!("{}d ago", days)
    } else if days < 365 {
        format!("{}mo ago", days / 30)
    } else {
        format!("{}y ago", days / 365)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::ProjectStatus;
    use chrono::{Duration, Utc};
    use std::path::PathBuf;

    fn make_project_at(name: &str, parent: &str, is_clean: bool) -> ProjectStatus {
        ProjectStatus {
            name: name.to_string(),
            path: PathBuf::from(parent).join(name),
            branch: "main".to_string(),
            is_clean,
            changed_files: if is_clean { 0 } else { 2 },
            last_commit: Some(Utc::now() - Duration::days(3)),
            ahead: 0,
            behind: 0,
            remote_url: None,
            stash_count: 0,
            last_commit_message: Some("initial commit".to_string()),
            ci_status: crate::ci::CiStatus::Unknown,
        }
    }

    #[test]
    fn test_group_by_parent_single_group() {
        let statuses = vec![
            make_project_at("alpha", "/tmp/projects", true),
            make_project_at("beta", "/tmp/projects", false),
        ];

        let groups = group_by_parent(statuses);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].label, "/tmp/projects");
        assert_eq!(groups[0].projects.len(), 2);
        assert_eq!(groups[0].summary.total, 2);
        assert_eq!(groups[0].summary.dirty, 1);
    }

    #[test]
    fn test_group_by_parent_multiple_groups() {
        let statuses = vec![
            make_project_at("app1", "/home/user/projects", true),
            make_project_at("app2", "/home/user/projects", true),
            make_project_at("lib1", "/home/user/work", false),
        ];

        let groups = group_by_parent(statuses);
        assert_eq!(groups.len(), 2);
        // BTreeMap sorts alphabetically
        assert_eq!(groups[0].label, "/home/user/projects");
        assert_eq!(groups[0].projects.len(), 2);
        assert_eq!(groups[1].label, "/home/user/work");
        assert_eq!(groups[1].projects.len(), 1);
    }

    #[test]
    fn test_group_by_parent_empty_input() {
        let groups = group_by_parent(vec![]);
        assert!(groups.is_empty());
    }

    #[test]
    fn test_group_summary_stats() {
        let statuses = vec![
            make_project_at("clean1", "/tmp/a", true),
            make_project_at("dirty1", "/tmp/a", false),
            make_project_at("dirty2", "/tmp/a", false),
        ];

        let groups = group_by_parent(statuses);
        assert_eq!(groups[0].summary.total, 3);
        assert_eq!(groups[0].summary.dirty, 2);
        assert_eq!(groups[0].summary.clean, 1);
    }

    #[test]
    fn test_grouped_json_structure() {
        let statuses = vec![
            make_project_at("app", "/tmp/projects", true),
            make_project_at("lib", "/tmp/work", false),
        ];

        let groups = group_by_parent(statuses);
        let json_str = format_grouped_json(&groups).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        assert!(parsed["groups"].is_object());
        assert!(parsed["groups"]["/tmp/projects"].is_object());
        assert!(parsed["groups"]["/tmp/work"].is_object());
        assert!(parsed["groups"]["/tmp/projects"]["projects"].is_array());
        assert!(parsed["summary"].is_object());
        assert_eq!(parsed["summary"]["total"], 2);
    }

    #[test]
    fn test_grouped_json_empty() {
        let groups = group_by_parent(vec![]);
        let json_str = format_grouped_json(&groups).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert!(parsed["groups"].as_object().unwrap().is_empty());
        assert_eq!(parsed["summary"]["total"], 0);
    }

    #[test]
    fn test_grouped_csv_has_group_column() {
        let statuses = vec![make_project_at("app", "/tmp/projects", true)];
        let groups = group_by_parent(statuses);
        let csv = format_grouped_csv(&groups).unwrap();
        let lines: Vec<&str> = csv.lines().collect();
        assert!(lines[0].starts_with("Group,"));
        assert!(lines[1].starts_with("/tmp/projects,app,"));
    }

    #[test]
    fn test_grouped_csv_empty() {
        let groups = group_by_parent(vec![]);
        let csv = format_grouped_csv(&groups).unwrap();
        assert_eq!(csv.lines().count(), 1); // Header only
    }

    #[test]
    fn test_grouped_csv_multiple_groups() {
        let statuses = vec![
            make_project_at("a", "/tmp/g1", true),
            make_project_at("b", "/tmp/g2", false),
        ];
        let groups = group_by_parent(statuses);
        let csv = format_grouped_csv(&groups).unwrap();
        let lines: Vec<&str> = csv.lines().collect();
        assert_eq!(lines.len(), 3); // header + 2 data rows
    }

    #[test]
    fn test_grouped_markdown_has_subheaders() {
        let statuses = vec![
            make_project_at("app", "/tmp/projects", true),
            make_project_at("lib", "/tmp/work", false),
        ];
        let groups = group_by_parent(statuses);
        let md = format_grouped_markdown(&groups).unwrap();
        assert!(md.contains("### /tmp/projects"));
        assert!(md.contains("### /tmp/work"));
        assert!(md.contains("| app |"));
        assert!(md.contains("| lib |"));
    }

    #[test]
    fn test_grouped_markdown_overall_summary_with_multiple_groups() {
        let statuses = vec![
            make_project_at("a", "/tmp/g1", true),
            make_project_at("b", "/tmp/g2", false),
        ];
        let groups = group_by_parent(statuses);
        let md = format_grouped_markdown(&groups).unwrap();
        assert!(md.contains("**Overall:**"));
        assert!(md.contains("2 projects"));
    }

    #[test]
    fn test_grouped_markdown_single_group_no_overall() {
        let statuses = vec![
            make_project_at("a", "/tmp/g1", true),
            make_project_at("b", "/tmp/g1", false),
        ];
        let groups = group_by_parent(statuses);
        let md = format_grouped_markdown(&groups).unwrap();
        assert!(!md.contains("**Overall:**"));
    }

    #[test]
    fn test_grouped_markdown_empty() {
        let groups = group_by_parent(vec![]);
        let md = format_grouped_markdown(&groups).unwrap();
        assert!(md.is_empty());
    }

    #[test]
    fn test_csv_escape_in_group() {
        assert_eq!(csv_escape("normal"), "normal");
        assert_eq!(csv_escape("has,comma"), "\"has,comma\"");
        assert_eq!(csv_escape("has\"quote"), "\"has\"\"quote\"");
    }

    #[test]
    fn test_normalize_label_non_home() {
        let path = Path::new("/tmp/projects");
        let label = normalize_label(path);
        assert_eq!(label, "/tmp/projects");
    }

    #[test]
    fn test_normalize_label_uses_tilde() {
        // This test depends on HOME being set, which it normally is
        if let Ok(home) = std::env::var("HOME") {
            let path_str = format!("{}/projects", home);
            let path = Path::new(&path_str);
            let label = normalize_label(path);
            assert_eq!(label, "~/projects");
        }
    }

    #[test]
    fn test_normalize_label_home_itself() {
        if let Ok(home) = std::env::var("HOME") {
            let path = Path::new(&home);
            let label = normalize_label(path);
            assert_eq!(label, "~");
        }
    }
}