Skip to content

Commit 939b3cb

Browse files
authored
Add clipboard support (#2)
1 parent 41841ae commit 939b3cb

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Options:
2424
-a, --aa_factor FLOAT Antialias factor
2525
-s, --style TEXT
2626
-l, --lang TEXT
27+
-c, --clipboard Copy image to clipboard
2728
-f, --image_format [png|jpeg|bmp|gif]
2829
Antialias factor
2930
-o, --output FILE Output path for image

src/codepic/cli.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import io
22
import os
33
import sys
4+
import tempfile
5+
from subprocess import run
46

57
import click
68
from PIL import Image
@@ -26,6 +28,7 @@
2628
@click.option('-a', '--aa_factor', type=float, default=1, help='Antialias factor')
2729
@click.option('-s', '--style', type=str, default='one-dark')
2830
@click.option('-l', '--lang', type=str)
31+
@click.option('-c', '--clipboard', is_flag=True, help='Copy image to clipboard')
2932
@click.option(
3033
'-f',
3134
'--image_format',
@@ -65,32 +68,37 @@ def cli(
6568
image_format: str | None,
6669
style: str,
6770
lang: str | None,
71+
clipboard: bool,
6872
):
6973
code = ''
7074

7175
if font_name is None:
7276
font_name = ''
7377

74-
if not image_format and output:
75-
ext = os.path.splitext(source_file)[1]
76-
if ext:
77-
ext = ext.lower()
78-
if ext in ['png', 'jpeg', 'jpg', 'bmp', 'gif']:
79-
image_format = ext
80-
if image_format == 'jpg':
81-
image_format = 'jpeg'
82-
8378
if not image_format:
8479
image_format = 'png'
80+
if output:
81+
ext = os.path.splitext(source_file)[1]
82+
if ext:
83+
ext = ext.lower()
84+
if ext in ['png', 'jpeg', 'jpg', 'bmp', 'gif']:
85+
image_format = ext
86+
if image_format == 'jpg':
87+
image_format = 'jpeg'
88+
89+
if clipboard and image_format != 'png':
90+
exit('Image format must be png to use clipboard')
8591

8692
write_to_stdout = False
8793
if output == '-':
8894
write_to_stdout = True
95+
8996
elif not output:
90-
if source_file == '-':
91-
write_to_stdout = True
92-
else:
93-
output = os.path.splitext(source_file)[0] + '.' + image_format.lower()
97+
if not clipboard:
98+
if source_file == '-':
99+
write_to_stdout = True
100+
else:
101+
output = os.path.splitext(source_file)[0] + '.' + image_format.lower()
94102

95103
formatter = ImageFormatter(
96104
font_name=font_name,
@@ -141,18 +149,21 @@ def cli(
141149
if height.endswith('%'):
142150
perc = int(height[:-1]) / 100
143151
height = int(img.height * perc)
152+
144153
else:
145154
height = int(height)
146155

147156
if width:
148157
if width.endswith('%'):
149158
perc = int(width[:-1]) / 100
150159
width = int(img.width * perc)
160+
151161
else:
152162
width = int(width)
153163

154164
if not width and height:
155165
width = int(height / aspect)
166+
156167
if not height and width:
157168
height = int(width * aspect)
158169

@@ -162,9 +173,17 @@ def cli(
162173
buff = io.BytesIO()
163174
img.save(buff, format='PNG')
164175

176+
buff = buff.getbuffer()
177+
178+
if clipboard:
179+
with tempfile.NamedTemporaryFile('wb', delete=True) as fp:
180+
fp.write(buff)
181+
run(f'xclip -selection clipboard -target image/png < {fp.name}', shell=True)
182+
fp.flush()
183+
165184
if write_to_stdout:
166-
sys.stdout.buffer.write(buff.getbuffer())
185+
sys.stdout.buffer.write(buff)
167186

168-
else:
187+
elif output and output != '-':
169188
with open(output, 'wb') as f:
170-
f.write(buff.getbuffer())
189+
f.write(buff)

0 commit comments

Comments
 (0)