diff --git a/gen/go_code.py b/gen/go_code.py index 912a66c52..84c3596ef 100755 --- a/gen/go_code.py +++ b/gen/go_code.py @@ -19,6 +19,7 @@ from itertools import chain from typing import ( Any, BinaryIO, + NamedTuple, Optional, TextIO, Union, @@ -179,6 +180,81 @@ def stringify() -> None: stringify_file(path) # }}} +# {{{ Bitfields + +class BitField(NamedTuple): + name: str + bits: int + + +def typename_for_bitsize(bits: int) -> str: + if bits <= 8: + return 'uint8' + if bits <= 16: + return 'uint16' + if bits <= 32: + return 'uint32' + return 'uint64' + + +def make_bitfield(dest: str, typename: str, *fields_: str) -> None: + output_path = os.path.join(dest, f'{typename.lower()}_generated.go') + ans = [f'package {os.path.basename(dest)}', ''] + a = ans.append + + def fieldify(spec: str) -> BitField: + name, num = spec.partition(' ')[::2] + return BitField(name, int(num)) + + fields = tuple(map(fieldify, fields_)) + total_size = sum(x.bits for x in fields) + if total_size > 64: + raise ValueError(f'Total size of bit fields: {total_size} for {typename} is larger than 64 bits') + a(f'// Total number of bits used: {total_size}') + itype = typename_for_bitsize(total_size) + a(f'type {typename} {itype}') + a('') + shift = 0 + for bf in reversed(fields): + tn = typename_for_bitsize(bf.bits) + mask = '0b' + '1' * bf.bits + a(f'func (s {typename}) {bf.name.capitalize()}() {tn} {{') # }} + if shift: + a(f' return {tn}((s >> {shift}) & {mask})') + else: + a(f' return {tn}(s & {mask})') + a('}') + a('') + a(f'func (s *{typename}) Set{bf.name.capitalize()}(val {tn}) {{') # }} + if shift: + a(f' *s &^= {mask} << {shift}') + a(f' *s |= {typename}(val&{mask}) << {shift}') + else: + a(f' *s &^= {mask}') + a(f' *s |= {typename}(val & {mask})') + a('}') + a('') + shift += bf.bits + + with replace_if_needed(output_path) as buf: + print('\n'.join(ans), file=buf) + + +def make_bitfields() -> None: + from kitty.fast_data_types import SCALE_BITS, SUBSCALE_BITS, WIDTH_BITS + make_bitfield( + 'tools/vt', 'CellAttrs', + 'decoration 3', 'bold 1', 'italic 1', 'reverse 1', 'strike 1', 'dim 1', + ) + make_bitfield('tools/vt', 'Ch', 'is_idx 1', 'ch_or_idx 31') + make_bitfield( + 'tools/vt', 'MultiCell', + 'is_multicell 1', 'natural_width 1', f'scale {SCALE_BITS}', f'subscale_n {SUBSCALE_BITS}', f'subscale_d {SUBSCALE_BITS}', + f'width {WIDTH_BITS}', f'x {WIDTH_BITS + SCALE_BITS + 1}', f'y {SCALE_BITS + 1}', 'vertical_align 3', + ) + make_bitfield('tools/vt', 'CellColor', 'red 8', 'green 8', 'blue 8', 'is_idx 1') +# }}} + # Completions {{{ @lru_cache @@ -906,6 +982,7 @@ def main(args: list[str]=sys.argv) -> None: update_at_commands() kitten_clis() stringify() + make_bitfields() print(json.dumps(changed, indent=2)) stdout, stderr = simdgen_process.communicate() if simdgen_process.wait() != 0: diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index c1b0ac295..d7155714f 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -11,6 +11,9 @@ from kitty.types import LayerShellConfig, SignalInfo from kitty.typing import EdgeLiteral, NotRequired, ReadableBuffer, WriteableBuffer # Constants {{{ +SCALE_BITS: int +WIDTH_BITS: int +SUBSCALE_BITS: int GLFW_LAYER_SHELL_NONE: int GLFW_LAYER_SHELL_PANEL: int GLFW_LAYER_SHELL_TOP: int diff --git a/kitty/screen.c b/kitty/screen.c index 216b67e53..d15be2672 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -8,6 +8,7 @@ #define EXTRA_INIT { \ PyModule_AddIntMacro(module, SCROLL_LINE); PyModule_AddIntMacro(module, SCROLL_PAGE); PyModule_AddIntMacro(module, SCROLL_FULL); \ PyModule_AddIntMacro(module, EXTEND_CELL); PyModule_AddIntMacro(module, EXTEND_WORD); PyModule_AddIntMacro(module, EXTEND_LINE); \ + PyModule_AddIntMacro(module, SCALE_BITS); PyModule_AddIntMacro(module, WIDTH_BITS); PyModule_AddIntMacro(module, SUBSCALE_BITS); \ if (PyModule_AddFunctions(module, module_methods) != 0) return false; \ } diff --git a/tools/vt/cell.go b/tools/vt/cell.go new file mode 100644 index 000000000..e43f29aa5 --- /dev/null +++ b/tools/vt/cell.go @@ -0,0 +1,14 @@ +package vt + +import ( + "fmt" +) + +var _ = fmt.Print + +type Cell struct { + Ch Ch + Fg, Bg, Dec CellColor + Attrs CellAttrs + Mc MultiCell +}