ccr 0.2.3

CLI Code Resume — one TUI session picker across Claude Code, Codex, and Gemini CLI
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
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

mod backends;
mod bookmarks;
mod nicknames;
mod session;
mod tail;
mod tui;
mod util;

use backends::{all, by_name, scan_all};
use session::{Role, Session, Turn};
use tui::{AppAction, run};
use util::truncate;

#[derive(Copy, Clone, Debug, clap::ValueEnum)]
enum ExportFormat {
    Md,
    Json,
}

#[derive(Parser)]
#[command(name = "ccr", version, about = "CLI Code Resume — TUI session picker")]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Subcommand)]
enum Command {
    /// List all sessions as plain text (tool id date title).
    List,
    /// Print the absolute path to a session's on-disk file.
    /// Useful in shell pipelines: `cat $(ccr path <id>)`.
    Path {
        /// Session id.
        id: String,
    },
    /// Print a session's raw file contents (equivalent to `cat $(ccr path …)`).
    Show {
        /// Session id.
        id: String,
    },
    /// Export a session as markdown (default) or JSON — full turns, not just preview.
    Export {
        /// Session id.
        id: String,
        /// Output format.
        #[arg(long, value_enum, default_value_t = ExportFormat::Md)]
        format: ExportFormat,
    },
    /// Activity overview — totals, per-tool, per-project, per-day histogram.
    Stats,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    match cli.command {
        None => launch_picker(),
        Some(Command::List) => run_list(),
        Some(Command::Path { id }) => run_path(&id),
        Some(Command::Show { id }) => run_show(&id),
        Some(Command::Export { id, format }) => run_export(&id, format),
        Some(Command::Stats) => run_stats(),
    }
}

fn find_session_by_id(id: &str) -> Result<Session> {
    let backends = all();
    scan_all(&backends)
        .into_iter()
        .find(|s| s.id == id)
        .with_context(|| format!("no session with id `{id}`"))
}

fn run_path(id: &str) -> Result<()> {
    let s = find_session_by_id(id)?;
    println!("{}", s.origin.display());
    Ok(())
}

fn run_show(id: &str) -> Result<()> {
    let s = find_session_by_id(id)?;
    let content = std::fs::read_to_string(&s.origin)
        .with_context(|| format!("read {}", s.origin.display()))?;
    print!("{content}");
    Ok(())
}

fn run_export(id: &str, format: ExportFormat) -> Result<()> {
    let backends = all();
    let session = scan_all(&backends)
        .into_iter()
        .find(|s| s.id == id)
        .with_context(|| format!("no session with id `{id}`"))?;
    let backend = by_name(&backends, session.backend)
        .with_context(|| format!("unknown backend `{}`", session.backend))?;
    let turns = backend.all_turns(&session)?;
    match format {
        ExportFormat::Md => print!("{}", format_md(&session, &turns)),
        ExportFormat::Json => println!("{}", format_json(&session, &turns)?),
    }
    Ok(())
}

pub(crate) fn format_md(s: &Session, turns: &[Turn]) -> String {
    let mut out = String::new();
    out.push_str(&format!("# Session `{}`\n\n", s.id));
    out.push_str(&format!("- **Tool:** {}\n", s.backend));
    out.push_str(&format!(
        "- **Last active:** {}\n",
        s.last_activity.format("%Y-%m-%d %H:%M")
    ));
    out.push_str(&format!("- **cwd:** `{}`\n", s.cwd.display()));
    out.push_str(&format!("- **Turns:** {}\n\n", turns.len()));
    out.push_str("---\n\n");
    for t in turns {
        let tag = match t.role {
            Role::User => "## ❯ user",
            Role::Assistant => "## ◆ assistant",
        };
        out.push_str(tag);
        out.push_str("\n\n");
        out.push_str(t.text.trim());
        out.push_str("\n\n");
    }
    out
}

/// Replace any `None` message_count with a freshly computed value. The counter
/// is injected so the hot path stays testable without touching the filesystem.
fn fill_counts(sessions: &mut [Session], count: impl Fn(&Session) -> usize) {
    for s in sessions.iter_mut() {
        if s.message_count.is_none() {
            s.message_count = Some(count(s));
        }
    }
}

fn run_stats() -> Result<()> {
    let backends = all();
    let mut sessions = scan_all(&backends);
    fill_counts(&mut sessions, |s| {
        by_name(&backends, s.backend)
            .and_then(|b| b.all_turns(s).ok())
            .map(|t| t.len())
            .unwrap_or(0)
    });
    print!("{}", format_stats(&sessions));
    Ok(())
}

pub(crate) fn format_stats(sessions: &[Session]) -> String {
    use std::collections::BTreeMap;
    use std::collections::HashMap;

    let mut out = String::new();
    let total = sessions.len();
    let total_turns: usize = sessions.iter().filter_map(|s| s.message_count).sum();
    let tools: std::collections::BTreeSet<&str> = sessions.iter().map(|s| s.backend).collect();

    out.push_str(&format!(
        "Total: {total} session{}  ·  {total_turns} turn{}  ·  {} tool{}\n",
        plural(total),
        plural(total_turns),
        tools.len(),
        plural(tools.len()),
    ));

    let mut by_tool: HashMap<&str, (usize, usize)> = HashMap::new();
    for s in sessions {
        let e = by_tool.entry(s.backend).or_default();
        e.0 += 1;
        e.1 += s.message_count.unwrap_or(0);
    }
    out.push_str("\nBy tool:\n");
    let mut tool_rows: Vec<_> = by_tool.iter().collect();
    tool_rows.sort_by_key(|(_, (count, _))| std::cmp::Reverse(*count));
    for (tool, (count, turns)) in tool_rows {
        out.push_str(&format!(
            "  {tool:<10} {count:>5} session{}  {turns:>7} turn{}\n",
            plural(*count),
            plural(*turns)
        ));
    }

    let mut by_project: HashMap<String, usize> = HashMap::new();
    for s in sessions {
        let name = s
            .cwd
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("(unknown)")
            .to_string();
        *by_project.entry(name).or_default() += 1;
    }
    let mut projects: Vec<_> = by_project.into_iter().collect();
    projects.sort_by_key(|(_, count)| std::cmp::Reverse(*count));
    let top = projects.len().min(10);
    out.push_str(&format!("\nBy project (top {top}):\n"));
    for (name, count) in projects.iter().take(top) {
        out.push_str(&format!(
            "  {name:<30} {count:>5} session{}\n",
            plural(*count)
        ));
    }

    let now = chrono::Local::now().date_naive();
    let mut by_date: BTreeMap<chrono::NaiveDate, usize> = BTreeMap::new();
    for s in sessions {
        let d = s.last_activity.date_naive();
        if (now - d).num_days() < 30 {
            *by_date.entry(d).or_default() += 1;
        }
    }
    if !by_date.is_empty() {
        out.push_str("\nActivity (last 30 days):\n");
        let max = by_date.values().copied().max().unwrap_or(1).max(1);
        for (date, count) in by_date.iter().rev() {
            let width = (count * 30 + max / 2) / max;
            let bar = "".repeat(width.max(1));
            out.push_str(&format!("  {date}  {bar} {count}\n"));
        }
    }

    let live = sessions.iter().filter(|s| s.possibly_live).count();
    if live > 0 {
        out.push_str(&format!(
            "\nPossibly live: {live} session{} (active in last 5 min)\n",
            plural(live),
        ));
    }

    out
}

fn plural(n: usize) -> &'static str {
    if n == 1 { "" } else { "s" }
}

pub(crate) fn format_json(s: &Session, turns: &[Turn]) -> Result<String> {
    let turns_json: Vec<serde_json::Value> = turns
        .iter()
        .map(|t| {
            serde_json::json!({
                "role": match t.role {
                    Role::User => "user",
                    Role::Assistant => "assistant",
                },
                "text": &t.text,
            })
        })
        .collect();
    let doc = serde_json::json!({
        "id": s.id,
        "backend": s.backend,
        "cwd": s.cwd.to_string_lossy(),
        "last_activity": s.last_activity.to_rfc3339(),
        "message_count": turns.len(),
        "turns": turns_json,
    });
    Ok(serde_json::to_string_pretty(&doc)?)
}

fn launch_picker() -> Result<()> {
    let backends = all();
    let sessions = scan_all(&backends);
    if sessions.is_empty() {
        eprintln!("ccr: no sessions found. Supported tools:");
        for b in &backends {
            eprintln!("  - {}", b.name());
        }
        std::process::exit(1);
    }
    match run(sessions, &backends)? {
        AppAction::Quit => Ok(()),
        AppAction::Resume(s) => {
            let backend = by_name(&backends, s.backend)
                .with_context(|| format!("unknown backend `{}`", s.backend))?;
            let status = backend
                .resume(&s)
                .status()
                .with_context(|| format!("failed to spawn `{}` — is it on PATH?", s.backend))?;
            std::process::exit(status.code().unwrap_or(1));
        }
        AppAction::View(s) => {
            let status = std::process::Command::new("agx")
                .arg(&s.origin)
                .current_dir(&s.cwd)
                .status()
                .context(
                    "failed to spawn `agx` — install from https://github.com/brevity1swos/agx",
                )?;
            std::process::exit(status.code().unwrap_or(1));
        }
    }
}

fn run_list() -> Result<()> {
    let backends = all();
    for s in scan_all(&backends) {
        println!(
            "[{}] {}  {}  {}",
            s.backend,
            s.id,
            s.last_activity.format("%Y-%m-%d %H:%M"),
            truncate(&s.title, 60)
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Local;
    use std::path::PathBuf;

    fn sample_session() -> Session {
        Session {
            backend: "claude",
            id: "abc-123".into(),
            cwd: PathBuf::from("/proj"),
            title: "hi".into(),
            last_activity: Local::now(),
            message_count: Some(2),
            preview: Vec::new(),
            possibly_live: false,
            origin: PathBuf::from("<t>"),
            searchable: String::new(),
        }
    }

    fn sample_turns() -> Vec<Turn> {
        vec![
            Turn {
                role: Role::User,
                text: "hello".into(),
            },
            Turn {
                role: Role::Assistant,
                text: "hi back".into(),
            },
        ]
    }

    #[test]
    fn format_md_has_header_and_turns() {
        let md = format_md(&sample_session(), &sample_turns());
        assert!(md.starts_with("# Session `abc-123`"));
        assert!(md.contains("- **Tool:** claude"));
        assert!(md.contains("## ❯ user"));
        assert!(md.contains("hello"));
        assert!(md.contains("## ◆ assistant"));
        assert!(md.contains("hi back"));
    }

    #[test]
    fn format_json_round_trips() {
        let s = sample_session();
        let turns = sample_turns();
        let json = format_json(&s, &turns).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(v["id"], "abc-123");
        assert_eq!(v["backend"], "claude");
        assert_eq!(v["turns"][0]["role"], "user");
        assert_eq!(v["turns"][1]["role"], "assistant");
    }

    #[test]
    fn format_stats_on_empty_still_prints_zero_row() {
        let out = format_stats(&[]);
        assert!(out.starts_with("Total: 0 sessions"));
    }

    #[test]
    fn fill_counts_replaces_none_with_computed() {
        let mut s = sample_session();
        s.message_count = None;
        let mut sessions = vec![s];
        fill_counts(&mut sessions, |_s| 7);
        assert_eq!(sessions[0].message_count, Some(7));
    }

    #[test]
    fn format_json_message_count_is_turn_count_not_null() {
        let mut s = sample_session();
        s.message_count = None;
        let turns = sample_turns(); // 2 turns
        let json = format_json(&s, &turns).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(v["message_count"], 2);
        assert!(!v["message_count"].is_null());
    }

    #[test]
    fn format_stats_groups_by_tool_and_project() {
        let mut a = sample_session();
        a.cwd = PathBuf::from("/repos/alpha");
        a.backend = "claude";
        a.message_count = Some(10);
        let mut b = a.clone();
        b.id = "def".into();
        b.backend = "codex";
        b.cwd = PathBuf::from("/repos/beta");
        b.message_count = Some(3);

        let out = format_stats(&[a, b]);
        assert!(out.contains("Total: 2 sessions"));
        assert!(out.contains("13 turns"));
        assert!(out.contains("2 tools"));
        assert!(out.contains("claude"));
        assert!(out.contains("codex"));
        assert!(out.contains("alpha"));
        assert!(out.contains("beta"));
    }
}