Make help pagination not mess up colors. Resolve #2646

This commit is contained in:
Griatch 2022-09-22 23:30:25 +02:00
parent 8ee3702efd
commit 377d366d3e
4 changed files with 48 additions and 27 deletions

View file

@ -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 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.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 import create, evmore
from evennia.utils.ansi import ANSIString from evennia.utils.ansi import ANSIString
from evennia.help.filehelp import FILE_HELP_ENTRIES
from evennia.utils.eveditor import EvEditor from evennia.utils.eveditor import EvEditor
from evennia.utils.utils import class_from_module, inherits_from, format_grid, pad from evennia.utils.utils import class_from_module, dedent, format_grid, inherits_from, pad
from evennia.help.utils import help_search_with_index, parse_entry_for_subcategories
CMD_IGNORE_PREFIXES = settings.CMD_IGNORE_PREFIXES CMD_IGNORE_PREFIXES = settings.CMD_IGNORE_PREFIXES
COMMAND_DEFAULT_CLASS = class_from_module(settings.COMMAND_DEFAULT_CLASS) COMMAND_DEFAULT_CLASS = class_from_module(settings.COMMAND_DEFAULT_CLASS)
@ -174,7 +173,11 @@ class CmdHelp(COMMAND_DEFAULT_CLASS):
else: else:
subtopics = [f"|w{topic}/{subtop}|n" for subtop in subtopics] subtopics = [f"|w{topic}/{subtop}|n" for subtop in subtopics]
subtopics = "\n|CSubtopics:|n\n {}".format( 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: else:
subtopics = "" subtopics = ""
@ -186,7 +189,11 @@ class CmdHelp(COMMAND_DEFAULT_CLASS):
else: else:
suggested = [f"|w{sug}|n" for sug in suggested] suggested = [f"|w{sug}|n" for sug in suggested]
suggested = "\n|COther topic suggestions:|n\n{}".format( 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: else:
suggested = "" suggested = ""
@ -280,7 +287,13 @@ class CmdHelp(COMMAND_DEFAULT_CLASS):
+ self.index_topic_clr + self.index_topic_clr
) )
grid, verbatim_elements = _group_by_category(cmd_help_dict) 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 "" cmd_grid = ANSIString("\n").join(gridrows) if gridrows else ""
if any(db_help_dict.values()): if any(db_help_dict.values()):
@ -291,7 +304,13 @@ class CmdHelp(COMMAND_DEFAULT_CLASS):
+ self.index_topic_clr + self.index_topic_clr
) )
grid, verbatim_elements = _group_by_category(db_help_dict) 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 "" db_grid = ANSIString("\n").join(gridrows) if gridrows else ""
# only show the main separators if there are actually both cmd and db-based help # only show the main separators if there are actually both cmd and db-based help

View file

@ -62,15 +62,11 @@ Xterm256 greyscale:
""" """
import functools import functools
import re import re
from collections import OrderedDict from collections import OrderedDict
from django.conf import settings from django.conf import settings
from evennia.utils import logger, utils
from evennia.utils import utils
from evennia.utils import logger
from evennia.utils.utils import to_str from evennia.utils.utils import to_str
MXP_ENABLED = settings.MXP_ENABLED MXP_ENABLED = settings.MXP_ENABLED

View file

@ -37,14 +37,14 @@ the `caller.msg()` construct every time the page is updated.
""" """
from django.conf import settings from django.conf import settings
from django.db.models.query import QuerySet
from django.core.paginator import Paginator from django.core.paginator import Paginator
from evennia.commands.command import Command from django.db.models.query import QuerySet
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.utils.translation import gettext as _ 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_NOMATCH = cmdhandler.CMD_NOMATCH
_CMD_NOINPUT = cmdhandler.CMD_NOINPUT _CMD_NOINPUT = cmdhandler.CMD_NOINPUT
@ -60,7 +60,7 @@ _LBR = ANSIString("\n")
# text # text
_DISPLAY = """{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): class CmdMore(Command):

View file

@ -1860,7 +1860,7 @@ def percentile(iterable, percent, key=lambda x: x):
return d0 + d1 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 This helper function makes a 'grid' output, where it distributes the given
string-elements as evenly as possible to fill out the given width. 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 by padding unless filling the entire line. This is useful for embedding
decorations in the grid, such as horizontal bars. decorations in the grid, such as horizontal bars.
ignore_ansi (bool, optional): Ignore ansi markups when calculating white spacing. 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: Returns:
list: The grid as a list of ready-formatted rows. We return it 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: if sum(display_len((element)) for element in elements) <= width:
# grid fits in one line # grid fits in one line
return _minimal_rows(elements) rows = _minimal_rows(elements)
else: else:
# full multi-line grid # 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(): def get_evennia_pids():