Merge branch 'options_formatter' of https://github.com/InspectorCaracal/evennia into InspectorCaracal-options_formatter

This commit is contained in:
Griatch 2022-06-04 13:21:17 +02:00
commit 1fc5b36dcd

View file

@ -274,12 +274,13 @@ import inspect
from ast import literal_eval from ast import literal_eval
from fnmatch import fnmatch from fnmatch import fnmatch
from math import ceil
from inspect import isfunction, getargspec from inspect import isfunction, getargspec
from django.conf import settings from django.conf import settings
from evennia import Command, CmdSet from evennia import Command, CmdSet
from evennia.utils import logger from evennia.utils import logger
from evennia.utils.evtable import EvTable from evennia.utils.evtable import EvTable, EvColumn
from evennia.utils.ansi import strip_ansi from evennia.utils.ansi import strip_ansi
from evennia.utils.utils import mod_import, make_iter, pad, to_str, m_len, is_iter, dedent, crop from evennia.utils.utils import mod_import, make_iter, pad, to_str, m_len, is_iter, dedent, crop
from evennia.commands import cmdhandler from evennia.commands import cmdhandler
@ -1210,7 +1211,6 @@ class EvMenu:
Args: Args:
optionlist (list): List of (key, description) tuples for every optionlist (list): List of (key, description) tuples for every
option related to this node. option related to this node.
caller (Object, Account or None, optional): The caller of the node.
Returns: Returns:
options (str): The formatted option display. options (str): The formatted option display.
@ -1229,7 +1229,7 @@ class EvMenu:
table = [] table = []
for key, desc in optionlist: for key, desc in optionlist:
if key or desc: if key or desc:
desc_string = ": %s" % desc if desc else "" desc_string = f": {desc}" if desc else ""
table_width_max = max( table_width_max = max(
table_width_max, table_width_max,
max(m_len(p) for p in key.split("\n")) max(m_len(p) for p in key.split("\n"))
@ -1239,42 +1239,31 @@ class EvMenu:
raw_key = strip_ansi(key) raw_key = strip_ansi(key)
if raw_key != key: if raw_key != key:
# already decorations in key definition # already decorations in key definition
table.append(" |lc%s|lt%s|le%s" % (raw_key, key, desc_string)) table.append(f" |lc{raw_key}|lt{key}|le{desc_string}")
else: else:
# add a default white color to key # add a default white color to key
table.append(" |lc%s|lt|w%s|n|le%s" % (raw_key, raw_key, desc_string)) table.append(f" |lc{raw_key}|lt|w{key}|n|le{desc_string}")
ncols = _MAX_TEXT_WIDTH // table_width_max # number of ncols ncols = _MAX_TEXT_WIDTH // table_width_max # number of columns
if ncols < 0: if ncols < 0:
# no visible option at all # no visible options at all
return "" return ""
ncols = ncols + 1 if ncols == 0 else ncols ncols = 1 if ncols == 0 else ncols
# get the amount of rows needed (start with 4 rows)
nrows = 4
while nrows * ncols < nlist:
nrows += 1
ncols = nlist // nrows # number of full columns
nlastcol = nlist % nrows # number of elements in last column
# get the final column count # minimum number of rows in a column
ncols = ncols + 1 if nlastcol > 0 else ncols min_rows = 4
if ncols > 1:
# only extend if longer than one column
table.extend([" " for i in range(nrows - nlastcol)])
# build the actual table grid # split the items into columns
table = [table[icol * nrows : (icol * nrows) + nrows] for icol in range(0, ncols)] split = max(min_rows, ceil(len(table)/ncols))
max_end = len(table)
cols_list = []
for icol in range(ncols):
start = icol*split
end = min(start+split,max_end)
cols_list.append(EvColumn(*table[start:end]))
# adjust the width of each column return str(EvTable(table=cols_list, border="none"))
for icol in range(len(table)):
col_width = (
max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep
)
table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]]
# format the table into columns
return str(EvTable(table=table, border="none"))
def node_formatter(self, nodetext, optionstext): def node_formatter(self, nodetext, optionstext):
""" """