Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qmk info: Add --ascii flag #10793

Merged
merged 7 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/python/qmk/cli/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Compile an info.json for a particular keyboard and pretty-print it.
"""
import json
import platform

from milc import cli

Expand All @@ -16,7 +17,7 @@
COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'


def show_keymap(kb_info_json, title_caps=True):
def show_keymap(kb_info_json, render_ascii, title_caps=True):
fauxpark marked this conversation as resolved.
Show resolved Hide resolved
"""Render the keymap in ascii art.
"""
keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
Expand All @@ -36,19 +37,19 @@ def show_keymap(kb_info_json, title_caps=True):
else:
cli.echo('{fg_cyan}layer_%s{fg_reset}:', layer_num)

print(render_layout(kb_info_json['layouts'][layout_name]['layout'], layer))
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], render_ascii, layer))


def show_layouts(kb_info_json, title_caps=True):
def show_layouts(kb_info_json, render_ascii, title_caps=True):
"""Render the layouts with info.json labels.
"""
for layout_name, layout_art in render_layouts(kb_info_json).items():
for layout_name, layout_art in render_layouts(kb_info_json, render_ascii).items():
title = layout_name.title() if title_caps else layout_name
cli.echo('{fg_cyan}%s{fg_reset}:', title)
print(layout_art) # Avoid passing dirty data to cli.echo()


def show_matrix(kb_info_json, title_caps=True):
def show_matrix(kb_info_json, render_ascii, title_caps=True):
"""Render the layout with matrix labels in ascii art.
"""
for layout_name, layout in kb_info_json['layouts'].items():
Expand All @@ -69,7 +70,7 @@ def show_matrix(kb_info_json, title_caps=True):
else:
cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name)

print(render_layout(kb_info_json['layouts'][layout_name]['layout'], labels))
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], render_ascii, labels))


def print_friendly_output(kb_info_json):
Expand All @@ -91,13 +92,13 @@ def print_friendly_output(kb_info_json):
cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))

if cli.config.info.layouts:
show_layouts(kb_info_json, True)
show_layouts(kb_info_json, cli.config.info.ascii, True)

if cli.config.info.matrix:
show_matrix(kb_info_json, True)
show_matrix(kb_info_json, cli.config.info.ascii, True)

if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
show_keymap(kb_info_json, True)
show_keymap(kb_info_json, cli.config.info.ascii, True)


def print_text_output(kb_info_json):
Expand All @@ -110,20 +111,21 @@ def print_text_output(kb_info_json):
cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])

if cli.config.info.layouts:
show_layouts(kb_info_json, False)
show_layouts(kb_info_json, cli.config.info.ascii, False)

if cli.config.info.matrix:
show_matrix(kb_info_json, False)
show_matrix(kb_info_json, cli.config.info.ascii, False)

if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
show_keymap(kb_info_json, False)
show_keymap(kb_info_json, cli.config.info.ascii, False)


@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
@cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
@cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
@cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
@cli.argument('--ascii', action='store_true', help='Render layout box drawings in ASCII only.')
@cli.subcommand('Keyboard information.')
@automagic_keyboard
@automagic_keymap
Expand All @@ -132,14 +134,19 @@ def info(cli):
"""
# Determine our keyboard(s)
if not cli.config.info.keyboard:
cli.log.error('Missing paramater: --keyboard')
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False

if not is_keyboard(cli.config.info.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
return False

platform_id = platform.platform().lower()

if 'windows' in platform_id:
cli.config.info.ascii = True
fauxpark marked this conversation as resolved.
Show resolved Hide resolved

# Build the info.json file
kb_info_json = info_json(cli.config.info.keyboard)

Expand Down
36 changes: 28 additions & 8 deletions lib/python/qmk/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
from qmk.c_parse import parse_config_h_file
from qmk.makefile import parse_rules_mk_file

BOX_DRAWING_CHARACTERS = { # noqa: this reads better across multiple lines
"unicode": {
"tl": "┌",
"tr": "┐",
"bl": "└",
"br": "┘",
"v": "│",
"h": "─"
fauxpark marked this conversation as resolved.
Show resolved Hide resolved
},
"ascii": {
"tl": " ",
"tr": " ",
"bl": "|",
"br": "|",
"v": "|",
"h": "_"
}
}

base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep


Expand Down Expand Up @@ -72,10 +91,11 @@ def rules_mk(keyboard):
return rules


def render_layout(layout_data, key_labels=None):
def render_layout(layout_data, render_ascii, key_labels=None):
"""Renders a single layout.
"""
textpad = [array('u', ' ' * 200) for x in range(50)]
style = 'ascii' if render_ascii else 'unicode'
fauxpark marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +94 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A thought- it might be more future-proof to do this so we can have other layout styles in the future. Dots? Letters?

Suggested change
def render_layout(layout_data, render_ascii, key_labels=None):
"""Renders a single layout.
"""
textpad = [array('u', ' ' * 200) for x in range(50)]
style = 'ascii' if render_ascii else 'unicode'
def render_layout(layout_data, style='unicode', key_labels=None):
"""Renders a single layout.
Args:
style
The style for the box drawing characters. 'ascii' or 'unicode'
key_labels
A sequence of label strings to write on top of the keys
"""
textpad = [array('u', ' ' * 200) for x in range(50)]
style = 'ascii' if render_ascii else 'unicode'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did experiment with other styles a bit. In the end I found that the current ascii style here, with underscores for the horizontals and pipes for everything else, looked the least awful.

This looks way too messy, for example:
image


for key in layout_data:
x = ceil(key.get('x', 0) * 4)
Expand All @@ -97,13 +117,13 @@ def render_layout(layout_data, key_labels=None):
label = label[:label_len]

label_blank = ' ' * label_len
label_border = '─' * label_len
label_border = BOX_DRAWING_CHARACTERS[style]['h'] * label_len
label_middle = label + ' '*label_leftover # noqa: yapf insists there be no whitespace around *

top_line = array('u', '┌' + label_border + '┐')
lab_line = array('u', '│' + label_middle + '│')
mid_line = array('u', '│' + label_blank + '│')
bot_line = array('u', '└' + label_border + "┘")
top_line = array('u', BOX_DRAWING_CHARACTERS[style]['tl'] + label_border + BOX_DRAWING_CHARACTERS[style]['tr'])
lab_line = array('u', BOX_DRAWING_CHARACTERS[style]['v'] + label_middle + BOX_DRAWING_CHARACTERS[style]['v'])
mid_line = array('u', BOX_DRAWING_CHARACTERS[style]['v'] + label_blank + BOX_DRAWING_CHARACTERS[style]['v'])
bot_line = array('u', BOX_DRAWING_CHARACTERS[style]['bl'] + label_border + BOX_DRAWING_CHARACTERS[style]['br'])

textpad[y][x:x + w] = top_line
textpad[y + 1][x:x + w] = lab_line
Expand All @@ -119,13 +139,13 @@ def render_layout(layout_data, key_labels=None):
return '\n'.join(lines)


def render_layouts(info_json):
def render_layouts(info_json, render_ascii):
"""Renders all the layouts from an `info_json` structure.
"""
layouts = {}

for layout in info_json['layouts']:
layout_data = info_json['layouts'][layout]['layout']
layouts[layout] = render_layout(layout_data)
layouts[layout] = render_layout(layout_data, render_ascii)

return layouts