goto_session: Add --sort-by=alphabetical

Have the interactive session picker list the sessions in a fixed order
rather than by most recent.
This commit is contained in:
Kovid Goyal
2025-09-29 13:03:23 +05:30
parent d8b524c692
commit 2ceddba923
4 changed files with 41 additions and 7 deletions

View File

@@ -147,6 +147,10 @@ Detailed list of changes
- macOS: Workaround for bug in macOS Tahoe that caused closed OS Windows to
remain as invisible rectangles that intercept mouse events (:iss:`8952`)
- goto_session: Add ``--sort-by=alphabetical`` to have the interactive session
picker list the sessions in a fixed order rather than by most recent
(:disc:`9033`)
0.43.0 [2025-09-28]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -55,6 +55,8 @@ easily swap between them, kitty has you covered. You can use the
map f7>h goto_session ~/path/to/hot/hot.kitty-session
# Browse and select from the list of known projects defined via goto_session commands
map f7>/ goto_session
# Same as above, but the sessions are listed alphabetically instead of by most recent
map f7>/ goto_session --sort-by=alphabetical
# Go to the previously active session (larger negative numbers jump further back in history)
map f7>- goto_session -1

View File

@@ -13,7 +13,7 @@ class CLIOptions:
LaunchCLIOptions = AskCLIOptions = ClipboardCLIOptions = DiffCLIOptions = CLIOptions
HintsCLIOptions = IcatCLIOptions = PanelCLIOptions = ResizeCLIOptions = CLIOptions
ErrorCLIOptions = UnicodeCLIOptions = RCOptions = RemoteFileCLIOptions = CLIOptions
BroadcastCLIOptions = ShowKeyCLIOptions = SaveAsSessionOptions = CLIOptions
BroadcastCLIOptions = ShowKeyCLIOptions = SaveAsSessionOptions = GotoSessionOptions = CLIOptions
ThemesCLIOptions = TransferCLIOptions = LoadConfigRCOptions = ActionRCOptions = CLIOptions
@@ -74,8 +74,9 @@ def generate_stub() -> None:
from kittens.transfer.main import option_text
do(option_text(), 'TransferCLIOptions')
from kitty.session import save_as_session_options
from kitty.session import goto_session_options, save_as_session_options
do(save_as_session_options(), 'SaveAsSessionOptions')
do(goto_session_options(), 'GotoSessionOptions')
from kitty.rc.base import all_command_names, command_for_name
for cmd_name in all_command_names():

View File

@@ -11,7 +11,7 @@ from functools import partial
from gettext import gettext as _
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union
from .cli_stub import CLIOptions, SaveAsSessionOptions
from .cli_stub import CLIOptions, GotoSessionOptions, SaveAsSessionOptions
from .constants import config_dir, unserialize_launch_flag
from .fast_data_types import get_options
from .layout.interface import all_layouts
@@ -493,11 +493,15 @@ def close_session_with_confirm(boss: BossType, cmdline: Sequence[str]) -> None:
do_close(True)
def choose_session(boss: BossType) -> None:
def choose_session(boss: BossType, opts: GotoSessionOptions) -> None:
all_known_sessions = get_all_known_sessions()
hmap = {n: len(goto_session_history)-i for i, n in enumerate(goto_session_history)}
def skey(name: str) -> tuple[int, str]:
return hmap.get(name, len(goto_session_history)), name.lower()
if opts.sort_by == 'alphabetical':
def skey(name: str) -> tuple[int, str]:
return 0, name.lower()
else:
def skey(name: str) -> tuple[int, str]:
return hmap.get(name, len(goto_session_history)), name.lower()
names = sorted(all_known_sessions, key=skey)
def chosen(name: str | None) -> None:
@@ -507,9 +511,32 @@ def choose_session(boss: BossType) -> None:
_('Select a session to activate'), ((name, name) for name in names), chosen)
def parse_goto_session_cmdline(args: list[str]) -> tuple[GotoSessionOptions, list[str]]:
from kitty.cli import cached_parse_cmdline
ans = GotoSessionOptions()
leftover_args = cached_parse_cmdline(goto_session_options(), args, ans)
return ans, leftover_args
def goto_session_options() -> str:
return '''
--sort-by
choices=recent,alphabetical
default=recent
When interactively choosing sessions from a list, how to sort the list.
'''
def goto_session(boss: BossType, cmdline: Sequence[str]) -> None:
try:
opts, cmdline = parse_goto_session_cmdline(list(cmdline))
except Exception as e:
boss.show_error(_('Invalid goto_session command'), _(
'The command goto_session {0} is invalid with error: {1}').format(shlex.join(cmdline), e))
return
if not cmdline:
choose_session(boss)
choose_session(boss, opts)
return
path = cmdline[0]
if len(cmdline) == 1: