Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [Unreleased]

- TUI: open one file browser directly; Enter opens folders or runs MDA on the highlighted file, without the separate U folder-confirmation step.
- TUI: show primary navigation controls in the footer and put advanced shortcuts behind `?`; show the output-folder field only when relevant.
- TUI: keep processing responsive, prevent overlapping runs, and remember the last browsed folder on exit.
- `--in-place` now shows a diff and asks for confirmation per file (`--yes` skips the prompt; required when stdin is not a terminal). Declined files are reported as `declined` and left untouched.
- In-place writes always back up the original outside the vault: `--backup-dir` → `MDA_BACKUP_DIR` → `~/home/backups-obsidian`, as `<YYYYMMDD>/<vault>/<path>.<sha256[:8]>.bak`. `--backup` is a deprecated no-op; no sibling `.bak` files are created by the CLI any more.
- In-place writes are refused if the source changed since it was read, and the written bytes are verified against the confirmed text.
- TUI: in-place output mode is blocked with a message until it can show the per-file diff.

## [0.2.9]

- TUI: live read-only preview of the highlighted supported file (`.md`, `.txt`, `.pdf`, `.docx`, `.doc` listing).
Expand Down
837 changes: 728 additions & 109 deletions README.md

Large diffs are not rendered by default.

68 changes: 63 additions & 5 deletions mda_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import argparse
import difflib
import os
import sys
from pathlib import Path
Expand All @@ -20,6 +21,7 @@
list_skill_scripts,
load_system_prompt,
print_json_line,
resolve_backup_dir,
resolve_skill_dir,
resolve_skill_id,
resolve_skill_script,
Expand Down Expand Up @@ -121,6 +123,25 @@ def run_skill_script(
return 0


def confirm_in_place_write(dst: Path, old_text: str, new_text: str) -> bool:
"""Show the diff for ``dst`` on stderr and ask before overwriting it."""
diff = difflib.unified_diff(
old_text.splitlines(keepends=True),
new_text.splitlines(keepends=True),
fromfile=f"{dst} (current)",
tofile=f"{dst} (proposed)",
)
for line in diff:
sys.stderr.write(line if line.endswith("\n") else line + "\n")
sys.stderr.write(f"\nWrite these changes to {dst}? [y/N] ")
sys.stderr.flush()
try:
answer = input()
except EOFError:
return False
return answer.strip().lower() in ("y", "yes", "j", "ja")


def build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
prog="mda",
Expand Down Expand Up @@ -149,15 +170,15 @@ def build_parser() -> argparse.ArgumentParser:
default=None,
help=(
"Supported file or directory (.md, .pdf, .docx, .doc, .txt) for batch mode. "
"Omit to open the interactive UI, or pass with --tui as the folder-picker start path."
"Omit to open the interactive browser, or pass with --tui as its starting path."
),
)
p.add_argument(
"--tui",
"-T",
action="store_true",
help=(
"Open the interactive terminal UI (folder picker, then file browser). "
"Open the interactive file browser (Enter opens folders or runs MDA on a file). "
"This is the default when no target path is given."
),
)
Expand All @@ -177,7 +198,26 @@ def build_parser() -> argparse.ArgumentParser:
p.add_argument(
"--in-place",
action="store_true",
help="Overwrite each source file with the restructured output.",
help=(
"Write the result directly to each source file. Shows a diff and asks for "
"confirmation per file (skip with --yes); the original is always backed up "
"outside the vault first (see --backup-dir)."
),
)
p.add_argument(
"-y",
"--yes",
action="store_true",
help="With --in-place: write without the per-file prompt (content already confirmed).",
)
p.add_argument(
"--backup-dir",
type=Path,
default=None,
help=(
"Where --in-place saves the original before overwriting "
"(default: MDA_BACKUP_DIR or ~/home/backups-obsidian)."
),
)
p.add_argument(
"--dry-run",
Expand Down Expand Up @@ -255,7 +295,7 @@ def build_parser() -> argparse.ArgumentParser:
p.add_argument(
"--backup",
action="store_true",
help="When used with --in-place in batch mode, keep a sibling .bak copy before overwrite.",
help="Deprecated no-op: --in-place always backs up to the backup dir before overwrite.",
)
p.add_argument(
"-q",
Expand Down Expand Up @@ -336,6 +376,10 @@ def main(argv: list[str] | None = None) -> int:
parser.error("--in-place and --out-dir are mutually exclusive")
if args.backup and not args.in_place:
parser.error("--backup requires --in-place")
if args.yes and not args.in_place:
parser.error("--yes requires --in-place")
if args.backup_dir is not None and not args.in_place:
parser.error("--backup-dir requires --in-place")

max_attempts = 1 if args.no_retry else max(1, args.max_retries)

Expand Down Expand Up @@ -399,6 +443,18 @@ def main(argv: list[str] | None = None) -> int:
print(f"{j.src} -> {j.dst}")
return 0

backup_dir = resolve_backup_dir(args.backup_dir) if args.in_place else None
confirm = None
if args.in_place and not args.yes:
if not sys.stdin.isatty():
parser.error(
"--in-place asks for confirmation per file and stdin is not a terminal; "
"run it interactively, or pass --yes when the content is already confirmed"
)
confirm = confirm_in_place_write
if backup_dir is not None and not args.json_lines:
print(f"In-place backups: {backup_dir}", file=sys.stderr)

provider = resolve_provider_name(cli_provider=args.provider)
ensure_api_credentials(provider)

Expand All @@ -424,7 +480,9 @@ def main(argv: list[str] | None = None) -> int:
system=system,
progress=not args.quiet,
max_attempts=max_attempts,
backup_in_place=args.backup,
backup_in_place=args.in_place,
backup_dir=backup_dir,
confirm=confirm,
)
if args.json_lines:
print_json_line(job_result_record(result, provider=ctx.provider))
Expand Down
100 changes: 94 additions & 6 deletions mda_cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import hashlib
import json
import os
import shutil
Expand All @@ -10,6 +11,7 @@
import time
from collections.abc import Callable, Sequence
from dataclasses import dataclass
from datetime import datetime
from importlib.resources import files as importlib_files
from pathlib import Path
from typing import Literal
Expand Down Expand Up @@ -45,6 +47,8 @@
DEFAULT_MODEL = "claude-sonnet-4-20250514"
DEFAULT_MAX_TOKENS = 16_000
DEFAULT_API_TIMEOUT = 600.0
# In-place writes always snapshot the original here first (override: MDA_BACKUP_DIR / --backup-dir).
DEFAULT_BACKUP_DIR = Path.home() / "home" / "backups-obsidian"
LARGE_FILE_BYTES = 400_000
LARGE_BATCH_TOTAL_BYTES = 2_000_000

Expand Down Expand Up @@ -163,7 +167,7 @@ class Job:
class JobResult:
job: Job
ok: bool
action: Literal["wrote", "unchanged", "skipped-empty", "failed"]
action: Literal["wrote", "unchanged", "skipped-empty", "declined", "failed"]
error: str | None = None
changed: bool = False
backup_path: Path | None = None
Expand Down Expand Up @@ -484,6 +488,47 @@ def next_backup_path(path: Path) -> Path:
n += 1


class WriteDeclined(Exception):
"""The user did not confirm an in-place overwrite; nothing was written."""


ConfirmWrite = Callable[[Path, str, str], bool]


def sha256_hex(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()


def resolve_backup_dir(cli_backup_dir: Path | None = None) -> Path:
"""Backup root for in-place writes: ``--backup-dir`` → ``MDA_BACKUP_DIR`` → default."""
if cli_backup_dir is not None:
return Path(cli_backup_dir).expanduser()
env = os.environ.get("MDA_BACKUP_DIR", "").strip()
if env:
return Path(env).expanduser()
return DEFAULT_BACKUP_DIR


def find_vault_root(path: Path) -> Path | None:
"""Nearest ancestor of ``path`` that holds an ``.obsidian`` folder, if any."""
for parent in path.resolve().parents:
if (parent / ".obsidian").is_dir():
return parent
return None


def central_backup_path(path: Path, backup_dir: Path, data: bytes) -> Path:
"""``<backup_dir>/<YYYYMMDD>/<vault>/<path in vault>.<sha256[:8]>.bak`` (outside the vault)."""
resolved = path.resolve()
root = find_vault_root(resolved)
if root is not None:
rel = Path(root.name) / resolved.relative_to(root)
else:
rel = Path(resolved.parent.name or "files") / resolved.name
day = datetime.now().strftime("%Y%m%d")
return backup_dir / day / rel.parent / f"{rel.name}.{sha256_hex(data)[:8]}.bak"


def _write_text_atomic(path: Path, text: str) -> None:
"""Write ``text`` to ``path`` atomically on the destination filesystem."""
path.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -520,8 +565,17 @@ def write_output_text(
text: str,
*,
create_backup: bool = False,
backup_dir: Path | None = None,
confirm: ConfirmWrite | None = None,
expected_sha256: str | None = None,
) -> tuple[bool, Path | None, int]:
"""Persist output text safely, returning ``(changed, backup_path, bytes_out)``."""
"""Persist output text safely, returning ``(changed, backup_path, bytes_out)``.

When overwriting an existing file: ``expected_sha256`` aborts if the file changed since it
was read, ``confirm(dst, old_text, new_text)`` must return true (else ``WriteDeclined``),
and ``create_backup`` snapshots the original to ``backup_dir`` (sibling ``.bak`` when no
``backup_dir`` is given). The written bytes are read back and compared.
"""
bytes_out = len(text.encode("utf-8"))
if dst.exists() and dst.is_file():
try:
Expand All @@ -533,11 +587,29 @@ def write_output_text(
pass

backup_path: Path | None = None
if create_backup and dst.exists() and dst.is_file():
backup_path = next_backup_path(dst)
shutil.copy2(dst, backup_path)
if dst.exists() and dst.is_file():
original = dst.read_bytes()
if expected_sha256 is not None and sha256_hex(original) != expected_sha256:
raise OSError(f"{dst} changed since it was read; not overwritten")
if confirm is not None:
old_text = read_markdown_text(dst)
if not confirm(dst, old_text, text):
raise WriteDeclined(str(dst))
if sha256_hex(dst.read_bytes()) != sha256_hex(original):
raise OSError(f"{dst} changed while waiting for confirmation; not overwritten")
if create_backup:
if backup_dir is not None:
backup_path = central_backup_path(dst, backup_dir, original)
backup_path.parent.mkdir(parents=True, exist_ok=True)
else:
backup_path = next_backup_path(dst)
shutil.copy2(dst, backup_path)
if backup_path.read_bytes() != original:
raise OSError(f"backup verification failed: {backup_path}")

_write_text_atomic(dst, text)
if dst.read_bytes() != text.encode("utf-8"):
raise OSError(f"written content does not match the confirmed content: {dst}")
return True, backup_path, bytes_out


Expand Down Expand Up @@ -599,6 +671,8 @@ def process_job(
progress_writer: Callable[[str], None] | None = None,
max_attempts: int = 3,
backup_in_place: bool = False,
backup_dir: Path | None = None,
confirm: ConfirmWrite | None = None,
) -> JobResult:
def _emit(msg: str) -> None:
if log is not None:
Expand All @@ -619,6 +693,7 @@ def _emit(msg: str) -> None:
)

bytes_in = len(raw.encode("utf-8"))
src_sha256 = sha256_hex(job.src.read_bytes())
if not raw.strip():
_emit(f" skip (empty): {job.src}")
return JobResult(
Expand Down Expand Up @@ -662,11 +737,24 @@ def _emit(msg: str) -> None:
)

output = strip_outer_fence(output)
in_place = job.src.resolve() == job.dst.resolve()
try:
changed, backup_path, bytes_out = write_output_text(
job.dst,
output,
create_backup=backup_in_place and job.src.resolve() == job.dst.resolve(),
create_backup=backup_in_place and in_place,
backup_dir=backup_dir if in_place else None,
confirm=confirm if in_place else None,
expected_sha256=src_sha256 if in_place else None,
)
except WriteDeclined:
_emit(f" declined (not written): {job.dst}")
return JobResult(
job=job,
ok=True,
action="declined",
changed=False,
bytes_in=bytes_in,
)
except OSError as e:
msg = f"write failed: {e}"
Expand Down
28 changes: 26 additions & 2 deletions mda_cli/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import urllib.request
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from typing import Literal, Protocol

from mda_cli.core import (
DEFAULT_API_TIMEOUT,
DEFAULT_MAX_TOKENS,
DEFAULT_MODEL,
ConfirmWrite,
friendly_api_message as _friendly_anthropic,
strip_outer_fence,
)
Expand Down Expand Up @@ -436,9 +438,17 @@ def process_job_with_provider(
progress_writer: Callable[[str], None] | None = None,
max_attempts: int = 3,
backup_in_place: bool = False,
backup_dir: Path | None = None,
confirm: ConfirmWrite | None = None,
):
"""Same contract as ``core.process_job`` but provider-aware."""
from mda_cli.core import JobResult, read_markdown_text, write_output_text
from mda_cli.core import (
JobResult,
WriteDeclined,
read_markdown_text,
sha256_hex,
write_output_text,
)

def _emit(msg: str) -> None:
if log is not None:
Expand All @@ -448,6 +458,7 @@ def _emit(msg: str) -> None:

_emit(f"-> {job.src}")
raw = read_markdown_text(job.src)
src_sha256 = sha256_hex(job.src.read_bytes())
bytes_in = len(raw.encode("utf-8"))
if not raw.strip():
_emit(f" skip (empty): {job.src}")
Expand Down Expand Up @@ -492,11 +503,24 @@ def _emit(msg: str) -> None:
)

output = strip_outer_fence(output)
in_place = job.src.resolve() == job.dst.resolve()
try:
changed, backup_path, bytes_out = write_output_text(
job.dst,
output,
create_backup=backup_in_place and job.src.resolve() == job.dst.resolve(),
create_backup=backup_in_place and in_place,
backup_dir=backup_dir if in_place else None,
confirm=confirm if in_place else None,
expected_sha256=src_sha256 if in_place else None,
)
except WriteDeclined:
_emit(f" declined (not written): {job.dst}")
return JobResult(
job=job,
ok=True,
action="declined",
changed=False,
bytes_in=bytes_in,
)
except OSError as e:
msg = f"write failed: {e}"
Expand Down
Loading