From 174fab50c28ad77339501f949afb8999f29a8786 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sun, 27 Feb 2022 20:45:47 +1100 Subject: [PATCH 1/7] `qmk info`: nicer rendering of ISO enter --- lib/python/qmk/keyboard.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index c87ea9050b76..bf9aa1cb877c 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -169,6 +169,7 @@ def render_layout(layout_data, render_ascii, key_labels=None): y = ceil(key.get('y', 0) * 3) w = ceil(key.get('w', 1) * 4) h = ceil(key.get('h', 1) * 3) + is_iso_key = w == 5 and h == 6 if key_labels: label = key_labels.pop(0) @@ -198,6 +199,14 @@ def render_layout(layout_data, render_ascii, key_labels=None): textpad[y + i + 2][x:x + w] = mid_line textpad[y + h - 1][x:x + w] = bot_line + if is_iso_key: + textpad[y][x - 1] = box_chars['tl'] + textpad[y][x] = box_chars['h'] + textpad[y + 1][x - 1] = box_chars['v'] + textpad[y + 1][x] = ' ' + textpad[y + 2][x - 1] = box_chars['bl'] + textpad[y + 2][x] = box_chars['tr'] + lines = [] for line in textpad: if line.tounicode().strip(): From acd55f24391f94e60ffd70d506bb42ea1a0a4833 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 5 Mar 2022 12:27:51 +1100 Subject: [PATCH 2/7] Refactor key rendering --- lib/python/qmk/keyboard.py | 101 ++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index bf9aa1cb877c..8bd7a6ee735c 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -162,14 +162,12 @@ def render_layout(layout_data, render_ascii, key_labels=None): """ textpad = [array('u', ' ' * 200) for x in range(100)] style = 'ascii' if render_ascii else 'unicode' - box_chars = BOX_DRAWING_CHARACTERS[style] for key in layout_data: - x = ceil(key.get('x', 0) * 4) - y = ceil(key.get('y', 0) * 3) - w = ceil(key.get('w', 1) * 4) - h = ceil(key.get('h', 1) * 3) - is_iso_key = w == 5 and h == 6 + x = key.get('x', 0) + y = key.get('y', 0) + w = key.get('w', 1) + h = key.get('h', 1) if key_labels: label = key_labels.pop(0) @@ -178,34 +176,10 @@ def render_layout(layout_data, render_ascii, key_labels=None): else: label = key.get('label', '') - label_len = w - 2 - label_leftover = label_len - len(label) - - if len(label) > label_len: - label = label[:label_len] - - label_blank = ' ' * label_len - label_border = box_chars['h'] * label_len - label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * - - top_line = array('u', box_chars['tl'] + label_border + box_chars['tr']) - lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) - mid_line = array('u', box_chars['v'] + label_blank + box_chars['v']) - bot_line = array('u', box_chars['bl'] + label_border + box_chars['br']) - - textpad[y][x:x + w] = top_line - textpad[y + 1][x:x + w] = lab_line - for i in range(h - 3): - textpad[y + i + 2][x:x + w] = mid_line - textpad[y + h - 1][x:x + w] = bot_line - - if is_iso_key: - textpad[y][x - 1] = box_chars['tl'] - textpad[y][x] = box_chars['h'] - textpad[y + 1][x - 1] = box_chars['v'] - textpad[y + 1][x] = ' ' - textpad[y + 2][x - 1] = box_chars['bl'] - textpad[y + 2][x] = box_chars['tr'] + if x >= 0.25 and w == 1.25 and h == 2: + render_key_isoenter(textpad, x, y, w, h, label, style) + else: + render_key_rect(textpad, x, y, w, h, label, style) lines = [] for line in textpad: @@ -225,3 +199,62 @@ def render_layouts(info_json, render_ascii): layouts[layout] = render_layout(layout_data, render_ascii) return layouts + +def render_key_rect(textpad, x, y, w, h, label, style): + box_chars = BOX_DRAWING_CHARACTERS[style] + x = ceil(x * 4) + y = ceil(y * 3) + w = ceil(w * 4) + h = ceil(h * 3) + + label_len = w - 2 + label_leftover = label_len - len(label) + + if len(label) > label_len: + label = label[:label_len] + + label_blank = ' ' * label_len + label_border = box_chars['h'] * label_len + label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * + + top_line = array('u', box_chars['tl'] + label_border + box_chars['tr']) + lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) + mid_line = array('u', box_chars['v'] + label_blank + box_chars['v']) + bot_line = array('u', box_chars['bl'] + label_border + box_chars['br']) + + textpad[y][x:x + w] = top_line + textpad[y + 1][x:x + w] = lab_line + for i in range(h - 3): + textpad[y + i + 2][x:x + w] = mid_line + textpad[y + h - 1][x:x + w] = bot_line + +def render_key_isoenter(textpad, x, y, w, h, label, style): + box_chars = BOX_DRAWING_CHARACTERS[style] + x = ceil(x * 4) + y = ceil(y * 3) + w = ceil(w * 4) + h = ceil(h * 3) + + label_len = w - 2 + label_leftover = label_len - len(label) + 1 + + if len(label) > label_len: + label = label[:label_len] + + label_blank = ' ' * label_len + label_border_top = box_chars['h'] * (label_len + 1) + label_border = box_chars['h'] * label_len + label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * + + top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr']) + lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) + crn_line = array('u', box_chars['bl'] + box_chars['tr'] + label_blank + box_chars['v']) + mid_line = array('u', box_chars['v'] + label_blank + box_chars['v']) + bot_line = array('u', box_chars['bl'] + label_border + box_chars['br']) + + textpad[y][x - 1:x + w + 1] = top_line + textpad[y + 1][x - 1:x + w + 1] = lab_line + textpad[y + 2][x - 1:x + w + 1] = crn_line + textpad[y + 3][x:x + w] = mid_line + textpad[y + 4][x:x + w] = mid_line + textpad[y + h - 1][x:x + w] = bot_line From 0adb449bdc98517022a71c49fe0134b7204addf9 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 5 Mar 2022 12:33:33 +1100 Subject: [PATCH 3/7] Tweak ISO enter label length calcs --- lib/python/qmk/keyboard.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index 8bd7a6ee735c..59502db7e4da 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -235,22 +235,22 @@ def render_key_isoenter(textpad, x, y, w, h, label, style): w = ceil(w * 4) h = ceil(h * 3) - label_len = w - 2 - label_leftover = label_len - len(label) + 1 + label_len = w - 1 + label_leftover = label_len - len(label) if len(label) > label_len: label = label[:label_len] - label_blank = ' ' * label_len - label_border_top = box_chars['h'] * (label_len + 1) - label_border = box_chars['h'] * label_len + label_blank = ' ' * (label_len - 1) + label_border_top = box_chars['h'] * label_len + label_border_bottom = box_chars['h'] * (label_len - 1) label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr']) lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) crn_line = array('u', box_chars['bl'] + box_chars['tr'] + label_blank + box_chars['v']) mid_line = array('u', box_chars['v'] + label_blank + box_chars['v']) - bot_line = array('u', box_chars['bl'] + label_border + box_chars['br']) + bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br']) textpad[y][x - 1:x + w + 1] = top_line textpad[y + 1][x - 1:x + w + 1] = lab_line From 9743f1b825fa0f1e10a06fdaf7121365c46cd7f8 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 5 Mar 2022 12:42:10 +1100 Subject: [PATCH 4/7] Formatting --- lib/python/qmk/keyboard.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index 59502db7e4da..defbb8f8cacd 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -200,6 +200,7 @@ def render_layouts(info_json, render_ascii): return layouts + def render_key_rect(textpad, x, y, w, h, label, style): box_chars = BOX_DRAWING_CHARACTERS[style] x = ceil(x * 4) @@ -228,6 +229,7 @@ def render_key_rect(textpad, x, y, w, h, label, style): textpad[y + i + 2][x:x + w] = mid_line textpad[y + h - 1][x:x + w] = bot_line + def render_key_isoenter(textpad, x, y, w, h, label, style): box_chars = BOX_DRAWING_CHARACTERS[style] x = ceil(x * 4) @@ -241,9 +243,9 @@ def render_key_isoenter(textpad, x, y, w, h, label, style): if len(label) > label_len: label = label[:label_len] - label_blank = ' ' * (label_len - 1) + label_blank = ' ' * (label_len-1) label_border_top = box_chars['h'] * label_len - label_border_bottom = box_chars['h'] * (label_len - 1) + label_border_bottom = box_chars['h'] * (label_len-1) label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr']) From 9f59eeffdd26c363fd26fe064c16c58fef2c70de Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 5 Mar 2022 12:59:56 +1100 Subject: [PATCH 5/7] Make yapf go away --- lib/python/qmk/keyboard.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index defbb8f8cacd..f72eef1d123f 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -216,7 +216,7 @@ def render_key_rect(textpad, x, y, w, h, label, style): label_blank = ' ' * label_len label_border = box_chars['h'] * label_len - label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * + label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * top_line = array('u', box_chars['tl'] + label_border + box_chars['tr']) lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) @@ -243,10 +243,10 @@ def render_key_isoenter(textpad, x, y, w, h, label, style): if len(label) > label_len: label = label[:label_len] - label_blank = ' ' * (label_len-1) + label_blank = ' ' * (label_len-1) # noqa: yapf insists there be no whitespace around - and * label_border_top = box_chars['h'] * label_len - label_border_bottom = box_chars['h'] * (label_len-1) - label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * + label_border_bottom = box_chars['h'] * (label_len-1) # noqa + label_middle = label + ' '*label_leftover # noqa top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr']) lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) From 868bc6e980eb013597a929f47680698aea15b083 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 5 Mar 2022 13:04:57 +1100 Subject: [PATCH 6/7] Make linter go away --- lib/python/qmk/keyboard.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index f72eef1d123f..f6a2782a82ca 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -216,7 +216,7 @@ def render_key_rect(textpad, x, y, w, h, label, style): label_blank = ' ' * label_len label_border = box_chars['h'] * label_len - label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * + label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around * top_line = array('u', box_chars['tl'] + label_border + box_chars['tr']) lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) @@ -243,10 +243,10 @@ def render_key_isoenter(textpad, x, y, w, h, label, style): if len(label) > label_len: label = label[:label_len] - label_blank = ' ' * (label_len-1) # noqa: yapf insists there be no whitespace around - and * + label_blank = ' ' * (label_len-1) # noqa: yapf insists there be no whitespace around - and * label_border_top = box_chars['h'] * label_len - label_border_bottom = box_chars['h'] * (label_len-1) # noqa - label_middle = label + ' '*label_leftover # noqa + label_border_bottom = box_chars['h'] * (label_len-1) # noqa + label_middle = label + ' '*label_leftover # noqa top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr']) lab_line = array('u', box_chars['v'] + label_middle + box_chars['v']) From ab69917755c6fd635df7303e4a839d19aa341093 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 6 Mar 2022 09:43:36 +1100 Subject: [PATCH 7/7] Update lib/python/qmk/keyboard.py Co-authored-by: Joel Challis --- lib/python/qmk/keyboard.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index f6a2782a82ca..0530c4acc5c4 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -254,9 +254,9 @@ def render_key_isoenter(textpad, x, y, w, h, label, style): mid_line = array('u', box_chars['v'] + label_blank + box_chars['v']) bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br']) - textpad[y][x - 1:x + w + 1] = top_line - textpad[y + 1][x - 1:x + w + 1] = lab_line - textpad[y + 2][x - 1:x + w + 1] = crn_line + textpad[y][x - 1:x + w] = top_line + textpad[y + 1][x - 1:x + w] = lab_line + textpad[y + 2][x - 1:x + w] = crn_line textpad[y + 3][x:x + w] = mid_line textpad[y + 4][x:x + w] = mid_line textpad[y + h - 1][x:x + w] = bot_line