set-spacing: Accept the *-horizontal and *-vertical edge names in kitten @

The Go parser had its own copy of the edge name mapping, which had drifted
from the Python one. Generate the Go mapping from the Python one so there
is a single source of truth, and share the documentation of the spacing
syntax between set-spacing and launch --spacing, documenting the long
forms.

Closes #10712
This commit is contained in:
Kovid Goyal 2026-10-21 10:20:35 +05:30
parent 5f45d30e36
commit f9bd7c2085
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
5 changed files with 65 additions and 26 deletions

View file

@ -41,6 +41,7 @@ from kitty.key_encoding import config_mod_map
from kitty.key_names import character_key_name_aliases, functional_key_name_aliases
from kitty.options.types import Options
from kitty.rc.base import RemoteCommand, all_command_names, command_for_name
from kitty.rc.set_spacing import spacing_edge_map
from kitty.remote_control import global_options_spec
from kitty.simple_cli_definitions import CompletionSpec, parse_option_spec, serialize_as_go_string
@ -809,6 +810,9 @@ func add_rc_global_opts(cmd *cli.Command) {{
"""
with replace_if_needed('tools/cmd/at/global_opts_generated.go') as f:
f.write(code)
entries = '\n'.join(f'\t{json.dumps(k)}: {{{", ".join(map(json.dumps, v))}}},' for k, v in spacing_edge_map().items())
with replace_if_needed('tools/cmd/at/spacing_edges_generated.go') as f:
f.write(f'package at\n\nvar spacing_edge_map = map[string][]string{{\n{entries}\n}}\n')
def update_completion() -> None:

View file

@ -61,6 +61,8 @@ This option was added to kitty in version 0.26.0
@run_once
def options_spec() -> str:
from .rc.set_spacing import SPACING_SETTINGS_DOC
return f"""
--source-window
A match expression to select the window from which data such as title, colors, env vars,
@ -457,8 +459,7 @@ from, or specify them individually, for example::
--spacing
type=list
Set the margin and padding for the newly created window.
For example: :code:`margin=20` or :code:`padding-left=10` or :code:`margin-h=30`. The shorthand form sets
all values, the :code:`*-h` and :code:`*-v` variants set horizontal and vertical values.
{SPACING_SETTINGS_DOC}
Can be specified multiple times. Note that this is ignored for overlay windows as these use the settings
from the base window.

View file

@ -3,7 +3,7 @@
from collections.abc import Iterable
from typing import TYPE_CHECKING, LiteralString
from typing import TYPE_CHECKING
from .base import MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, PayloadType, RCOptions, RemoteCommand, ResponseType, Window
@ -35,14 +35,28 @@ def patch_configured_edges(opts: 'Options', s: dict[str, float | None]) -> None:
setattr(opts, q, new_edges)
def parse_spacing_settings(args: Iterable[str]) -> dict[str, float | None]:
mapper: dict[str, list[LiteralString]] = {}
def spacing_edge_map() -> dict[str, tuple[str, ...]]:
# This is the single source of truth for the edge names accepted when
# setting spacing, it is also used to generate the Go parser's map.
ans: dict[str, tuple[str, ...]] = {}
for q in ('margin', 'padding'):
mapper[q] = f'{q}-left {q}-top {q}-right {q}-bottom'.split()
mapper[f'{q}-h'] = mapper[f'{q}-horizontal'] = f'{q}-left {q}-right'.split()
mapper[f'{q}-v'] = mapper[f'{q}-vertical'] = f'{q}-top {q}-bottom'.split()
ans[q] = tuple(f'{q}-{edge}' for edge in ('left', 'top', 'right', 'bottom'))
ans[f'{q}-h'] = ans[f'{q}-horizontal'] = (f'{q}-left', f'{q}-right')
ans[f'{q}-v'] = ans[f'{q}-vertical'] = (f'{q}-top', f'{q}-bottom')
for edge in ('left', 'top', 'right', 'bottom'):
mapper[f'{q}-{edge}'] = [f'{q}-{edge}']
ans[f'{q}-{edge}'] = (f'{q}-{edge}',)
return ans
SPACING_SETTINGS_DOC = (
'For example: :code:`margin=20` or :code:`padding-left=10` or :code:`margin-h=30`. The shorthand form sets'
' all values, the :code:`*-h` (or :code:`*-horizontal`) and :code:`*-v` (or :code:`*-vertical`) variants'
' set horizontal and vertical values. The special value :code:`default` resets to using the default value.'
)
def parse_spacing_settings(args: Iterable[str]) -> dict[str, float | None]:
mapper = spacing_edge_map()
settings: dict[str, float | None] = {}
for spec in args:
parts = spec.split('=', 1)
@ -74,11 +88,9 @@ class SetSpacing(RemoteCommand):
short_desc = 'Set window paddings and margins'
desc = (
'Set the paddings and margins for the specified windows (defaults to active window).'
' For example: :code:`margin=20` or :code:`padding-left=10` or :code:`margin-h=30`. The shorthand form sets'
' all values, the :code:`*-h` and :code:`*-v` variants set horizontal and vertical values.'
' The special value :code:`default` resets to using the default value.'
' If you specify a tab rather than a window, all windows in that tab are affected.'
'Set the paddings and margins for the specified windows (defaults to active window). '
+ SPACING_SETTINGS_DOC
+ ' If you specify a tab rather than a window, all windows in that tab are affected.'
)
options_spec = (
"""\

View file

@ -10,17 +10,6 @@ import (
func parse_set_spacing(args []string) (map[string]any, error) {
ans := make(map[string]any, len(args))
mapper := make(map[string][]string, 32)
types := [2]string{"margin", "padding"}
for _, q := range types {
mapper[q] = []string{q + "-left", q + "-top", q + "-right", q + "-bottom"}
mapper[q+"-h"] = []string{q + "-left", q + "-right"}
mapper[q+"-v"] = []string{q + "-top", q + "-bottom"}
mapper[q+"-left"] = []string{q + "-left"}
mapper[q+"-right"] = []string{q + "-right"}
mapper[q+"-top"] = []string{q + "-top"}
mapper[q+"-bottom"] = []string{q + "-bottom"}
}
for _, arg := range args {
k, v, found := strings.Cut(arg, "=")
if !found {
@ -28,7 +17,7 @@ func parse_set_spacing(args []string) (map[string]any, error) {
}
k = strings.ToLower(k)
v = strings.ToLower(v)
which, found := mapper[k]
which, found := spacing_edge_map[k]
if !found {
return nil, fmt.Errorf("%s is not a valid edge specification", k)
}

View file

@ -0,0 +1,33 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package at
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestParseSetSpacing(t *testing.T) {
ten := float64(10)
for args, expected := range map[string]map[string]any{
"padding=default": {"padding-left": nil, "padding-top": nil, "padding-right": nil, "padding-bottom": nil},
"margin-h=10": {"margin-left": ten, "margin-right": ten},
"Margin-Horizontal=10 padding-vertical=default": {"margin-left": ten, "margin-right": ten, "padding-top": nil, "padding-bottom": nil},
"padding-v=10 margin-bottom=10": {"padding-top": ten, "padding-bottom": ten, "margin-bottom": ten},
} {
actual, err := parse_set_spacing(strings.Fields(args))
if err != nil {
t.Fatalf("parse_set_spacing(%#v) failed with error: %s", args, err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("parse_set_spacing(%#v) returned unexpected result:\n%s", args, diff)
}
}
for _, arg := range []string{"margin", "margin-diagonal=10", "padding=x"} {
if actual, err := parse_set_spacing([]string{arg}); err == nil {
t.Fatalf("parse_set_spacing(%#v) accepted invalid input and returned: %v", arg, actual)
}
}
}