shellist 0.3.0

Shell history analysis. Parses bash, zsh, and fish history, counts commands, and ranks by frequency.
Documentation
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
//! Shellist — shell history analysis CLI.
//!
//! Reads shell history (bash, zsh, fish), ranks commands by frequency, and
//! prints a table. Supports multiple output formats, subcommand depth,
//! regex filtering, date ranges, trends, and shell completions.
//!
//! ```text
//! $ shellist --top 3 --bars
//! Rank  Command  Count  Bars
//! ----  -------  -----  ------------------------------
//!    1  ls         120  ##############################
//!    2  git         95  #######################
//!    3  cd          80  ####################
//! ```

use std::env;
use std::io::{IsTerminal, Read};
use std::process;

use shellist::{
    Bucket, HistoryParser, Shell, TableOptions, completions, count_commands_at_depth,
    default_history_path, detect_shell, filter_by_min_frequency, filter_commands, format_csv,
    format_json, format_stats, format_table, format_trend, grep_filter, load_history_file,
    man_page, parse_date_to_unix, rank_commands, rank_commands_ascending, top_n,
};

use regex::Regex;

/// Bash builtins that often leak into `.bash_history` from shell init scripts.
const DEFAULT_IGNORE: &[&str] = &["set", "shopt"];

struct Args {
    top: Option<usize>,
    ignore: Vec<String>,
    no_default_ignore: bool,
    min_freq: Option<usize>,
    path: Option<String>,
    shell: Option<Shell>,
    depth: Option<usize>,
    json: bool,
    csv: bool,
    bars: bool,
    percent: bool,
    stats: bool,
    grep: Option<String>,
    asc: bool,
    since: Option<String>,
    until: Option<String>,
    trend: bool,
    trend_bucket: Option<Bucket>,
    output: Option<String>,
    completions: Option<Shell>,
    man: bool,
}

impl Args {
    const fn empty() -> Self {
        Self {
            top: None,
            ignore: Vec::new(),
            no_default_ignore: false,
            min_freq: None,
            path: None,
            shell: None,
            depth: None,
            json: false,
            csv: false,
            bars: false,
            percent: false,
            stats: false,
            grep: None,
            asc: false,
            since: None,
            until: None,
            trend: false,
            trend_bucket: None,
            output: None,
            completions: None,
            man: false,
        }
    }
}

fn need_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> String {
    iter.next().unwrap_or_else(|| {
        eprintln!("shellist: {flag} requires a value");
        process::exit(1);
    })
}

fn parse_usize(val: String, flag: &str) -> usize {
    val.parse().unwrap_or_else(|_| {
        eprintln!("shellist: {flag} expects a number, got '{val}'");
        process::exit(1);
    })
}

fn parse_args() -> Args {
    let mut args = Args::empty();
    let mut iter = env::args().skip(1);

    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "-" => args.path = Some("-".to_string()),
            "--help" => {
                print_help();
                process::exit(0);
            }
            "--top" => args.top = Some(parse_usize(need_value(&mut iter, "--top"), "--top")),
            "--min" => args.min_freq = Some(parse_usize(need_value(&mut iter, "--min"), "--min")),
            "--depth" => {
                args.depth = Some(parse_usize(need_value(&mut iter, "--depth"), "--depth"))
            }
            "--ignore" => {
                let val = need_value(&mut iter, "--ignore");
                args.ignore = val.split(',').map(|s| s.trim().to_lowercase()).collect();
            }
            "--no-default-ignore" => args.no_default_ignore = true,
            "--path" => args.path = Some(need_value(&mut iter, "--path")),
            "--shell" => {
                let val = need_value(&mut iter, "--shell");
                args.shell = Some(parse_shell(&val, "--shell"));
            }
            "--json" => args.json = true,
            "--csv" => args.csv = true,
            "--bars" => args.bars = true,
            "--percent" => args.percent = true,
            "--stats" => args.stats = true,
            "--grep" => args.grep = Some(need_value(&mut iter, "--grep")),
            "--asc" => args.asc = true,
            "--since" => args.since = Some(need_value(&mut iter, "--since")),
            "--until" => args.until = Some(need_value(&mut iter, "--until")),
            "--trend" => args.trend = true,
            "--trend-bucket" => {
                let val = need_value(&mut iter, "--trend-bucket");
                args.trend_bucket = Some(parse_bucket(&val));
            }
            "--output" => args.output = Some(need_value(&mut iter, "--output")),
            "--completions" => {
                let val = need_value(&mut iter, "--completions");
                args.completions = Some(parse_shell(&val, "--completions"));
            }
            "--man" => args.man = true,
            other => {
                eprintln!("shellist: unknown flag '{other}'");
                process::exit(1);
            }
        }
    }

    args
}

fn parse_shell(val: &str, flag: &str) -> Shell {
    Shell::from_name(val).unwrap_or_else(|| {
        eprintln!("shellist: {flag} expects bash, zsh, or fish, got '{val}'");
        process::exit(1);
    })
}

fn parse_bucket(val: &str) -> Bucket {
    Bucket::from_name(val).unwrap_or_else(|| {
        eprintln!("shellist: --trend-bucket expects day, week, or month, got '{val}'");
        process::exit(1);
    })
}

fn print_help() {
    println!(
        "shellist {version} — shell history analysis

USAGE:
    shellist [OPTIONS]

INPUT:
    --path PATH          Read history from PATH (default: per-shell file)
    --shell bash|zsh|fish  Force a parser (default: auto-detect)
    -                    Read history from stdin (or pipe in)

FILTERING:
    --top N              Show only the top N commands
    --ignore X,Y         Exclude commands (comma-separated)
    --no-default-ignore  Don't filter bash internals ({ignores})
    --min N              Only commands used at least N times
    --grep PATTERN       Keep commands matching a regex
    --depth N            Treat first N tokens as the command key (default 1)
    --since DATE         Only on/after DATE (YYYY-MM-DD, needs timestamps)
    --until DATE         Only on/before DATE (YYYY-MM-DD, needs timestamps)
    --asc                Sort ascending

OUTPUT:
    --bars               Add an ASCII bar chart column
    --percent            Add a percentage column
    --json               Output as JSON
    --csv                Output as CSV
    --stats              Print summary statistics
    --trend              Usage bucketed over time, UTC-based (needs timestamps)
    --trend-bucket day|week|month|daily|weekly|monthly  Bucket for --trend (default: day)
    --output FILE        Write output to FILE instead of stdout

INTEGRATION:
    --completions bash|zsh|fish  Print a completion script
    --man                Print the man page
    --help               Print this help",
        version = env!("CARGO_PKG_VERSION"),
        ignores = DEFAULT_IGNORE.join(", ")
    );
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
    let mut args = parse_args();
    execute(&mut args)
}

fn execute(args: &mut Args) -> Result<(), Box<dyn std::error::Error>> {
    if args.man {
        print!("{}", man_page());
        return Ok(());
    }
    if let Some(shell) = args.completions {
        print!("{}", completions(shell));
        return Ok(());
    }

    let grep_re = match args.grep.as_deref() {
        Some(p) => Some(
            Regex::new(&format!("(?i){p}")).map_err(|e| format!("invalid --grep pattern: {e}"))?,
        ),
        None => None,
    };
    let since = match args.since.as_deref() {
        Some(d) => Some(
            parse_date_to_unix(d)
                .ok_or_else(|| format!("invalid --since date '{d}' (use YYYY-MM-DD)"))?,
        ),
        None => None,
    };
    let until = match args.until.as_deref() {
        Some(d) => Some(
            parse_date_to_unix(d)
                .ok_or_else(|| format!("invalid --until date '{d}' (use YYYY-MM-DD)"))?,
        ),
        None => None,
    };

    let content = read_input(args)?;
    let (output, was_empty) = core_pipeline(&content, args, grep_re, since, until)?;

    if was_empty {
        let source = source_label(args);
        eprintln!("shellist: no commands to show from {source}");
    } else {
        write_output(&output, args.output.as_deref())?;
    }
    Ok(())
}

fn core_pipeline(
    content: &str,
    args: &mut Args,
    grep_re: Option<Regex>,
    since: Option<i64>,
    until: Option<i64>,
) -> Result<(String, bool), Box<dyn std::error::Error>> {
    let shell = args.shell.unwrap_or_else(|| detect_shell(content));
    let mut entries = match shell {
        Shell::Bash => shellist::DefaultHistoryParser::new().parse(content),
        Shell::Zsh => shellist::ZshHistoryParser::new().parse(content),
        Shell::Fish => shellist::FishHistoryParser::new().parse(content),
    };

    if since.is_some() || until.is_some() {
        let had_timestamps = entries.iter().any(|e| e.timestamp.is_some());
        entries.retain(|e| match e.timestamp {
            Some(t) => since.is_none_or(|s| t as i64 >= s) && until.is_none_or(|u| t as i64 <= u),
            None => false,
        });
        if entries.is_empty() && !had_timestamps {
            eprintln!(
                "shellist: no timestamped entries for date filter \
                 (need zsh extended, fish, or timestamped history)"
            );
            return Ok((String::new(), false));
        }
        if entries.is_empty() {
            eprintln!("shellist: date range excludes all entries");
            return Ok((String::new(), false));
        }
    }

    if args.trend {
        let bucket = args.trend_bucket.unwrap_or(Bucket::Day);
        let out = format_trend(&entries, bucket);
        if out.is_empty() {
            eprintln!(
                "shellist: no timestamped entries for --trend \
                 (need zsh extended, fish, or timestamped history)"
            );
            return Ok((String::new(), false));
        }
        return Ok((out, false));
    }

    let depth = args.depth.unwrap_or(1);
    let counts = count_commands_at_depth(&entries, depth);
    let mut ranked = if args.asc {
        rank_commands_ascending(counts)
    } else {
        rank_commands(counts)
    };

    if let Some(re) = grep_re {
        ranked = grep_filter(&ranked, &re);
    }

    let ignore: Vec<String> = if args.no_default_ignore {
        std::mem::take(&mut args.ignore)
    } else {
        let mut merged = DEFAULT_IGNORE
            .iter()
            .map(|s| (*s).to_string())
            .collect::<Vec<_>>();
        merged.extend(std::mem::take(&mut args.ignore));
        merged
    };
    if !ignore.is_empty() {
        ranked = filter_commands(ranked, &ignore);
    }
    if let Some(min) = args.min_freq {
        ranked = filter_by_min_frequency(ranked, min);
    }
    if let Some(n) = args.top {
        ranked = top_n(ranked, n);
    }

    let (output, was_empty) = if args.json {
        (format_json(&ranked), false)
    } else if args.csv {
        (format_csv(&ranked), false)
    } else if args.stats {
        (format_stats(&ranked), false)
    } else if ranked.is_empty() {
        (String::new(), true)
    } else {
        let opts = TableOptions {
            percent: args.percent,
            bars: args.bars,
        };
        (format_table(&ranked, &opts), false)
    };

    Ok((output, was_empty))
}

fn read_input(args: &Args) -> Result<String, Box<dyn std::error::Error>> {
    if let Some(path) = &args.path {
        if path == "-" {
            return read_stdin();
        }
        return load_history_file(path).map_err(Into::into);
    }
    if !std::io::stdin().is_terminal() {
        return read_stdin();
    }
    let path = match args.shell {
        Some(shell) => shell.default_history_path(),
        None => default_history_path(),
    };
    let path =
        path.ok_or("HOME environment variable not set — cannot resolve default history path")?;
    load_history_file(path).map_err(Into::into)
}

fn read_stdin() -> Result<String, Box<dyn std::error::Error>> {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf)?;
    Ok(buf)
}

fn source_label(args: &Args) -> String {
    if let Some(p) = &args.path {
        if p == "-" {
            return "stdin".to_string();
        }
        return p.clone();
    }
    if !std::io::stdin().is_terminal() {
        return "stdin".to_string();
    }
    let path = match args.shell {
        Some(shell) => shell.default_history_path(),
        None => default_history_path(),
    };
    path.map_or_else(
        || "default history path".to_string(),
        |p| p.to_string_lossy().into_owned(),
    )
}

fn write_output(content: &str, output: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
    match output {
        Some(path) => std::fs::write(path, content)?,
        None => print!("{content}"),
    }
    Ok(())
}

fn main() {
    if let Err(e) = run() {
        eprintln!("shellist: {e}");
        process::exit(1);
    }
}