From 2f8bc985b79e036ca6d34bb680a8858154c17a3b Mon Sep 17 00:00:00 2001 From: Elvis Claros Castro Date: Sat, 3 Oct 2026 13:07:58 -0300 Subject: Export a single session, or only what the filters select --json and --html now honour -s, the text filter, -p, -g, -E and -n. With -s the page defaults to session-.html. A partial export carries only the memories of the projects it includes; with no filter everything goes, as before. --- claude_logbook/__init__.py | 2 +- claude_logbook/cli.py | 59 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 14 deletions(-) (limited to 'claude_logbook') diff --git a/claude_logbook/__init__.py b/claude_logbook/__init__.py index abe9c71..f7d4458 100644 --- a/claude_logbook/__init__.py +++ b/claude_logbook/__init__.py @@ -3,6 +3,6 @@ No dependencies: standard library only. """ -__version__ = "1.3.0" +__version__ = "1.4.0" __all__ = ["__version__"] diff --git a/claude_logbook/cli.py b/claude_logbook/cli.py index 43b08eb..f90ac26 100644 --- a/claude_logbook/cli.py +++ b/claude_logbook/cli.py @@ -37,6 +37,8 @@ examples: claude-logbook -r 3 command to resume #3 eval "$(claude-logbook -r 3)" resume it right away claude-logbook --html --open write sessions.html and open it + claude-logbook -s 3 --html export only #3 (session-.html) + claude-logbook -p api --json export only that project's sessions the # is the position in the table you are looking at, so if you filtered you have to repeat the filter to read that row: @@ -131,9 +133,11 @@ def build_parser(): output_path = ap.add_argument_group("export") output_path.add_argument("--json", action="store_true", - help="dump every session as JSON") - output_path.add_argument("--html", nargs="?", const=DEFAULT_HTML, metavar="FILE", - help=f"write a self-contained page (default {DEFAULT_HTML})") + help="dump the sessions as JSON (all, or what -s and " + "the filters select)") + output_path.add_argument("--html", nargs="?", const="", metavar="FILE", + help=f"write a self-contained page (default {DEFAULT_HTML}, " + f"or session-.html with -s)") output_path.add_argument("--template", metavar="FILE", help="use another template for --html") output_path.add_argument("--open", action="store_true", @@ -266,19 +270,44 @@ def delete_targets(pool, args): # ──────────────────────────────── commands ──────────────────────────────── -def cmd_json(sessions): +def export_selection(sessions, args): + """What --json and --html export: the -s session, or what the filters and + -n leave. With no filter at all, everything, memories included; otherwise + only the memories of the projects that made it.""" + if args.show: + chosen = [pick(filtered(sessions, args), args.show)] + else: + chosen = filtered(sessions, args) + if args.limit: + chosen = chosen[: args.limit] + + memories = mem.load_memories(sessions) + if len(chosen) != len(sessions): + dirs = {s["project_dir"] for s in chosen} + memories = [m for m in memories if m["project_dir"] in dirs] + return chosen, memories + + +def cmd_json(sessions, memories): import json payload = webpage.build_payload( - public_records(sessions), - mem.public_records(mem.load_memories(sessions))) + public_records(sessions), mem.public_records(memories)) json.dump(payload, sys.stdout, ensure_ascii=False, separators=(",", ":")) sys.stdout.write("\n") return 0 -def cmd_html(sessions, args): - out = args.html - memories = mem.public_records(mem.load_memories(sessions)) +def html_path(chosen, args): + if args.html: + return args.html + if args.show: + return f"session-{chosen[0]['id'][:8]}.html" + return DEFAULT_HTML + + +def cmd_html(sessions, memories, args): + out = html_path(sessions, args) + memories = mem.public_records(memories) stats = webpage.write(public_records(sessions), out, memories=memories, template=webpage.template_text(args.template)) print(f"{stats['sessions']} sessions · {stats['projects']} projects · " @@ -558,10 +587,14 @@ def run(args): if args.memory: return run_memory(sessions, args, st) - if args.json: - return cmd_json(sessions) - if args.html: - return cmd_html(sessions, args) + if args.json or args.html is not None: + chosen, memories = export_selection(sessions, args) + if not chosen: + print("No session matches that filter.", file=sys.stderr) + return 1 + if args.json: + return cmd_json(chosen, memories) + return cmd_html(chosen, memories, args) if args.delete or args.delete_empty: targets = delete_targets(filtered(sessions, args), args) -- cgit v1.2.3