Skip to content

Commit 3a07c80

Browse files
committed
Use stdout piping for some screenshot tools
1 parent f9a1287 commit 3a07c80

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pyazo_cli"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = ""
55
authors = [
66
{name = "Jelena Dokic", email = "jrubics@hacke.rs"},

src/pyazo_cli/__init__.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
config = config_parser["pyazo"]
3131
tmp_file = os.path.join(tempfile.gettempdir(), "screenshot.png")
3232

33+
STDOUT_TOOLS = ("hyprshot", "grimshot", "maim")
3334
OS_BACKENDS = {
3435
"Linux": {
35-
"maim": ["-s", "-n", "0", tmp_file],
36+
"maim": ["-s", "-n", "0"],
3637
"scrot": ["-s", tmp_file],
3738
"import": [tmp_file], # ImageMagick
38-
"grimshot": ["save", "area", tmp_file],
39+
"grimshot": ["save", "area", "-"],
3940
"spectacle": ["-b", "-r", "-n", "-o", tmp_file],
40-
"hyprshot": ["-m", "region", "-s", "-z", "-o", "/", "-f", tmp_file],
41+
"hyprshot": ["-m", "region", "-s", "-z", "-r", "png"],
4142
},
4243
"Darwin": {
4344
"screencapture": ["-i", tmp_file],
@@ -61,24 +62,33 @@ def notify(message: str, time: int):
6162
def make_screenshot():
6263
util = config.get("util")
6364
backends = OS_BACKENDS[platform.system()]
64-
if util is not None:
65-
args = backends[util]
66-
ret = run([util] + args)
67-
if ret.returncode != 0:
68-
message = "Error: Failed to take screenshot."
65+
if util is None:
66+
for util in backends.keys():
67+
if shutil.which(util) is not None:
68+
break
69+
else:
70+
message = "No screenshot tool available"
6971
print(message, file=sys.stderr)
7072
notify(message, 4000)
71-
exit(-1)
72-
else:
73-
for util, args in backends.items():
74-
if shutil.which(util) is not None and run([util] + args).returncode == 0:
75-
break
73+
exit(1)
74+
75+
args = backends[util]
76+
ret = run([util] + args, capture_output=True)
77+
if ret.returncode != 0:
78+
message = "Failed to take screenshot"
79+
print(message, file=sys.stderr)
80+
notify(message, 4000)
81+
exit(1)
82+
83+
if util in STDOUT_TOOLS:
84+
with open(tmp_file, "wb") as f:
85+
f.write(ret.stdout)
7686

7787
if not os.path.isfile(tmp_file):
78-
message = "Error: Failed to take screenshot."
88+
message = "Failed to take screenshot: No output file"
7989
print(message, file=sys.stderr)
8090
notify(message, 4000)
81-
exit(-1)
91+
exit(1)
8292

8393

8494
def upload_file(
@@ -100,10 +110,10 @@ def upload_file(
100110
)
101111

102112
if r.status_code >= 400:
103-
message = f"Error: Failed to upload screenshot. [{r.status_code}]"
113+
message = f"Failed to upload screenshot. [{r.status_code}]"
104114
print(message, file=sys.stderr)
105115
notify(message, 4000)
106-
exit(-2)
116+
exit(2)
107117

108118
url = f"{config.get('url')}/{r.json()['id']}"
109119

@@ -147,10 +157,10 @@ def delete_last_image() -> None:
147157
headers={"Authorization": f"Bearer {config.get('token')}"},
148158
)
149159
if r.status_code >= 400:
150-
message = f"Error: Failed to delete image. [{r.status_code}]"
160+
message = f"Failed to delete image. [{r.status_code}]"
151161
print(message, file=sys.stderr)
152162
notify(message, 4000)
153-
exit(-2)
163+
exit(2)
154164

155165

156166
@click.command()
@@ -166,17 +176,17 @@ def delete_last_image() -> None:
166176
@click.option(
167177
"--no-copy",
168178
is_flag=True,
169-
help="Don't copy the url to the clipboard after upload.",
179+
help="Don't copy the url to the clipboard after upload",
170180
)
171181
@click.option(
172182
"--no-output",
173183
is_flag=True,
174-
help="Don't print the url to stdout after upload.",
184+
help="Don't print the url to stdout after upload",
175185
)
176186
@click.option(
177187
"--no-save",
178188
is_flag=True,
179-
help="Don't save the file locally after upload.",
189+
help="Don't save the file locally after upload",
180190
)
181191
def main(
182192
private: bool,

0 commit comments

Comments
 (0)