Keep the file manager panel and the shell in the same directory in both directions, through the shell integration instead of typing commands.
270 lines
9.2 KiB
Python
270 lines
9.2 KiB
Python
#!/usr/bin/env python
|
|
# License: GPL v3 Copyright: 2026, FWS
|
|
|
|
import shlex
|
|
import sys
|
|
from functools import partial
|
|
from typing import Any
|
|
|
|
from kitty.conf.types import Definition
|
|
from kitty.constants import appname
|
|
from kitty.simple_cli_definitions import CONFIG_HELP, CompletionSpec
|
|
from kitty.typing_compat import BossType
|
|
|
|
from ..tui.handler import result_handler
|
|
|
|
definition = Definition(
|
|
'!kittens.file_manager',
|
|
)
|
|
|
|
agr = definition.add_group
|
|
egr = definition.end_group
|
|
opt = definition.add_option
|
|
map = definition.add_map
|
|
|
|
agr('appearance', 'Appearance')
|
|
|
|
opt(
|
|
'show_hidden',
|
|
'no',
|
|
option_type='to_bool',
|
|
long_text="""
|
|
Show entries whose name starts with a full stop when the panel opens. The setting
|
|
can be toggled at runtime with the :ref:`toggle_hidden <file-manager-shortcuts>`
|
|
shortcut.
|
|
""",
|
|
)
|
|
|
|
opt(
|
|
'sort_by',
|
|
'name',
|
|
long_text="""
|
|
How to order entries. :code:`name` lists directories first, then files, both
|
|
sorted by name ignoring case. :code:`mtime` lists directories first, then files
|
|
sorted by last modification time, most recent first. :code:`size` lists
|
|
directories first, then files sorted by size, largest first.
|
|
""",
|
|
)
|
|
|
|
egr()
|
|
|
|
agr('shell', 'Shell synchronisation')
|
|
|
|
opt(
|
|
'sync_cwd',
|
|
'yes',
|
|
option_type='to_bool',
|
|
long_text="""
|
|
Keep the shell's working directory and the panel in step. A :code:`cd` in the
|
|
shell moves the panel to the same directory, and walking through the panel moves
|
|
the shell to the directory being browsed at its next prompt. Quitting the panel
|
|
without validating a directory withdraws a request the shell has not read yet.
|
|
|
|
The exchange goes through two small files in the session directory kitty
|
|
exports as :envvar:`KITTY_CWD_SYNC_DIR`, so nothing is typed into the shell:
|
|
the command never lands in the history and a half typed multi-line command is
|
|
left untouched. Bash, zsh and fish are supported by their kitty shell
|
|
integration; other shells keep the current behaviour. Set :code:`sync_cwd no`
|
|
to keep the two directories independent.
|
|
""",
|
|
)
|
|
|
|
egr()
|
|
|
|
agr('opening', 'Opening files')
|
|
|
|
opt(
|
|
'editor',
|
|
'',
|
|
long_text="""
|
|
Deprecated and ignored. Files are opened through kitty's edit protocol, so the
|
|
editor is the :opt:`editor` option of :file:`kitty.conf`, with :envvar:`VISUAL`
|
|
then :envvar:`EDITOR` used when it is :code:`.` (the default). The option is
|
|
still accepted so that existing configuration files keep loading.
|
|
""",
|
|
)
|
|
|
|
opt(
|
|
'open_with_sudo',
|
|
'no',
|
|
option_type='to_bool',
|
|
long_text="""
|
|
Deprecated and ignored. kitty's edit protocol sends the file back when the
|
|
editor exits and therefore needs a file the user can write to. Open privileged
|
|
files with :code:`sudo -e` from the shell instead.
|
|
""",
|
|
)
|
|
|
|
egr()
|
|
|
|
agr('shortcuts', 'Keyboard shortcuts')
|
|
|
|
map('Quit without changing directory', 'quit q quit')
|
|
map('Quit without changing directory', 'quit esc quit')
|
|
map('Move selection up', 'move_up up move_up')
|
|
map('Move selection up', 'move_up --allow-fallback=shifted,ascii k move_up')
|
|
map('Move selection down', 'move_down down move_down')
|
|
map('Move selection down', 'move_down --allow-fallback=shifted,ascii j move_down')
|
|
map('Move selection up', 'move_up shift+tab move_up')
|
|
map('Move selection down', 'move_down tab move_down')
|
|
map('Move selection up by a page', 'page_up page_up page_up')
|
|
map('Move selection up by a page', 'page_up --allow-fallback=shifted,ascii ctrl+b page_up')
|
|
map('Move selection down by a page', 'page_down page_down page_down')
|
|
map('Move selection down by a page', 'page_down --allow-fallback=shifted,ascii ctrl+f page_down')
|
|
map('Select first entry', 'home home home')
|
|
map('Select first entry', 'home --allow-fallback=shifted,ascii g home')
|
|
map('Select last entry', 'end end end')
|
|
map('Select last entry', 'end shift+g end')
|
|
map(
|
|
'Open the selection',
|
|
'activate enter activate',
|
|
long_text="""
|
|
When the selection is a directory it is opened in the panel, exactly like the
|
|
:ref:`enter_dir <file-manager-shortcuts>` shortcut. When it is a file, the panel
|
|
opens it in kitty's editor (:opt:`editor`, :envvar:`VISUAL` or
|
|
:envvar:`EDITOR`) in a new tab, and is drawn again with the same selection once
|
|
the editor exits. The new tab is what gives the file a whole window even when
|
|
the panel runs in a dock.
|
|
""",
|
|
)
|
|
map(
|
|
'Browse into the selected directory',
|
|
'enter_dir right enter_dir',
|
|
long_text="""
|
|
Move the panel into the selected directory without changing the shell's working
|
|
directory. Use the :ref:`cd_here <file-manager-shortcuts>` shortcut to also
|
|
change the shell's directory.
|
|
""",
|
|
)
|
|
map('Browse into the selected directory', 'enter_dir --allow-fallback=shifted,ascii l enter_dir')
|
|
map('Browse into the parent directory', 'parent_dir left parent_dir')
|
|
map('Browse into the parent directory', 'parent_dir backspace parent_dir')
|
|
map('Browse into the parent directory', 'parent_dir --allow-fallback=shifted,ascii h parent_dir')
|
|
map(
|
|
'Change shell directory to the directory being browsed',
|
|
'cd_here ctrl+enter cd_here',
|
|
long_text="""
|
|
Changes the shell's working directory to the directory currently shown in the
|
|
panel, regardless of the selection.
|
|
""",
|
|
)
|
|
map('Change shell directory to the directory being browsed', 'cd_here --allow-fallback=shifted,ascii c cd_here')
|
|
map('Toggle display of hidden entries', 'toggle_hidden --allow-fallback=shifted,ascii . toggle_hidden')
|
|
map('Create a new file', 'create_file --allow-fallback=shifted,ascii n create_file')
|
|
map('Create a new directory', 'create_dir shift+n create_dir')
|
|
map('Create a new directory', 'create_dir --allow-fallback=shifted,ascii a create_dir')
|
|
map('Rename the selected entry', 'rename --allow-fallback=shifted,ascii r rename')
|
|
map(
|
|
'Delete the selected entry',
|
|
'delete --allow-fallback=shifted,ascii d delete',
|
|
long_text="""
|
|
Deletes a file, a symbolic link or an empty directory after asking for
|
|
confirmation. Directories are never deleted recursively.
|
|
""",
|
|
)
|
|
map('Copy the path of the selection to the clipboard', 'copy_path --allow-fallback=shifted,ascii y copy_path')
|
|
map('Reload the current directory', 'refresh --allow-fallback=shifted,ascii ctrl+r refresh')
|
|
map(
|
|
'Filter the listing with a fuzzy query',
|
|
'search --allow-fallback=shifted,ascii / search',
|
|
long_text="""
|
|
Filters the listing as text is typed, using the same fuzzy matcher as the
|
|
command palette. :kbd:`Entrée` opens the selection, :kbd:`Échap` clears the
|
|
query and restores the full listing.
|
|
""",
|
|
)
|
|
map(
|
|
'List and run the actions of the panel',
|
|
'palette --allow-fallback=shifted,ascii : palette',
|
|
long_text="""
|
|
Lists every action of the panel with its description, filtered by a fuzzy query,
|
|
and runs the selected one with :kbd:`Entrée`. The same actions can be bound to
|
|
dedicated shortcuts in :file:`file-manager.conf`.
|
|
""",
|
|
)
|
|
map('Open the selected file in a new tab', 'open_tab --allow-fallback=shifted,ascii o open_tab')
|
|
map('Open the selected file in a new OS window', 'open_window --allow-fallback=shifted,ascii t open_window')
|
|
|
|
egr()
|
|
|
|
OPTIONS = partial(
|
|
"""\
|
|
--start-dir
|
|
Directory to start browsing in. Defaults to the current working directory.
|
|
|
|
|
|
--show-hidden
|
|
type=bool-set
|
|
Show hidden files when the panel opens.
|
|
|
|
|
|
--open-here
|
|
type=bool-set
|
|
Deprecated and ignored. Files are always opened in a new tab, so this flag is
|
|
only accepted so that existing mappings keep working.
|
|
|
|
|
|
--config -c
|
|
type=list
|
|
completion=type:file ext:conf group:"Config files" kwds:none,NONE
|
|
{config_help}
|
|
|
|
|
|
--override -o
|
|
type=list
|
|
Override individual configuration options, can be specified multiple times.
|
|
Syntax: :italic:`name=value`. For example: :italic:`-o show_hidden=yes`
|
|
|
|
""".format,
|
|
config_help=CONFIG_HELP.format(conf_name='file-manager', appname=appname),
|
|
)
|
|
|
|
|
|
def main(args: list[str]) -> None:
|
|
raise SystemExit('This kitten must be run from a kitty.conf mapping')
|
|
|
|
|
|
def change_directory(w: Any, path: str) -> None:
|
|
# Writing to the pty, unlike pasting, makes the shell run the command. When
|
|
# the shell is busy, only paste the text so that it can be edited instead of
|
|
# feeding it into whatever program is running.
|
|
cmd = 'cd ' + shlex.quote(path)
|
|
if w.at_prompt:
|
|
w.write_to_child(cmd + '\r')
|
|
else:
|
|
w.paste_text(cmd)
|
|
|
|
|
|
@result_handler(has_ready_notification=True)
|
|
def handle_result(args: list[str], data: dict[str, Any], target_window_id: int, boss: BossType) -> None:
|
|
if not data:
|
|
return
|
|
action, path = data.get('action'), data.get('path')
|
|
if action != 'cd' or not path:
|
|
return
|
|
if data.get('sync'):
|
|
# The panel published the directory in the session file and the shell
|
|
# picks it up from its prompt hook, see the sync_cwd option.
|
|
return
|
|
w = boss.window_id_map.get(target_window_id)
|
|
if w is None:
|
|
boss.ring_bell_if_allowed()
|
|
return
|
|
change_directory(w, path)
|
|
|
|
|
|
help_text = 'Browse directories in a side panel and change the working directory of the shell without leaving it'
|
|
usage = '[directory]'
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv)
|
|
elif __name__ == '__doc__':
|
|
cd = sys.cli_docs # type: ignore
|
|
cd['usage'] = usage
|
|
cd['options'] = OPTIONS
|
|
cd['help_text'] = help_text
|
|
cd['short_desc'] = 'Browse directories and change the shell directory'
|
|
cd['args_completion'] = CompletionSpec.from_string('type:directory')
|
|
elif __name__ == '__conf__':
|
|
sys.options_definition = definition # type: ignore
|