#!/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 ` 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=""" Command used to open a file when the panel opens it itself, that is when :code:`--open-here` is given. When empty, :envvar:`VISUAL` then :envvar:`EDITOR` are used, falling back to :code:`vi`. kitty's own :opt:`editor` option is not visible to a panel started with :code:`launch`, so set this option when the panel runs in a dock and the two differ. """, ) opt( 'open_with_sudo', 'no', option_type='to_bool', long_text=""" Prefix the editor command with :code:`sudo` when opening a file, so that files the user cannot write to, such as system configuration files, can still be edited. The password prompt, if any, appears in the shell that hosts the panel. """, ) 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 shell receives the command that opens it in the editor configured with the :opt:`editor` option, :envvar:`VISUAL` or :envvar:`EDITOR`. With :code:`--open-here` the panel runs the editor in its own window instead, which is what makes this action work in a docked panel. """, ) 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 ` 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 Open files in the panel's own window instead of sending the editor command to the shell. Required in a window dock, where kitty never hands the result back to the shell. --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) def open_file(w: Any, path: str, use_sudo: bool = False) -> None: # Run the editor in the shell that hosted the panel, like the cd action, so # the file opens where the user already is instead of in a new tab. from kitty.utils import get_editor cmd = get_editor(path_to_edit=path) if use_sudo: # -- stops sudo from eating editor arguments that look like its own options. cmd = ['sudo', '--'] + cmd text = shlex.join(cmd) if w.at_prompt: w.write_to_child(text + '\r') else: w.paste_text(text) @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 not action or not path: return w = boss.window_id_map.get(target_window_id) if w is None: boss.ring_bell_if_allowed() return if action == 'cd': change_directory(w, path) elif action == 'open': open_file(w, path, data.get('sudo') == '1') 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