From 4b81e4936e026d202622e95cf41687ad34b0c833 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 2 Sep 2022 19:41:05 +0530 Subject: [PATCH] launch: Allow setting the margin and padding for the newly created window Fixes #5463 --- docs/changelog.rst | 2 ++ kitty/launch.py | 15 +++++++++++ kitty/rc/launch.py | 1 + kitty/rc/set_spacing.py | 58 +++++++++++++++++++++++------------------ 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8c0b06e69..94d339284 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -42,6 +42,8 @@ Detailed list of changes - Fix regression in 0.26.0 that caused launching kitty without working STDIO handles to result in high CPU usage and prewarming failing (:iss:`5444`) +- :doc:`/launch`: Allow setting the margin and padding for the newly created window (:iss:`5463`) + - macOS: Fix regression in 0.26.0 that caused asking the user for a line of input such as for :ac:`set_tab_title` to not work (:iss:`5447`) - hints kitten: hyperlink matching: Fix hints occasionally matching text on subsequent line as part of hyperlink (:pull:`5450`) diff --git a/kitty/launch.py b/kitty/launch.py index 93de09d77..75b6109a9 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -278,6 +278,14 @@ from, or specify them individually, for example:: --color background=white --color foreground=red +--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. +Can be specified multiple times. + + --watcher -w type=list completion=type:file ext:py relative:conf group:"Python scripts" @@ -457,6 +465,10 @@ def launch( 'overlay_for': None, 'stdin': None } + spacing = {} + if opts.spacing: + from .rc.set_spacing import parse_spacing_settings, patch_window_edges + spacing = parse_spacing_settings(opts.spacing) if opts.cwd: if opts.cwd == 'current': if active: @@ -552,6 +564,9 @@ def launch( if tab is not None: watchers = load_watch_modules(opts.watcher) new_window: Window = tab.new_window(env=env or None, watchers=watchers or None, is_clone_launch=is_clone_launch, **kw) + if spacing: + patch_window_edges(new_window, spacing) + tab.relayout() if opts.color: apply_colors(new_window, opts.color) if opts.keep_focus and active: diff --git a/kitty/rc/launch.py b/kitty/rc/launch.py index 6af9f7c43..779325337 100644 --- a/kitty/rc/launch.py +++ b/kitty/rc/launch.py @@ -41,6 +41,7 @@ class Launch(RemoteCommand): @first_cmd_output_on_screen.@last_cmd_output.@last_visited_cmd_output: Where to get stdin for the process from stdin_add_formatting/bool: Boolean indicating whether to add formatting codes to stdin stdin_add_line_wrap_markers/bool: Boolean indicating whether to add line wrap markers to stdin + spacing/list.str: A list of spacing specifications, see the docs for the set-spacing command no_response/bool: Boolean indicating whether to send back the window id marker/str: Specification for marker for new window, for example: "text 1 ERROR" logo/str: Path to window logo diff --git a/kitty/rc/set_spacing.py b/kitty/rc/set_spacing.py index ce6cc2082..c37730a7e 100644 --- a/kitty/rc/set_spacing.py +++ b/kitty/rc/set_spacing.py @@ -2,7 +2,7 @@ # License: GPLv3 Copyright: 2020, Kovid Goyal -from typing import TYPE_CHECKING, Dict, Optional, List +from typing import TYPE_CHECKING, Dict, Iterable, List, Optional from .base import ( MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, @@ -37,6 +37,34 @@ def patch_configured_edges(opts: 'Options', s: Dict[str, Optional[float]]) -> No setattr(opts, q, new_edges) +def parse_spacing_settings(args: Iterable[str]) -> Dict[str, Optional[float]]: + mapper: Dict[str, List[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() + for edge in ('left', 'top', 'right', 'bottom'): + mapper[f'{q}-{edge}'] = [f'{q}-{edge}'] + settings: Dict[str, Optional[float]] = {} + for spec in args: + parts = spec.split('=', 1) + if len(parts) != 2: + raise ValueError(f'{spec} is not a valid setting') + which = mapper.get(parts[0].lower()) + if not which: + raise ValueError(f'{parts[0]} is not a valid edge specification') + if parts[1].lower() == 'default': + val = None + else: + try: + val = float(parts[1]) + except Exception: + raise ValueError(f'{parts[1]} is not a number') + for q in which: + settings[q] = val + return settings + + class SetSpacing(RemoteCommand): ''' @@ -70,32 +98,12 @@ windows). argspec = 'MARGIN_OR_PADDING ...' def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: - settings: Dict[str, Optional[float]] = {} - mapper: Dict[str, List[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() - for edge in ('left', 'top', 'right', 'bottom'): - mapper[f'{q}-{edge}'] = [f'{q}-{edge}'] if not args: self.fatal('At least one setting must be specified') - for spec in args: - parts = spec.split('=', 1) - if len(parts) != 2: - self.fatal(f'{spec} is not a valid setting') - which = mapper.get(parts[0].lower()) - if not which: - self.fatal(f'{parts[0]} is not a valid edge specification') - if parts[1].lower() == 'default': - val = None - else: - try: - val = float(parts[1]) - except Exception: - self.fatal(f'{parts[1]} is not a number') - for q in which: - settings[q] = val + try: + settings = parse_spacing_settings(args) + except Exception as e: + self.fatal(str(e)) ans = { 'match_window': opts.match, 'match_tab': opts.match_tab, 'all': opts.all, 'configured': opts.configured,