From 377d366d3eb19e969cd5dc21c9a4ef2050308a95 Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 22 Sep 2022 23:30:25 +0200 Subject: [PATCH] Make help pagination not mess up colors. Resolve #2646 --- evennia/commands/default/help.py | 43 +++++++++++++++++++++++--------- evennia/utils/ansi.py | 6 +---- evennia/utils/evmore.py | 14 +++++------ evennia/utils/utils.py | 12 ++++++--- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/evennia/commands/default/help.py b/evennia/commands/default/help.py index 0f98dc2685..126ba13234 100644 --- a/evennia/commands/default/help.py +++ b/evennia/commands/default/help.py @@ -8,19 +8,18 @@ outside the game in modules given by ``settings.FILE_HELP_ENTRY_MODULES``. """ -import re -from itertools import chain -from dataclasses import dataclass -from django.conf import settings from collections import defaultdict -from evennia.utils.utils import dedent +from dataclasses import dataclass +from itertools import chain + +from django.conf import settings +from evennia.help.filehelp import FILE_HELP_ENTRIES from evennia.help.models import HelpEntry +from evennia.help.utils import help_search_with_index, parse_entry_for_subcategories from evennia.utils import create, evmore from evennia.utils.ansi import ANSIString -from evennia.help.filehelp import FILE_HELP_ENTRIES from evennia.utils.eveditor import EvEditor -from evennia.utils.utils import class_from_module, inherits_from, format_grid, pad -from evennia.help.utils import help_search_with_index, parse_entry_for_subcategories +from evennia.utils.utils import class_from_module, dedent, format_grid, inherits_from, pad CMD_IGNORE_PREFIXES = settings.CMD_IGNORE_PREFIXES COMMAND_DEFAULT_CLASS = class_from_module(settings.COMMAND_DEFAULT_CLASS) @@ -174,7 +173,11 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): else: subtopics = [f"|w{topic}/{subtop}|n" for subtop in subtopics] subtopics = "\n|CSubtopics:|n\n {}".format( - "\n ".join(format_grid(subtopics, width=self.client_width())) + "\n ".join( + format_grid( + subtopics, width=self.client_width(), line_prefix=self.index_topic_clr + ) + ) ) else: subtopics = "" @@ -186,7 +189,11 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): else: suggested = [f"|w{sug}|n" for sug in suggested] suggested = "\n|COther topic suggestions:|n\n{}".format( - "\n ".join(format_grid(suggested, width=self.client_width())) + "\n ".join( + format_grid( + suggested, width=self.client_width(), line_prefix=self.index_topic_clr + ) + ) ) else: suggested = "" @@ -280,7 +287,13 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): + self.index_topic_clr ) grid, verbatim_elements = _group_by_category(cmd_help_dict) - gridrows = format_grid(grid, width, sep=" ", verbatim_elements=verbatim_elements) + gridrows = format_grid( + grid, + width, + sep=" ", + verbatim_elements=verbatim_elements, + line_prefix=self.index_topic_clr, + ) cmd_grid = ANSIString("\n").join(gridrows) if gridrows else "" if any(db_help_dict.values()): @@ -291,7 +304,13 @@ class CmdHelp(COMMAND_DEFAULT_CLASS): + self.index_topic_clr ) grid, verbatim_elements = _group_by_category(db_help_dict) - gridrows = format_grid(grid, width, sep=" ", verbatim_elements=verbatim_elements) + gridrows = format_grid( + grid, + width, + sep=" ", + verbatim_elements=verbatim_elements, + line_prefix=self.index_topic_clr, + ) db_grid = ANSIString("\n").join(gridrows) if gridrows else "" # only show the main separators if there are actually both cmd and db-based help diff --git a/evennia/utils/ansi.py b/evennia/utils/ansi.py index 77403a01b2..551484cb96 100644 --- a/evennia/utils/ansi.py +++ b/evennia/utils/ansi.py @@ -62,15 +62,11 @@ Xterm256 greyscale: """ import functools - import re from collections import OrderedDict from django.conf import settings - -from evennia.utils import utils -from evennia.utils import logger - +from evennia.utils import logger, utils from evennia.utils.utils import to_str MXP_ENABLED = settings.MXP_ENABLED diff --git a/evennia/utils/evmore.py b/evennia/utils/evmore.py index da8f04ad5a..bc0958cba4 100644 --- a/evennia/utils/evmore.py +++ b/evennia/utils/evmore.py @@ -37,14 +37,14 @@ the `caller.msg()` construct every time the page is updated. """ from django.conf import settings -from django.db.models.query import QuerySet from django.core.paginator import Paginator -from evennia.commands.command import Command -from evennia.commands.cmdset import CmdSet -from evennia.commands import cmdhandler -from evennia.utils.ansi import ANSIString -from evennia.utils.utils import make_iter, inherits_from, justify, dedent +from django.db.models.query import QuerySet from django.utils.translation import gettext as _ +from evennia.commands import cmdhandler +from evennia.commands.cmdset import CmdSet +from evennia.commands.command import Command +from evennia.utils.ansi import ANSIString +from evennia.utils.utils import dedent, inherits_from, justify, make_iter _CMD_NOMATCH = cmdhandler.CMD_NOMATCH _CMD_NOINPUT = cmdhandler.CMD_NOINPUT @@ -60,7 +60,7 @@ _LBR = ANSIString("\n") # text _DISPLAY = """{text} -(|wPage|n [{pageno}/{pagemax}] |wn|next|n || |wp|nrevious || |wt|nop || |we|nnd || |wq|nuit)""" +|n(|wPage|n [{pageno}/{pagemax}] |wn|next|n || |wp|nrevious || |wt|nop || |we|nnd || |wq|nuit)""" class CmdMore(Command): diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index d598dde7e9..1fed76df21 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -1860,7 +1860,7 @@ def percentile(iterable, percent, key=lambda x: x): return d0 + d1 -def format_grid(elements, width=78, sep=" ", verbatim_elements=None): +def format_grid(elements, width=78, sep=" ", verbatim_elements=None, line_prefix=""): """ This helper function makes a 'grid' output, where it distributes the given string-elements as evenly as possible to fill out the given width. @@ -1878,6 +1878,8 @@ def format_grid(elements, width=78, sep=" ", verbatim_elements=None): by padding unless filling the entire line. This is useful for embedding decorations in the grid, such as horizontal bars. ignore_ansi (bool, optional): Ignore ansi markups when calculating white spacing. + line_prefix (str, optional): A prefix to add at the beginning of each line. + This can e.g. be used to preserve line color across line breaks. Returns: list: The grid as a list of ready-formatted rows. We return it @@ -1988,10 +1990,14 @@ def format_grid(elements, width=78, sep=" ", verbatim_elements=None): if sum(display_len((element)) for element in elements) <= width: # grid fits in one line - return _minimal_rows(elements) + rows = _minimal_rows(elements) else: # full multi-line grid - return _weighted_rows(elements) + rows = _weighted_rows(elements) + + if line_prefix: + return [line_prefix + row for row in rows] + return rows def get_evennia_pids():