Files
FWS-Kitty/kittens/file_manager/main.py
T
BlackAngelTV 4fb1402d9e feat(file-manager): open files in a full-window editor tab
Enter on a file handed the editor command to the shell, so the panel closed
and had to be reopened, and a docked panel ran the editor squeezed into the
dock. Delegate to kitty's edit protocol instead: the editor opens in a new
tab with the whole window and the panel is redrawn with the same selection
when it exits.

The editor now comes from kitty's editor option. file-manager's editor,
open_with_sudo and --open-here are kept but ignored so existing configs and
mappings keep loading.
2026-11-06 08:41:18 +01:00

218 lines
7.0 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.
""",
)
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 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(
'Change shell directory or open selection',
'activate enter activate',
long_text="""
When the selection is a directory it becomes the working directory of the shell
that launched the panel. 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:`activate <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('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')
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
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