From 75d0dcab8d58ba62fb841c03a6f6943aab50dab2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 5 Aug 2024 11:18:41 +0530 Subject: [PATCH] Nicer fix for non-zero fields in rc protocol --- gen/go_code.py | 20 +++++++++++--------- kitty/rc/resize_window.py | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/gen/go_code.py b/gen/go_code.py index efb5499af..32bf2aec6 100755 --- a/gen/go_code.py +++ b/gen/go_code.py @@ -314,17 +314,21 @@ def go_field_type(json_field_type: str) -> str: class JSONField: - def __init__(self, line: str) -> None: + def __init__(self, line: str, field_to_option_map: dict[str, str], option_map: dict[str, GoOption]) -> None: field_def = line.split(':', 1)[0] self.required = False self.field, self.field_type = field_def.split('/', 1) + self.go_option_name = field_to_option_map.get(self.field, self.field) + self.go_option_name = ''.join(x.capitalize() for x in self.go_option_name.split('_')) + self.omitempty = True + if fo := option_map.get(self.go_option_name): + if fo.type in ('int', 'float') and float(fo.default or 0) != 0: + self.omitempty = False self.field_type, self.special_parser = self.field_type.partition('=')[::2] - self.field_type, self.metadata = self.field_type.partition('-')[::2] if self.field.endswith('+'): self.required = True self.field = self.field[:-1] self.struct_field_name = self.field[0].upper() + self.field[1:] - self.omitempty = 'noomitempty' not in self.metadata def go_declaration(self) -> str: omitempty = ',omitempty' if self.omitempty else '' @@ -351,7 +355,7 @@ def go_code_for_remote_command(name: str, cmd: RemoteCommand, template: str) -> line = line.strip() if ':' not in line: continue - f = JSONField(line) + f = JSONField(line, cmd.field_to_option_map or {}, option_map) json_fields.append(f) field_types[f.field] = f.field_type jd.append(f.go_declaration()) @@ -362,11 +366,9 @@ def go_code_for_remote_command(name: str, cmd: RemoteCommand, template: str) -> unhandled = {} used_options = set() for field in json_fields: - oq = (cmd.field_to_option_map or {}).get(field.field, field.field) - oq = ''.join(x.capitalize() for x in oq.split('_')) - if oq in option_map: - o = option_map[oq] - used_options.add(oq) + if field.go_option_name in option_map: + o = option_map[field.go_option_name] + used_options.add(field.go_option_name) optstring = f'options_{name}.{o.go_var_name}' if field.special_parser: optstring = f'{field.special_parser}({optstring})' diff --git a/kitty/rc/resize_window.py b/kitty/rc/resize_window.py index f9fbe1ed4..58c378852 100644 --- a/kitty/rc/resize_window.py +++ b/kitty/rc/resize_window.py @@ -13,7 +13,7 @@ class ResizeWindow(RemoteCommand): protocol_spec = __doc__ = ''' match/str: Which window to resize self/bool: Boolean indicating whether to resize the window the command is run in - increment/int-noomitempty: Integer specifying the resize increment + increment/int: Integer specifying the resize increment axis/choices.horizontal.vertical.reset: One of :code:`horizontal, vertical` or :code:`reset` '''