Skip to content

Commit

Permalink
Fix for issue 76 - work around pyperclip issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jima80525 committed Aug 18, 2020
1 parent 871beb7 commit 7c492f9
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions markplates/__main__.py
Expand Up @@ -264,11 +264,24 @@ def main(verbose, clip, template):
try:
output = process_template(pathlib.Path(template))
print(output)
sys.stdout.flush()
if clip:
# copy lines to clipboard, but skip the first title and the subsequent blank line
# copy lines to clipboard, but skip the first title and the
# subsequent blank line
lines = output.split("\n")
to_clip = "\n".join(lines[2:])
pyperclip.copy(to_clip)

# NOTE: there seems to be a bug in pyperclip that is emitting output
# to stdout when the clipboard gets too large. Redirecting stdout
# to devnull seems to resolve the issue (along with the flush()
# above). This is ugly, but works
fdnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(fdnull, 1)
try:
pyperclip.copy(to_clip)
finally:
os.close(fdnull)

except FileNotFoundError as e:
print(f"Unable to import file:{e.filename}", file=sys.stderr)
sys.exit(1)
Expand Down

0 comments on commit 7c492f9

Please sign in to comment.