Skip to content

Commit

Permalink
menu: allow names in card content and simplify define single card decks
Browse files Browse the repository at this point in the history
Signed-off-by: Janar Sööt <janar.soot@gmail.com>
  • Loading branch information
mcmatrix authored and KevinOConnor committed Jan 8, 2019
1 parent b9cccc5 commit 005cbe1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
9 changes: 9 additions & 0 deletions config/example-menu.cfg
Expand Up @@ -171,6 +171,11 @@
#items:
# It accepts only 'card' elements. You are able to switch between different card screens
# by using encoder or up/down buttons.
#content:
# It allows quickly define single card decks by adding content directly to deck.
# You have to remove deck item attribute and use named items in content.
# The menu functionality will then internally create one card item for this deck.
# This is optional.

#[menu card1]
#type: card
Expand All @@ -179,6 +184,10 @@
# Card screen content. Each line represents display line.
# Quotes can be used in the beginning and end of line.
# Rendered elements are available for output formatting as {0}..{x}. It's always string type.
# It's possible directly use menu item names in content by leaving items attribute out or empty
# and use menu items names directly in content as {msg,xpos|ypos}. The menu functionality will then
# internally build a item list and replace names with indexes in content.
# This is optional.
#items:
# List of elements in card. Each line represents a single index for content formatting.
# It's possible to show multiple elements in one place by separating them with comma on single line.
Expand Down
44 changes: 41 additions & 3 deletions klippy/extras/display/menu.py
Expand Up @@ -5,7 +5,7 @@
# Copyright (C) 2018 Janar Sööt <janar.soot@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, logging, sys, ast, re
import os, logging, sys, ast, re, string


class error(Exception):
Expand Down Expand Up @@ -573,7 +573,7 @@ def __init__(self, manager, config, namespace='', sep=','):
self._sep = sep
self._show_back = False
self.selected = None
self.items = config.get('items')
self.items = config.get('items', '')

def is_accepted(self, item):
return (super(MenuGroup, self).is_accepted(item)
Expand Down Expand Up @@ -739,7 +739,7 @@ def __init__(self, manager, config, namespace=''):
super(MenuList, self).__init__(manager, config, namespace)
self._enter_gcode = config.get('enter_gcode', None)
self._leave_gcode = config.get('leave_gcode', None)
self.items = config.get('items')
self.items = config.get('items', '')

def is_accepted(self, item):
return (super(MenuList, self).is_accepted(item)
Expand Down Expand Up @@ -799,6 +799,32 @@ class MenuCard(MenuGroup):
def __init__(self, manager, config, namespace=''):
super(MenuCard, self).__init__(manager, config, namespace)
self.content = config.get('content')
if not self.items:
self.content = self._parse_content_items(self.content)

def _parse_content_items(self, content):
formatter = string.Formatter()
out = ""
items = []

try:
parsed_content = list(formatter.parse(content))
except Exception:
logging.exception("Card content parsing error")

for part in parsed_content:
# (literal_text, field_name, format_spec, conversion)
out += part[0]
if part[1]:
out += "{%s%s%s}" % (
len(items),
("!" + part[3]) if part[3] else '',
(":" + part[2]) if part[2] else '',
)
items.append(str(part[1]))

self.items = "\n".join(items)
return out

def _names_aslist(self):
return self._lines_aslist(self.items)
Expand Down Expand Up @@ -853,6 +879,18 @@ def __init__(self, manager, config, namespace=''):
self.menu = None
self._show_back = False
self._show_title = False
if not self.items:
card = MenuCard(self._manager, {
'name': ' '.join([self._name, 'Card']),
'use_cursor': config.get('use_cursor', 'false'),
'allow_without_selection': config.get(
'allow_without_selection', 'true'),
'content': config.get('content')
}, self.namespace)
name = " ".join(
config.get_name().split()[1:]) + "__singlecarddeck__"
self._manager.add_menuitem(name, card)
self.items = name

def _populate_menu(self):
self.menu = None
Expand Down

0 comments on commit 005cbe1

Please sign in to comment.