fix(inkscape): add -s/--save flag for auto-save in one-shot mode (fixes #182)

One-shot commands start a fresh Session each run, so in-memory changes
were silently discarded without an explicit 'document save'. This ports
the same pattern already used in the Shotcut harness (commit 17a30ba):

- Add -s/--save flag to the cli() group
- Register ctx.call_on_close(_auto_save_callback)
- _auto_save_callback saves if session is modified and not in REPL mode
This commit is contained in:
Octopus
2026-04-07 20:01:40 +08:00
committed by yuhao
parent 09a3dfa644
commit 3e0c3137a6

View File

@@ -39,6 +39,7 @@ from cli_anything.inkscape.core import export as export_mod
_session: Optional[Session] = None
_json_output = False
_repl_mode = False
_auto_save = False
def get_session() -> Session:
@@ -120,14 +121,20 @@ def handle_error(func):
@click.option("--json", "use_json", is_flag=True, help="Output as JSON")
@click.option("--project", "project_path", type=str, default=None,
help="Path to .inkscape-cli.json project file")
@click.option("-s", "--save", "auto_save", is_flag=True,
help="Auto-save project after each mutation command (one-shot mode)")
@click.pass_context
def cli(ctx, use_json, project_path):
def cli(ctx, use_json, project_path, auto_save):
"""Inkscape CLI — Stateful vector graphics editing from the command line.
Run without a subcommand to enter interactive REPL mode.
Use -s/--save to automatically save changes after each mutation command.
This is useful in one-shot mode where each command runs in a new process.
"""
global _json_output
global _json_output, _auto_save
_json_output = use_json
_auto_save = auto_save
if project_path:
sess = get_session()
@@ -135,10 +142,26 @@ def cli(ctx, use_json, project_path):
proj = doc_mod.open_document(project_path)
sess.set_project(proj, project_path)
# Register auto-save callback to run after each command
ctx.call_on_close(_auto_save_callback)
if ctx.invoked_subcommand is None:
ctx.invoke(repl, project_path=None)
def _auto_save_callback():
"""Auto-save callback that runs after each command."""
global _auto_save, _session
if _auto_save and _session and _session.has_project() and _session._modified:
# Don't auto-save if we're in REPL mode (user can explicitly save)
if not _repl_mode:
try:
saved = _session.save_session()
click.echo(f"Auto-saved to: {saved}")
except Exception as e:
click.echo(f"Auto-save failed: {e}", err=True)
# ── Document Commands ───────────────────────────────────────────
@cli.group()
def document():