diff --git a/docs/changelog.rst b/docs/changelog.rst index 019b263f0..2484c6172 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -38,7 +38,10 @@ Detailed list of changes 0.30.0 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- kitty @ ls: Add user variables set on windows to the output (:iss:`6502`) +- kitten @ set-user-vars: New remote control command to set user variables on a + window (:iss:`6502`) + +- kitten @ ls: Add user variables set on windows to the output (:iss:`6502`) 0.29.2 [2023-07-27] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/rc/set_user_vars.py b/kitty/rc/set_user_vars.py new file mode 100644 index 000000000..712e3a1ca --- /dev/null +++ b/kitty/rc/set_user_vars.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# License: GPLv3 Copyright: 2020, Kovid Goyal + +from typing import TYPE_CHECKING, Optional + +from .base import MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, PayloadType, RCOptions, RemoteCommand, ResponseType, Window + +if TYPE_CHECKING: + from kitty.cli_stub import SetUserVarsRCOptions as CLIOptions + + +class SetUserVars(RemoteCommand): + + protocol_spec = __doc__ = ''' + var/list.str: List of user variables of the form NAME=VALUE + match/str: Which windows to change the title in + ''' + + short_desc = 'Set user variables on a window' + desc = ( + 'Set user variables for the specified windows. If you use the :option:`kitty @ set-window-title --match` option' + ' the variables will be set for all matched windows. By default, only the window' + ' in which the command is run is affected. If you do not specify any variables, the' + ' current variables are printed out, one per line. To unset a variable specify just its name.' + ) + options_spec = MATCH_WINDOW_OPTION + args = RemoteCommand.Args(json_field='var', spec='[NAME=VALUE ...]') + + def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: + return {'match': opts.match, 'var': args, 'self': True} + + def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType: + remove = set() + val = {} + for x in payload_get('var') or (): + a, sep, b = x.partition('=') + if sep: + val[a] = b + else: + remove.add(a) + lines = [] + for window in self.windows_for_match_payload(boss, window, payload_get): + if window: + if val or remove: + window.user_vars.update(val) + for x in remove: + window.user_vars.pop(x, None) + else: + lines.append('\n'.join(f'{k}={v}' for k, v in window.user_vars.items())) + return '\n\n'.join(lines) + + +set_user_vars = SetUserVars()