3030config = config_parser ["pyazo" ]
3131tmp_file = os .path .join (tempfile .gettempdir (), "screenshot.png" )
3232
33+ STDOUT_TOOLS = ("hyprshot" , "grimshot" , "maim" )
3334OS_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):
6162def 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
8494def 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)
181191def main (
182192 private : bool ,
0 commit comments