external: do not crash if command can't be found

This commit is contained in:
dece 2021-06-03 19:08:54 +02:00
parent 0a05d75e6b
commit 52716e66a7
3 changed files with 18 additions and 3 deletions

View file

@ -675,7 +675,10 @@ class Browser:
return return
command = self.config["source_editor"] + [source_filename] command = self.config["source_editor"] + [source_filename]
open_external_program(command) success = open_external_program(command)
if not success:
self.set_status_error("Could not open editor.")
if delete_source_after: if delete_source_after:
os.unlink(source_filename) os.unlink(source_filename)
self.refresh_windows() self.refresh_windows()

View file

@ -200,7 +200,9 @@ class CommandLine:
return return
command = self.editor_command + [temp_filepath] command = self.editor_command + [temp_filepath]
open_external_program(command) success = open_external_program(command)
if not success:
return
try: try:
with open(temp_filepath, "rt") as temp_file: with open(temp_filepath, "rt") as temp_file:

View file

@ -1,6 +1,7 @@
"""Call external commands.""" """Call external commands."""
import curses import curses
import logging
import subprocess import subprocess
@ -9,12 +10,21 @@ def open_external_program(command):
The caller has to refresh whatever windows it manages after calling this The caller has to refresh whatever windows it manages after calling this
method or garbage may be left on the screen. method or garbage may be left on the screen.
Returns:
True if no exception occured.
""" """
curses.nocbreak() curses.nocbreak()
curses.echo() curses.echo()
curses.curs_set(1) curses.curs_set(1)
result = True
try:
subprocess.run(command) subprocess.run(command)
except OSError as exc:
logging.error(f"Failed to run '{command}': {exc}")
result = False
curses.mousemask(curses.ALL_MOUSE_EVENTS) curses.mousemask(curses.ALL_MOUSE_EVENTS)
curses.curs_set(0) curses.curs_set(0)
curses.noecho() curses.noecho()
curses.cbreak() curses.cbreak()
return result