|
| 1 | +# Plan: Clipboard Copy Feature |
| 2 | + |
| 3 | +Add `--copy/-c` and `--copy-only` flags to copy output to system clipboard. |
| 4 | + |
| 5 | +## Design |
| 6 | + |
| 7 | +- **No external dependencies** — use native OS tools (pbcopy, xclip, wl-copy) |
| 8 | +- **Error handling** — warning to stderr, exit 0 (non-critical) |
| 9 | +- **Feedback** — `Copied to clipboard (12.5 KB)` to stderr |
| 10 | + |
| 11 | +## Flag Behavior |
| 12 | + |
| 13 | +| Flags | stdout | file | clipboard | |
| 14 | +|-------|--------|------|-----------| |
| 15 | +| (none) | ✓ | - | - | |
| 16 | +| `-o file` | - | ✓ | - | |
| 17 | +| `-c` / `--copy` | ✓ | - | ✓ | |
| 18 | +| `--copy -o file` | - | ✓ | ✓ | |
| 19 | +| `--copy-only` | - | - | ✓ | |
| 20 | + |
| 21 | +## Detection Logic |
| 22 | + |
| 23 | +``` |
| 24 | +macOS: pbcopy |
| 25 | +Windows: clip.exe |
| 26 | +Linux (Wayland): wl-copy |
| 27 | +Linux (X11): xclip -selection clipboard |
| 28 | +``` |
| 29 | + |
| 30 | +## Implementation |
| 31 | + |
| 32 | +### NEW: `src/treemapper/clipboard.py` |
| 33 | + |
| 34 | +```python |
| 35 | +import os |
| 36 | +import platform |
| 37 | +import shutil |
| 38 | +import subprocess |
| 39 | + |
| 40 | +class ClipboardError(Exception): |
| 41 | + pass |
| 42 | + |
| 43 | +def detect_clipboard_command() -> list[str] | None: |
| 44 | + system = platform.system() |
| 45 | + if system == "Darwin": |
| 46 | + return ["pbcopy"] |
| 47 | + if system == "Windows": |
| 48 | + return ["clip"] |
| 49 | + if system in ("Linux", "FreeBSD"): |
| 50 | + if os.environ.get("WAYLAND_DISPLAY") and shutil.which("wl-copy"): |
| 51 | + return ["wl-copy"] |
| 52 | + if os.environ.get("DISPLAY") and shutil.which("xclip"): |
| 53 | + return ["xclip", "-selection", "clipboard"] |
| 54 | + return None |
| 55 | + |
| 56 | +def copy_to_clipboard(text: str) -> None: |
| 57 | + cmd = detect_clipboard_command() |
| 58 | + if cmd is None: |
| 59 | + raise ClipboardError("No clipboard tool found") |
| 60 | + proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
| 61 | + _, stderr = proc.communicate(input=text.encode("utf-8"), timeout=5) |
| 62 | + if proc.returncode != 0: |
| 63 | + raise ClipboardError(stderr.decode()) |
| 64 | + |
| 65 | +def clipboard_available() -> bool: |
| 66 | + return detect_clipboard_command() is not None |
| 67 | +``` |
| 68 | + |
| 69 | +### UPDATE: `cli.py` |
| 70 | + |
| 71 | +```python |
| 72 | +# ParsedArgs |
| 73 | +copy: bool |
| 74 | +copy_only: bool |
| 75 | + |
| 76 | +# Arguments |
| 77 | +parser.add_argument("-c", "--copy", action="store_true") |
| 78 | +parser.add_argument("--copy-only", action="store_true") |
| 79 | +``` |
| 80 | + |
| 81 | +### UPDATE: `treemapper.py` |
| 82 | + |
| 83 | +```python |
| 84 | +if args.copy: |
| 85 | + try: |
| 86 | + copy_to_clipboard(output_content) |
| 87 | + print(f"Copied to clipboard ({len(output_content) / 1024:.1f} KB)", file=sys.stderr) |
| 88 | + except ClipboardError as e: |
| 89 | + logging.warning(f"Clipboard: {e}") |
| 90 | + |
| 91 | +if args.copy_only and args.output_file is None: |
| 92 | + return |
| 93 | +``` |
| 94 | + |
| 95 | +## Testing |
| 96 | + |
| 97 | +Skip clipboard tests in CI (no display): |
| 98 | + |
| 99 | +```python |
| 100 | +@pytest.mark.skipif(os.environ.get("CI") or not os.environ.get("DISPLAY")) |
| 101 | +def test_clipboard_roundtrip(): |
| 102 | + pass |
| 103 | +``` |
0 commit comments