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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use chrono::Utc;
use crossterm::style::Stylize;

use crate::git::truncate_message;
use crate::theme::Theme;
use crate::types::ProjectStatus;

/// Maximum display width for commit messages in the table.
const MESSAGE_MAX_LEN: usize = 50;

/// Print a colored table of project statuses.
pub fn print_table(statuses: &[ProjectStatus], theme: &Theme) {
    if statuses.is_empty() {
        println!("No projects found.");
        return;
    }

    // Calculate column widths
    let name_width = statuses
        .iter()
        .map(|s| s.name.len())
        .max()
        .unwrap_or(7)
        .max(7);
    let branch_width = statuses
        .iter()
        .map(|s| s.branch.len())
        .max()
        .unwrap_or(6)
        .max(6);

    // Check if any project has a non-Unknown CI status
    let show_ci = statuses
        .iter()
        .any(|s| s.ci_status != crate::ci::CiStatus::Unknown);

    // Print header
    let ci_header = if show_ci { "  CI" } else { "" };
    let header = format!(
        "  {:<name_w$}  {:<branch_w$}  {:>8}  {:>7}  {:>14}  {:>12}  {:>5}{ci_h}  {}",
        "Project",
        "Branch",
        "Status",
        "Changed",
        "Last Commit",
        "Ahead/Behind",
        "Stash",
        "Message",
        name_w = name_width,
        branch_w = branch_width,
        ci_h = ci_header,
    );
    println!("{}", header.bold().with(theme.header.to_crossterm()));
    let ci_width = if show_ci { 4 } else { 0 };
    println!(
        "  {}",
        "".repeat(
            name_width + branch_width + 8 + 7 + 14 + 12 + 5 + ci_width + MESSAGE_MAX_LEN + 20
        )
    );

    let now = Utc::now();

    for s in statuses {
        let status_str = if s.is_clean { "clean" } else { "dirty" };
        let status_colored = if s.is_clean {
            format!("{:>8}", status_str).with(theme.clean.to_crossterm())
        } else {
            format!("{:>8}", status_str).with(theme.dirty.to_crossterm())
        };

        let last_commit_str = match s.last_commit {
            Some(dt) => format_relative_time(now, dt),
            None => "no commits".to_string(),
        };

        let last_commit_colored = match s.last_commit {
            Some(dt) => {
                let days = (now - dt).num_days();
                let text = format!("{:>14}", last_commit_str);
                if days < 7 {
                    text.with(theme.clean.to_crossterm())
                } else if days < 30 {
                    text.with(theme.dirty.to_crossterm())
                } else {
                    text.with(theme.stale.to_crossterm())
                }
            }
            None => format!("{:>14}", last_commit_str).with(theme.dim.to_crossterm()),
        };

        let changed_str = format!("{:>7}", s.changed_files);
        let changed_colored = if s.changed_files == 0 {
            changed_str.with(theme.clean.to_crossterm())
        } else {
            changed_str.with(theme.dirty.to_crossterm())
        };

        let ahead_behind = if s.ahead == 0 && s.behind == 0 {
            "".to_string()
        } else {
            format!("{}{}", s.ahead, s.behind)
        };

        let stash_str = if s.stash_count == 0 {
            "".to_string()
        } else {
            format!("{}", s.stash_count)
        };
        let stash_colored = if s.stash_count == 0 {
            format!("{:>5}", stash_str).with(theme.dim.to_crossterm())
        } else {
            format!("{:>5}", stash_str).with(theme.accent.to_crossterm())
        };

        let message_str = match &s.last_commit_message {
            Some(msg) => truncate_message(msg, MESSAGE_MAX_LEN),
            None => "".to_string(),
        };
        let message_colored = match &s.last_commit_message {
            Some(_) => message_str.with(theme.header.to_crossterm()),
            None => message_str.with(theme.dim.to_crossterm()),
        };

        let ci_col = if show_ci {
            format!("  {}", s.ci_status)
        } else {
            String::new()
        };

        println!(
            "  {:<name_w$}  {:<branch_w$}  {}  {}  {}  {:>12}  {}{ci}  {}",
            s.name,
            s.branch,
            status_colored,
            changed_colored,
            last_commit_colored,
            ahead_behind,
            stash_colored,
            message_colored,
            name_w = name_width,
            branch_w = branch_width,
            ci = ci_col,
        );
    }

    println!();
}

/// Format a plain-text table of project statuses (no ANSI colors).
/// Used when writing table output to a file.
pub fn format_table_plain(statuses: &[ProjectStatus]) -> String {
    use crate::summary::Summary;

    if statuses.is_empty() {
        return "No projects found.\n".to_string();
    }

    let mut out = String::new();

    // Check if any project has a non-Unknown CI status
    let show_ci = statuses
        .iter()
        .any(|s| s.ci_status != crate::ci::CiStatus::Unknown);

    // Calculate column widths
    let name_width = statuses
        .iter()
        .map(|s| s.name.len())
        .max()
        .unwrap_or(7)
        .max(7);
    let branch_width = statuses
        .iter()
        .map(|s| s.branch.len())
        .max()
        .unwrap_or(6)
        .max(6);

    let ci_header = if show_ci { "  CI" } else { "" };
    // Header
    out.push_str(&format!(
        "  {:<name_w$}  {:<branch_w$}  {:>8}  {:>7}  {:>14}  {:>12}  {:>5}{ci_h}  {}\n",
        "Project",
        "Branch",
        "Status",
        "Changed",
        "Last Commit",
        "Ahead/Behind",
        "Stash",
        "Message",
        name_w = name_width,
        branch_w = branch_width,
        ci_h = ci_header,
    ));
    let ci_width = if show_ci { 4 } else { 0 };
    out.push_str(&format!(
        "  {}\n",
        "-".repeat(
            name_width + branch_width + 8 + 7 + 14 + 12 + 5 + ci_width + MESSAGE_MAX_LEN + 20
        )
    ));

    let now = Utc::now();

    for s in statuses {
        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_str = if s.stash_count == 0 {
            "".to_string()
        } else {
            format!("{}", s.stash_count)
        };
        let message_str = match &s.last_commit_message {
            Some(msg) => truncate_message(msg, MESSAGE_MAX_LEN),
            None => "".to_string(),
        };

        let ci_col = if show_ci {
            format!("  {}", s.ci_status)
        } else {
            String::new()
        };

        out.push_str(&format!(
            "  {:<name_w$}  {:<branch_w$}  {:>8}  {:>7}  {:>14}  {:>12}  {:>5}{ci}  {}\n",
            s.name,
            s.branch,
            status_str,
            s.changed_files,
            last_commit_str,
            ahead_behind,
            stash_str,
            message_str,
            name_w = name_width,
            branch_w = branch_width,
            ci = ci_col,
        ));
    }

    out.push('\n');
    let summary = Summary::from_statuses(statuses);
    out.push_str(&format!("  {}\n", summary.to_plain_string()));

    out
}

/// Format a duration between now and a past timestamp as a human-readable string.
fn format_relative_time(now: chrono::DateTime<Utc>, then: chrono::DateTime<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_status(name: &str, is_clean: bool, changed: usize) -> ProjectStatus {
        let now = Utc::now();
        ProjectStatus {
            name: name.to_string(),
            path: PathBuf::from(format!("/tmp/{}", name)),
            branch: "main".to_string(),
            is_clean,
            changed_files: changed,
            last_commit: Some(now - Duration::hours(2)),
            ahead: 0,
            behind: 0,
            remote_url: None,
            stash_count: 0,
            last_commit_message: Some("initial commit".to_string()),
            ci_status: crate::ci::CiStatus::Unknown,
        }
    }

    // --- format_relative_time tests ---

    #[test]
    fn test_relative_time_just_now() {
        let now = Utc::now();
        assert_eq!(format_relative_time(now, now), "just now");
    }

    #[test]
    fn test_relative_time_seconds_ago() {
        let now = Utc::now();
        let then = now - Duration::seconds(30);
        assert_eq!(format_relative_time(now, then), "just now");
    }

    #[test]
    fn test_relative_time_minutes_ago() {
        let now = Utc::now();
        let then = now - Duration::minutes(5);
        assert_eq!(format_relative_time(now, then), "5m ago");
    }

    #[test]
    fn test_relative_time_59_minutes() {
        let now = Utc::now();
        let then = now - Duration::minutes(59);
        assert_eq!(format_relative_time(now, then), "59m ago");
    }

    #[test]
    fn test_relative_time_hours_ago() {
        let now = Utc::now();
        let then = now - Duration::hours(3);
        assert_eq!(format_relative_time(now, then), "3h ago");
    }

    #[test]
    fn test_relative_time_23_hours() {
        let now = Utc::now();
        let then = now - Duration::hours(23);
        assert_eq!(format_relative_time(now, then), "23h ago");
    }

    #[test]
    fn test_relative_time_days_ago() {
        let now = Utc::now();
        let then = now - Duration::days(5);
        assert_eq!(format_relative_time(now, then), "5d ago");
    }

    #[test]
    fn test_relative_time_29_days() {
        let now = Utc::now();
        let then = now - Duration::days(29);
        assert_eq!(format_relative_time(now, then), "29d ago");
    }

    #[test]
    fn test_relative_time_months_ago() {
        let now = Utc::now();
        let then = now - Duration::days(60);
        assert_eq!(format_relative_time(now, then), "2mo ago");
    }

    #[test]
    fn test_relative_time_11_months() {
        let now = Utc::now();
        let then = now - Duration::days(330);
        assert_eq!(format_relative_time(now, then), "11mo ago");
    }

    #[test]
    fn test_relative_time_years_ago() {
        let now = Utc::now();
        let then = now - Duration::days(400);
        assert_eq!(format_relative_time(now, then), "1y ago");
    }

    #[test]
    fn test_relative_time_multiple_years() {
        let now = Utc::now();
        let then = now - Duration::days(900);
        assert_eq!(format_relative_time(now, then), "2y ago");
    }

    // --- format_table_plain tests ---

    #[test]
    fn test_plain_table_empty() {
        let result = format_table_plain(&[]);
        assert_eq!(result, "No projects found.\n");
    }

    #[test]
    fn test_plain_table_single_clean_project() {
        let status = make_status("myapp", true, 0);
        let result = format_table_plain(&[status]);
        assert!(result.contains("myapp"));
        assert!(result.contains("clean"));
        assert!(result.contains("main"));
        assert!(result.contains("initial commit"));
        // Should have summary line
        assert!(result.contains("1 project"));
    }

    #[test]
    fn test_plain_table_single_dirty_project() {
        let status = make_status("myapp", false, 3);
        let result = format_table_plain(&[status]);
        assert!(result.contains("dirty"));
        assert!(result.contains("3"));
    }

    #[test]
    fn test_plain_table_multiple_projects() {
        let s1 = make_status("alpha", true, 0);
        let s2 = make_status("beta", false, 5);
        let result = format_table_plain(&[s1, s2]);
        assert!(result.contains("alpha"));
        assert!(result.contains("beta"));
        assert!(result.contains("2 projects"));
    }

    #[test]
    fn test_plain_table_contains_header() {
        let status = make_status("test", true, 0);
        let result = format_table_plain(&[status]);
        assert!(result.contains("Project"));
        assert!(result.contains("Branch"));
        assert!(result.contains("Status"));
        assert!(result.contains("Changed"));
        assert!(result.contains("Last Commit"));
        assert!(result.contains("Ahead/Behind"));
        assert!(result.contains("Stash"));
        assert!(result.contains("Message"));
    }

    #[test]
    fn test_plain_table_ahead_behind() {
        let mut status = make_status("myapp", true, 0);
        status.ahead = 3;
        status.behind = 1;
        let result = format_table_plain(&[status]);
        assert!(result.contains("↑3 ↓1"));
    }

    #[test]
    fn test_plain_table_no_ahead_behind_shows_dash() {
        let status = make_status("myapp", true, 0);
        let result = format_table_plain(&[status]);
        assert!(result.contains(""));
    }

    #[test]
    fn test_plain_table_stash_count() {
        let mut status = make_status("myapp", true, 0);
        status.stash_count = 4;
        let result = format_table_plain(&[status]);
        assert!(result.contains("4"));
    }

    #[test]
    fn test_plain_table_no_last_commit() {
        let mut status = make_status("myapp", true, 0);
        status.last_commit = None;
        status.last_commit_message = None;
        let result = format_table_plain(&[status]);
        assert!(result.contains("no commits"));
    }

    #[test]
    fn test_plain_table_long_project_name_widens_column() {
        let status = make_status("a-very-long-project-name", true, 0);
        let result = format_table_plain(&[status]);
        // The full name should appear, not truncated
        assert!(result.contains("a-very-long-project-name"));
    }

    #[test]
    fn test_relative_time_boundary_60_minutes() {
        let now = Utc::now();
        let then = now - Duration::minutes(60);
        // 60 minutes = 1 hour
        assert_eq!(format_relative_time(now, then), "1h ago");
    }

    #[test]
    fn test_relative_time_boundary_24_hours() {
        let now = Utc::now();
        let then = now - Duration::hours(24);
        // 24 hours = 1 day
        assert_eq!(format_relative_time(now, then), "1d ago");
    }

    #[test]
    fn test_relative_time_boundary_30_days() {
        let now = Utc::now();
        let then = now - Duration::days(30);
        // 30 days = 1 month
        assert_eq!(format_relative_time(now, then), "1mo ago");
    }

    #[test]
    fn test_relative_time_boundary_365_days() {
        let now = Utc::now();
        let then = now - Duration::days(365);
        // 365 days = 1 year
        assert_eq!(format_relative_time(now, then), "1y ago");
    }
}