DRYer
This commit is contained in:
parent
dff7ddea39
commit
62174fdbcc
@ -81,6 +81,10 @@ def colored(text, color, intense=False, reset_to=None, reset_to_intense=False):
|
|||||||
return '\033[{}m{}\033[{}m'.format(e, text, 39 if reset_to is None else color_code(reset_to, reset_to_intense))
|
return '\033[{}m{}\033[{}m'.format(e, text, 39 if reset_to is None else color_code(reset_to, reset_to_intense))
|
||||||
|
|
||||||
|
|
||||||
|
def faint(text):
|
||||||
|
return colored(text, 'black', True)
|
||||||
|
|
||||||
|
|
||||||
def styled(text, fg=None, bg=None, fg_intense=False, bg_intense=False, italic=None, bold=None, underline=None, underline_color=None, reverse=None):
|
def styled(text, fg=None, bg=None, fg_intense=False, bg_intense=False, italic=None, bold=None, underline=None, underline_color=None, reverse=None):
|
||||||
start, end = [], []
|
start, end = [], []
|
||||||
if fg is not None:
|
if fg is not None:
|
||||||
|
|||||||
@ -21,13 +21,20 @@ from kitty.key_encoding import (
|
|||||||
from ..tui.handler import Handler
|
from ..tui.handler import Handler
|
||||||
from ..tui.loop import Loop
|
from ..tui.loop import Loop
|
||||||
from ..tui.operations import (
|
from ..tui.operations import (
|
||||||
clear_screen, color_code, colored, cursor, set_line_wrapping,
|
clear_screen, color_code, colored, cursor, faint, set_line_wrapping,
|
||||||
set_window_title, sgr, styled
|
set_window_title, sgr, styled
|
||||||
)
|
)
|
||||||
|
|
||||||
HEX, NAME, EMOTICONS, FAVORITES = 'HEX', 'NAME', 'EMOTICONS', 'FAVORITES'
|
HEX, NAME, EMOTICONS, FAVORITES = 'HEX', 'NAME', 'EMOTICONS', 'FAVORITES'
|
||||||
favorites_path = os.path.join(config_dir, 'unicode-input-favorites.conf')
|
favorites_path = os.path.join(config_dir, 'unicode-input-favorites.conf')
|
||||||
INDEX_CHAR = '.'
|
INDEX_CHAR = '.'
|
||||||
|
DEFAULT_SET = tuple(map(
|
||||||
|
ord,
|
||||||
|
'‘’“”‹›«»‚„' '😀😛😇😈😉😍😎😮👍👎' '—–§¶†‡©®™' '→⇒•·°±−×÷¼½½¾'
|
||||||
|
'…µ¢£€¿¡¨´¸ˆ˜' 'ÀÁÂÃÄÅÆÇÈÉÊË' 'ÌÍÎÏÐÑÒÓÔÕÖØ' 'ŒŠÙÚÛÜÝŸÞßàá' 'âãäåæçèéêëìí'
|
||||||
|
'îïðñòóôõöøœš' 'ùúûüýÿþªºαΩ∞'
|
||||||
|
))
|
||||||
|
EMOTICONS_SET = tuple(range(0x1f600, 0x1f64f + 1))
|
||||||
|
|
||||||
|
|
||||||
def codepoint_ok(code):
|
def codepoint_ok(code):
|
||||||
@ -108,16 +115,6 @@ def load_favorites(refresh=False):
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
FAINT = 242
|
|
||||||
DEFAULT_SET = tuple(map(
|
|
||||||
ord,
|
|
||||||
'‘’“”‹›«»‚„' '😀😛😇😈😉😍😎😮👍👎' '—–§¶†‡©®™' '→⇒•·°±−×÷¼½½¾'
|
|
||||||
'…µ¢£€¿¡¨´¸ˆ˜' 'ÀÁÂÃÄÅÆÇÈÉÊË' 'ÌÍÎÏÐÑÒÓÔÕÖØ' 'ŒŠÙÚÛÜÝŸÞßàá' 'âãäåæçèéêëìí'
|
|
||||||
'îïðñòóôõöøœš' 'ùúûüýÿþªºαΩ∞'
|
|
||||||
))
|
|
||||||
EMOTICONS_SET = tuple(range(0x1f600, 0x1f64f + 1))
|
|
||||||
|
|
||||||
|
|
||||||
def encode_hint(num, digits=string.digits + string.ascii_lowercase):
|
def encode_hint(num, digits=string.digits + string.ascii_lowercase):
|
||||||
res = ''
|
res = ''
|
||||||
d = len(digits)
|
d = len(digits)
|
||||||
@ -177,7 +174,7 @@ class Table:
|
|||||||
yield ' ' * (2 - w)
|
yield ' ' * (2 - w)
|
||||||
if len(desc) > space_for_desc:
|
if len(desc) > space_for_desc:
|
||||||
desc = desc[:space_for_desc - 1] + '…'
|
desc = desc[:space_for_desc - 1] + '…'
|
||||||
yield colored(desc, FAINT)
|
yield faint(desc)
|
||||||
extra = space_for_desc - len(desc)
|
extra = space_for_desc - len(desc)
|
||||||
if extra > 0:
|
if extra > 0:
|
||||||
yield ' ' * extra
|
yield ' ' * extra
|
||||||
@ -323,7 +320,7 @@ class UnicodeInput(Handler):
|
|||||||
else:
|
else:
|
||||||
c, color = self.current_char, 'green'
|
c, color = self.current_char, 'green'
|
||||||
self.choice_line = _('Chosen:') + ' {} U+{} {}'.format(
|
self.choice_line = _('Chosen:') + ' {} U+{} {}'.format(
|
||||||
colored(c, 'green'), hex(ord(c))[2:], styled(name(c) or '', italic=True, fg=FAINT))
|
colored(c, 'green'), hex(ord(c))[2:], faint(styled(name(c) or '', italic=True)))
|
||||||
self.prompt = self.prompt_template.format(colored(c, color))
|
self.prompt = self.prompt_template.format(colored(c, color))
|
||||||
|
|
||||||
def init_terminal_state(self):
|
def init_terminal_state(self):
|
||||||
@ -375,11 +372,11 @@ class UnicodeInput(Handler):
|
|||||||
writeln()
|
writeln()
|
||||||
writeln(self.choice_line)
|
writeln(self.choice_line)
|
||||||
if self.mode is HEX:
|
if self.mode is HEX:
|
||||||
writeln(styled(_('Type {} followed by the index for the recent entries below').format(INDEX_CHAR), fg=FAINT))
|
writeln(faint(_('Type {} followed by the index for the recent entries below').format(INDEX_CHAR)))
|
||||||
elif self.mode is NAME:
|
elif self.mode is NAME:
|
||||||
writeln(styled(_('Use Tab or the arrow keys to choose a character from below'), fg=FAINT))
|
writeln(faint(_('Use Tab or the arrow keys to choose a character from below')))
|
||||||
elif self.mode is FAVORITES:
|
elif self.mode is FAVORITES:
|
||||||
writeln(styled(_('Press F12 to edit the list of favorites'), fg=FAINT))
|
writeln(faint(_('Press F12 to edit the list of favorites')))
|
||||||
self.table_at = y
|
self.table_at = y
|
||||||
self.write(self.table.layout(self.screen_size.rows - self.table_at, self.screen_size.cols))
|
self.write(self.table.layout(self.screen_size.rows - self.table_at, self.screen_size.cols))
|
||||||
|
|
||||||
|
|||||||
@ -14,12 +14,11 @@ from kitty.utils import read_with_timeout
|
|||||||
|
|
||||||
from ..tui.handler import Handler
|
from ..tui.handler import Handler
|
||||||
from ..tui.loop import Loop
|
from ..tui.loop import Loop
|
||||||
from ..tui.operations import clear_screen, colored, set_window_title, styled
|
from ..tui.operations import clear_screen, faint, set_window_title, styled
|
||||||
|
|
||||||
Mark = namedtuple('Mark', 'index start end text')
|
Mark = namedtuple('Mark', 'index start end text')
|
||||||
URL_PREFIXES = 'http https file ftp'.split()
|
URL_PREFIXES = 'http https file ftp'.split()
|
||||||
HINT_ALPHABET = string.digits + string.ascii_lowercase
|
HINT_ALPHABET = string.digits + string.ascii_lowercase
|
||||||
FAINT = 242
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=2048)
|
@lru_cache(maxsize=2048)
|
||||||
@ -39,9 +38,6 @@ def decode_hint(x):
|
|||||||
def render(lines, current_input):
|
def render(lines, current_input):
|
||||||
ans = []
|
ans = []
|
||||||
|
|
||||||
def faint(text):
|
|
||||||
return colored(text, FAINT)
|
|
||||||
|
|
||||||
def mark(m):
|
def mark(m):
|
||||||
hint = encode_hint(m.index)
|
hint = encode_hint(m.index)
|
||||||
text = m.text
|
text = m.text
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user