Skip to content

Commit d30ba96

Browse files
committed
Nicer formatting of mappings in the comment conf file
1 parent 3e3dc26 commit d30ba96

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

kitty/conf/types.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import builtins
55
import re
6+
import textwrap
67
import typing
8+
from functools import lru_cache
79
from importlib import import_module
810
from typing import (
911
Any, Callable, Dict, Iterable, Iterator, List, Match, Optional, Set, Tuple,
@@ -78,30 +80,31 @@ def iter_blocks(lines: Iterable[str]) -> Iterator[Tuple[List[str], int]]:
7880
yield current_block, indent_size
7981

8082

81-
def wrapped_block(lines: Iterable[str]) -> Iterator[str]:
82-
wrapper = getattr(wrapped_block, 'wrapper', None)
83-
if wrapper is None:
84-
import textwrap
85-
wrapper = textwrap.TextWrapper(
86-
initial_indent='#: ', subsequent_indent='#: ', width=70, break_long_words=False
83+
@lru_cache(maxsize=8)
84+
def block_wrapper(comment_symbol: str) -> textwrap.TextWrapper:
85+
return textwrap.TextWrapper(
86+
initial_indent=comment_symbol, subsequent_indent=comment_symbol, width=70, break_long_words=False
8787
)
88-
setattr(wrapped_block, 'wrapper', wrapper)
88+
89+
90+
def wrapped_block(lines: Iterable[str], comment_symbol: str = '#: ') -> Iterator[str]:
91+
wrapper = block_wrapper(comment_symbol)
8992
for block, indent_size in iter_blocks(lines):
9093
if indent_size > 0:
9194
for line in block:
9295
if not line:
9396
yield line
9497
else:
95-
yield '#: ' + line
98+
yield comment_symbol + line
9699
else:
97100
for line in wrapper.wrap('\n'.join(block)):
98101
yield line
99102

100103

101-
def render_block(text: str) -> str:
104+
def render_block(text: str, comment_symbol: str = '#: ') -> str:
102105
text = remove_markup(text)
103106
lines = text.splitlines()
104-
return '\n'.join(wrapped_block(lines))
107+
return '\n'.join(wrapped_block(lines, comment_symbol))
105108

106109

107110
class CoalescedIteratorData:
@@ -288,12 +291,17 @@ def key_text(self) -> str:
288291

289292
def as_conf(self, commented: bool = False, level: int = 0, action_group: List['Mapping'] = []) -> List[str]:
290293
ans: List[str] = []
294+
if not self.documented:
295+
return ans
291296
a = ans.append
297+
if self.short_text:
298+
a(render_block(self.short_text.strip())), a('')
292299
for sc in [self] + action_group:
293300
if sc.documented and sc.add_to_default:
294301
a(sc.setting_name + ' ' + sc.parseable_text)
295-
if self.documented and self.long_text:
296-
a(''), a(render_block(self.long_text.strip())), a('')
302+
if self.long_text:
303+
a(''), a(render_block(self.long_text.strip(), '#:: '))
304+
a('')
297305
return ans
298306

299307
def as_rst(

kitty/options/definition.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,7 @@
619619

620620
mma('Show clicked command output in pager',
621621
'show_clicked_cmd_output_ungrabbed ctrl+shift+right press ungrabbed mouse_show_command_output',
622-
long_text='''
623-
Requires :ref:`shell_integration` to work.
624-
'''
622+
long_text='Requires :ref:`shell_integration` to work'
625623
)
626624
egr() # }}}
627625
egr() # }}}

0 commit comments

Comments
 (0)