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
mod ci;
mod config;
mod export;
mod filter;
mod git;
mod group;
mod scanner;
mod since;
mod summary;
mod table;
pub mod theme;
mod tui;
mod types;
mod watch;

mod completions;

use std::path::PathBuf;

use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};

/// devpulse — Project Health Dashboard for Your Terminal
#[derive(Parser)]
#[command(
    name = "devpulse",
    version,
    about = "Project health dashboard for your terminal",
    long_about = "devpulse scans a directory of projects and displays a terminal dashboard showing \
                   git status, last activity, branch info, and ahead/behind counts for each.",
    after_help = "EXAMPLES:\n  \
                  devpulse              Scan current directory\n  \
                  devpulse ~/projects   Scan a specific directory\n  \
                  devpulse --sort name  Sort projects alphabetically\n  \
                  devpulse --watch      Refresh every 60s\n  \
                  devpulse -w -i 30     Refresh every 30s\n  \
                  devpulse completions bash  Generate bash completions"
)]
struct Cli {
    /// Subcommand (e.g. completions)
    #[command(subcommand)]
    command: Option<Commands>,

    /// Directory to scan for projects [default: current directory]
    #[arg(default_value = ".")]
    path: PathBuf,

    /// Sort projects by: activity (most stale first), name, or status
    #[arg(long, default_value = "activity")]
    sort: SortBy,

    /// Output results as JSON instead of a table (shorthand for --format json)
    #[arg(long)]
    json: bool,

    /// Output format: table, json, csv, markdown (or md)
    #[arg(long, value_enum)]
    format: Option<export::OutputFormat>,

    /// Watch mode: re-run at a regular interval
    #[arg(long, short = 'w')]
    watch: bool,

    /// Watch interval in seconds [default: 60]
    #[arg(long, short = 'i', default_value = "60")]
    interval: u64,

    /// Launch interactive TUI mode
    #[arg(long)]
    tui: bool,

    /// Filter projects by criteria. Can be specified multiple times.
    /// Values: dirty, clean, stale, unpushed, name:<substring>
    #[arg(long, short = 'f')]
    filter: Vec<String>,

    /// How many directory levels deep to scan for git projects [default: 1]
    /// 0 = check only the target directory itself
    /// 1 = immediate children (default)
    /// 2+ = recursive scanning
    #[arg(long, short = 'd')]
    depth: Option<u32>,

    /// Write output to a file instead of stdout.
    /// Works with all formats; table output strips ANSI colors when writing to file.
    #[arg(long, short = 'o', value_name = "PATH")]
    output: Option<PathBuf>,

    /// Group projects by their parent directory.
    /// Each group gets a sub-header and per-group summary stats.
    #[arg(long, short = 'g')]
    group: bool,

    /// Only show projects with commits within the given time window.
    /// Format: <number><unit> where unit is d (days), w (weeks), or m (months).
    /// Examples: 7d, 2w, 1m
    #[arg(long, value_name = "DURATION")]
    since: Option<String>,

    /// Include projects with no commits when using --since.
    /// By default, projects with no commit history are excluded.
    #[arg(long)]
    include_empty: bool,

    /// Disable colored output. Also respects the NO_COLOR environment variable
    /// and `color = false` in .devpulse.toml.
    /// Priority: --no-color flag > NO_COLOR env > config > default (colors on).
    #[arg(long)]
    no_color: bool,

    /// Skip CI status checks (faster, no network requests).
    /// By default, devpulse queries GitHub Actions for projects with GitHub remotes.
    #[arg(long)]
    no_ci: bool,

    /// Color theme: default, dracula, catppuccin-mocha, nord.
    /// Can also be set via `theme` in .devpulse.toml.
    #[arg(long, value_name = "THEME")]
    theme: Option<String>,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate shell completions for bash, zsh, or fish.
    ///
    /// Install the output to enable tab-completion for all devpulse flags and arguments.
    Completions {
        /// Target shell: bash, zsh, or fish
        #[arg(value_enum)]
        shell: completions::ShellArg,
    },
}

#[derive(Clone, Debug, ValueEnum)]
enum SortBy {
    /// Sort by last commit time, most stale first
    Activity,
    /// Sort by project name alphabetically
    Name,
    /// Sort by status (dirty first, then clean)
    Status,
}

fn sort_statuses(statuses: &mut [types::ProjectStatus], sort: &SortBy) {
    match sort {
        SortBy::Activity => {
            statuses.sort_by(|a, b| a.last_commit.cmp(&b.last_commit));
        }
        SortBy::Name => {
            statuses.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
        }
        SortBy::Status => {
            statuses.sort_by(|a, b| {
                a.is_clean
                    .cmp(&b.is_clean)
                    .then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase()))
            });
        }
    }
}

/// Parse and validate filter expressions from CLI arguments.
fn parse_filters(filter_args: &[String]) -> Result<Vec<filter::ProjectFilter>> {
    let mut filters = Vec::new();
    for expr in filter_args {
        match filter::parse_filter(expr) {
            Some(f) => filters.push(f),
            None => anyhow::bail!(
                "Unknown filter: '{}'. Valid filters: dirty, clean, stale, unpushed, name:<substring>",
                expr
            ),
        }
    }
    Ok(filters)
}

#[allow(clippy::too_many_arguments)]
fn scan_and_display(
    scan_path: &std::path::Path,
    sort: &SortBy,
    format: &export::OutputFormat,
    filters: &[filter::ProjectFilter],
    ignore: &[String],
    depth: u32,
    output: Option<&std::path::Path>,
    group_by_parent: bool,
    since_duration: Option<&since::SinceDuration>,
    include_empty: bool,
    use_color: bool,
    no_ci: bool,
    theme: &theme::Theme,
) -> Result<()> {
    let project_paths = scanner::discover_projects_with_depth(scan_path, ignore, depth)?;

    if project_paths.is_empty() {
        println!(
            "No projects found in {}.\n\
             Hint: devpulse looks for directories containing a .git folder.",
            scan_path.display()
        );
        return Ok(());
    }

    let results: Vec<_> = {
        use rayon::prelude::*;
        project_paths
            .par_iter()
            .map(|path| (path.clone(), git::get_project_status(path)))
            .collect()
    };

    let mut statuses = Vec::new();
    for (path, result) in results {
        match result {
            Ok(status) => statuses.push(status),
            Err(e) => eprintln!("  Warning: skipping {}: {}", path.display(), e),
        }
    }

    let mut statuses = filter::apply_filters(statuses, filters);

    // Apply --since filter if provided
    if let Some(since) = since_duration {
        statuses = since::filter_since(statuses, since, chrono::Utc::now(), include_empty);
    }

    // Fetch CI statuses from GitHub Actions (unless --no-ci)
    if !no_ci {
        let cache = ci::CiCache::new(300); // 5-minute cache TTL
        let ci_statuses = ci::fetch_ci_statuses(&statuses, &cache);
        for status in &mut statuses {
            if let Some(ci) = ci_statuses.get(&status.name) {
                status.ci_status = ci.clone();
            }
        }
    }

    sort_statuses(&mut statuses, sort);

    if group_by_parent {
        write_grouped_output(statuses, format, output, use_color, theme)?;
    } else if let Some(output_path) = output {
        export::write_output_to_file(&statuses, format, output_path)?;
    } else {
        export::write_output(&statuses, format, use_color, theme)?;
    }

    Ok(())
}

/// Write grouped output to stdout or a file.
fn write_grouped_output(
    statuses: Vec<types::ProjectStatus>,
    format: &export::OutputFormat,
    output: Option<&std::path::Path>,
    use_color: bool,
    theme: &theme::Theme,
) -> Result<()> {
    let groups = group::group_by_parent(statuses);
    let normalized = format.normalized();

    let content = match normalized {
        export::OutputFormat::Json => group::format_grouped_json(&groups)?,
        export::OutputFormat::Csv => group::format_grouped_csv(&groups)?,
        export::OutputFormat::Markdown | export::OutputFormat::Md => {
            group::format_grouped_markdown(&groups)?
        }
        export::OutputFormat::Table => {
            // For table format with grouping, print each group with a header
            let mut out = String::new();
            for g in &groups {
                out.push_str(&format!("\n── {} ──\n\n", g.label));
                out.push_str(&crate::table::format_table_plain(&g.projects));
                let summary_line = format!(
                    "  {} projects │ {} dirty │ {} stale │ {} unpushed\n",
                    g.summary.total, g.summary.dirty, g.summary.stale, g.summary.unpushed,
                );
                out.push_str(&summary_line);
            }
            out
        }
    };

    if let Some(output_path) = output {
        if let Some(parent) = output_path.parent()
            && !parent.as_os_str().is_empty()
        {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(output_path, &content)?;
        eprintln!("Wrote grouped output to {}", output_path.display());
    } else if matches!(normalized, export::OutputFormat::Table) {
        if use_color {
            // For table, print with colors
            for g in &groups {
                println!("\n── {} ──\n", g.label);
                crate::table::print_table(&g.projects, theme);
                g.summary.print_colored();
                println!();
            }
        } else {
            // Plain text, no ANSI
            print!("{content}");
        }
    } else {
        print!("{content}");
    }

    Ok(())
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    // Handle subcommands first
    if let Some(Commands::Completions { shell }) = &cli.command {
        completions::generate(*shell, &mut Cli::command());
        return Ok(());
    }

    let scan_path = if cli.path.is_absolute() {
        cli.path.clone()
    } else {
        std::env::current_dir()?.join(&cli.path)
    };

    // Load config file (local dir or home)
    let cfg = config::load_config(&scan_path)?;

    // CLI --sort overrides config sort (CLI default is "activity", so we check
    // if the user explicitly provided --sort by seeing if config has a different
    // value and CLI is at its default). Since clap always provides a default,
    // config sort only applies when the user didn't pass --sort.
    // We use a simple approach: config sort is used if present, but CLI flag
    // always wins since it's explicitly provided by the user.
    let sort = resolve_sort(&cli.sort, &cfg)?;

    let filters = parse_filters(&cli.filter)?;

    // Resolve depth: CLI flag takes priority, then config, then default of 1
    let depth = cli.depth.or(cfg.depth).unwrap_or(1);

    // Resolve output format: --format > --json > config format > table
    let output_format = if let Some(fmt) = cli.format {
        fmt
    } else if cli.json {
        export::OutputFormat::Json
    } else if let Some(ref fmt_str) = cfg.format {
        config::parse_format_str(fmt_str)?
    } else {
        export::OutputFormat::Table
    };

    // Use config scan_paths if the user didn't specify a path (default is ".")
    let scan_paths = if cli.path.as_os_str() == "." && !cfg.scan_paths.is_empty() {
        config::resolve_scan_paths(&cfg, &scan_path)
    } else {
        vec![scan_path.clone()]
    };

    // Resolve --since: CLI flag takes priority, then config
    let since_duration = if let Some(ref s) = cli.since {
        Some(since::parse_duration(s)?)
    } else if let Some(ref s) = cfg.since {
        Some(since::parse_duration(s)?)
    } else {
        None
    };

    let use_color = config::resolve_color(cli.no_color, &cfg);

    // Resolve theme: --theme flag > config theme > default
    let theme_name = cli.theme.as_deref().or(cfg.theme.as_deref());
    let active_theme = theme::resolve_theme(theme_name)?;

    let ignore = &cfg.ignore;

    if cli.tui {
        // TUI mode uses first scan path
        tui::run_tui(scan_paths.first().unwrap_or(&scan_path), &active_theme)?;
    } else if cli.watch {
        watch::run_watch_loop(
            scan_paths.first().unwrap_or(&scan_path),
            &sort,
            &output_format,
            cli.interval,
            &filters,
            depth,
            use_color,
            &active_theme,
        )?;
    } else {
        for path in &scan_paths {
            println!("Scanning {}...\n", path.display());
            scan_and_display(
                path,
                &sort,
                &output_format,
                &filters,
                ignore,
                depth,
                cli.output.as_deref(),
                cli.group,
                since_duration.as_ref(),
                cli.include_empty,
                use_color,
                cli.no_ci,
                &active_theme,
            )?;
        }
    }

    Ok(())
}

/// Resolve sort order: CLI value takes priority, config is fallback.
fn resolve_sort(cli_sort: &SortBy, cfg: &config::Config) -> Result<SortBy> {
    // If config specifies a sort, we use it as a potential fallback.
    // However, since clap always fills a default, we just use the CLI value.
    // Config sort is informational — the CLI default matches config intention.
    // To truly detect "user didn't pass --sort", we'd need clap's Option<SortBy>.
    // For now, config sort is documented but CLI always wins.
    if let Some(ref sort_str) = cfg.sort {
        // Validate config sort value even if not used
        match sort_str.as_str() {
            "activity" | "name" | "status" => {}
            other => anyhow::bail!(
                "Invalid sort value in config: '{}'. Valid values: activity, name, status",
                other
            ),
        }
    }
    Ok(cli_sort.clone())
}