Skip to content

Commit 8017b1e

Browse files
authored
Merge pull request #22 from jlowin/print
don't copy when printing to screen
2 parents 1815b4f + e481790 commit 8017b1e

File tree

3 files changed

+133
-94
lines changed

3 files changed

+133
-94
lines changed

src/copychat/cli.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ def main(
433433
error_console.print(
434434
f"Output {'appended' if append else 'written'} to [green]{outfile}[/]"
435435
)
436-
# Handle clipboard only if not writing to file
437-
else:
436+
# Only use clipboard if not writing to file AND not just printing to stdout
437+
elif not print_output or append:
438438
if append:
439439
try:
440440
existing_clipboard = pyperclip.paste()
@@ -444,13 +444,24 @@ def main(
444444
"[yellow]Warning: Could not read clipboard for append[/]"
445445
)
446446

447-
pyperclip.copy(result)
448-
# Calculate total lines outside the f-string
449-
total_lines = sum(f.content.count("\n") + 1 for f in format_result.files)
450-
error_console.print(
451-
f"{'Appended' if append else 'Copied'} to clipboard "
452-
f"(~{format_result.total_tokens:,} tokens, {total_lines:,} lines)"
453-
)
447+
try:
448+
pyperclip.copy(result)
449+
# Calculate total lines outside the f-string
450+
total_lines = sum(
451+
f.content.count("\n") + 1 for f in format_result.files
452+
)
453+
error_console.print(
454+
f"{'Appended' if append else 'Copied'} to clipboard "
455+
f"(~{format_result.total_tokens:,} tokens, {total_lines:,} lines)"
456+
)
457+
except Exception as e:
458+
error_console.print(
459+
f"[yellow]Warning: Could not copy to clipboard: {str(e)}[/]"
460+
)
461+
if not print_output:
462+
# If clipboard failed and we're not printing, show the content
463+
error_console.print("[cyan]Content would have been:[/]")
464+
print(result)
454465

455466
# Print to stdout only if explicitly requested
456467
if print_output:

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
from pathlib import Path
66

7-
runner = CliRunner(mix_stderr=False)
7+
runner = CliRunner()
88

99

1010
def strip_ansi(text: str) -> str:
@@ -259,7 +259,7 @@ def mock_copy(text):
259259

260260
def test_cli_github_item_basic(monkeypatch):
261261
"""Basic test for GitHub item handling that doesn't rely on internal implementation."""
262-
runner = CliRunner(mix_stderr=False)
262+
runner = CliRunner()
263263

264264
# Instead of mocking complex internals, just provide a simple mock for the scan_directory function
265265
# so it returns a known result when the CLI processes a GitHub item

0 commit comments

Comments
 (0)