macOS: Allow opening executables without a file extension with kitty as well

Fixes #5160
This commit is contained in:
Kovid Goyal 2022-06-02 09:25:30 +05:30
parent 55eeb9c11e
commit b937033411
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 27 additions and 33 deletions

View file

@ -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`)

View file

@ -144,33 +144,7 @@ You can customize these actions by creating a :file:`launch-actions.conf` file
in the :ref:`kitty config directory <confloc>`, 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()))

View file

@ -2,6 +2,7 @@
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
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

View file

@ -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/*