From b93703341112ced87bef77f6549592b0c7a05c33 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Jun 2022 09:25:30 +0530 Subject: [PATCH] macOS: Allow opening executables without a file extension with kitty as well Fixes #5160 --- docs/changelog.rst | 3 +++ docs/open_actions.rst | 34 ++++------------------------------ kitty/guess_mime_type.py | 18 +++++++++++++++--- kitty/open_actions.py | 5 +++++ 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index aaadfd28e..741e48442 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -50,6 +50,9 @@ Detailed list of changes - macOS: When pasting text and the clipboard has a filesystem path, paste the full path instead of the text, which is sometimes just the file name (:pull:`5142`) +- macOS: Allow opening executables without a file extension with kitty as well + (:iss:`5160`) + - Themes kitten: Add a tab to show user defined custom color themes separately (:pull:`5150`) diff --git a/docs/open_actions.rst b/docs/open_actions.rst index 96cd5bc50..6332ad68b 100644 --- a/docs/open_actions.rst +++ b/docs/open_actions.rst @@ -144,33 +144,7 @@ You can customize these actions by creating a :file:`launch-actions.conf` file in the :ref:`kitty config directory `, just like the :file:`open-actions.conf` file above. For example: -.. code:: conf - - # Open script files based on their shebangs - protocol file - ext sh,command,tool - action launch --hold --type=os-window kitty +shebang $FILE_PATH {SHELL} - - # Open shell specific script files - protocol file - ext fish,bash,zsh - action launch --hold --type=os-window kitty +shebang $FILE_PATH __ext__ - - # Open directories - protocol file - mime inode/directory - action launch --type=os-window --cwd $FILE_PATH - - # Open text files without fragments in the editor - protocol file - mime text/* - action launch --type=os-window $EDITOR $FILE_PATH - - # Open image files with icat - protocol file - mime image/* - action launch --type=os-window kitty +kitten icat --hold $FILE_PATH - - # Open ssh URLs with ssh command - protocol ssh - action launch --type=os-window ssh $URL +.. literalinclude:: ../kitty/open_actions.py + :language: conf + :start-at: # Open script files + :end-before: '''.splitlines())) diff --git a/kitty/guess_mime_type.py b/kitty/guess_mime_type.py index 5bd97bd46..9265144df 100644 --- a/kitty/guess_mime_type.py +++ b/kitty/guess_mime_type.py @@ -2,6 +2,7 @@ # License: GPLv3 Copyright: 2020, Kovid Goyal import os +import stat from contextlib import suppress from typing import Optional @@ -47,7 +48,15 @@ def initialize_mime_database() -> None: def guess_type(path: str, allow_filesystem_access: bool = False) -> Optional[str]: - if allow_filesystem_access and is_folder(path): + is_dir = is_exe = False + + if allow_filesystem_access: + with suppress(OSError): + st = os.stat(path) + is_dir = bool(stat.S_ISDIR(st.st_mode)) + is_exe = bool(not is_dir and st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) and os.access(path, os.X_OK)) + + if is_dir: return 'inode/directory' from mimetypes import guess_type as stdlib_guess_type initialize_mime_database() @@ -61,6 +70,9 @@ def guess_type(path: str, allow_filesystem_access: bool = False) -> Optional[str mt = f'text/{mt.split("/", 1)[-1]}' if not mt and is_rc_file(path): mt = 'text/plain' - if not mt and is_folder(path): - mt = 'inode/directory' + if not mt: + if is_dir: + mt = 'inode/directory' # type: ignore + elif is_exe: + mt = 'application/executable' return mt diff --git a/kitty/open_actions.py b/kitty/open_actions.py index 5495d9a30..b45e54b86 100644 --- a/kitty/open_actions.py +++ b/kitty/open_actions.py @@ -234,6 +234,11 @@ protocol file mime inode/directory action launch --type=os-window --cwd $FILE_PATH +# Open executable file +protocol file +mime application/executable,application/vnd.microsoft.portable-executable +action launch --hold --type=os-window $FILE_PATH + # Open text files without fragments in the editor protocol file mime text/*