diff --git a/openingithub/__init__.py b/openingithub/__init__.py index 2b98962..ad9b98a 100644 --- a/openingithub/__init__.py +++ b/openingithub/__init__.py @@ -1,18 +1,23 @@ -from subprocess import Popen -import platform -from fman import DirectoryPaneCommand, show_alert, load_json +import shutil +import subprocess +from fman import DirectoryPaneCommand, show_alert -PLUGIN_SETTINGS = load_json("OpenInGithub Settings.json")[0] -GITHUB_BINARY = PLUGIN_SETTINGS["github_binary"] -class OpenInGithub(DirectoryPaneCommand): - def __call__(self): - file_under_cursor = self.pane.get_file_under_cursor() - if file_under_cursor: - if platform.system() == "Windows": - Popen('"%s" %s' % (GITHUB_BINARY, file_under_cursor), shell=True) - else: - Popen('%s "%s"' % (GITHUB_BINARY, file_under_cursor), shell=True) +GITHUB_PATH = shutil.which("github") + +if GITHUB_PATH is None: + raise ValueError("Github command-line tool not found in PATH. Please install Github or add it to your PATH.") - else: - show_alert("No file selected.") +class OpenInGithub(DirectoryPaneCommand): + def __call__(self): + file_under_cursor_unf = self.pane.get_file_under_cursor() + file_under_cursor = file_under_cursor_unf if not file_under_cursor_unf.startswith("file://") else file_under_cursor_unf[7:] + + if not file_under_cursor: + show_alert("No file selected.") + return + try: + subprocess.run([GITHUB_PATH, file_under_cursor], shell=True) + + except subprocess.CalledProcessError: + show_alert("Failed to open file in Github.")