From 283f02210890db487dad88be9032780f0156dc22 Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Wed, 30 Jul 2025 22:49:29 -0700 Subject: [PATCH] Paginate long books, like the book on Alchemy --- commands/everyone.py | 30 +++++++++++++++++++----------- typeclasses/objects.py | 17 +++++++++++++++++ world/alchemy-book.md | 24 ++++++++++++++---------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/commands/everyone.py b/commands/everyone.py index e30bd98..b931e85 100755 --- a/commands/everyone.py +++ b/commands/everyone.py @@ -3,11 +3,11 @@ from random import random from re import split, sub, MULTILINE -from django.conf import settings from evennia.commands.default.general import CmdGet, NumberedTargetCommand from evennia.commands.default.muxcommand import MuxCommand from evennia.contrib.rpg.rpsystem import send_emote -from evennia.utils import evmore, iter_to_str, logger +from evennia.utils import iter_to_str, logger +from evennia.utils.evmore import EvMore from commands.command import Command from typeclasses.characters import Character @@ -485,24 +485,32 @@ class CmdRead(Command): if prefix: prefix = prefix + "|/" if contents.startswith("file:"): - self.show_file(reader, contents[5:], prefix or "") + self.show_file(reader, contents[5:], prefix, + self.client_width(), reader.client_height()) else: reader.msg(prefix + contents) - def show_file(self, reader, filename, prefix): + def show_file(self, reader, filename, prefix, width, height): """ Display a file to the user. The file is _somewhat_ Markdown formatted. """ - width = self.client_width() - 1 with open(filename, "r") as myfile: - no_data = sub(r">.*", "", myfile.read(), MULTILINE) - brk = '─' if reader.utf() else '-' - no_head = sub(r"#.*", '|W' + (brk * width) + '|n', - no_data, MULTILINE) - buf = prefix + paragraph(no_head) - evmore.msg(reader, buf) + buf = myfile.read() + session = reader.sessions.get()[0] + width = self.client_width() + tidied = md_to_evennia(buf, reader.utf(), width) + if prefix: + tidied = prefix + "|/" + tidied + EvMore(reader, tidied, session=session, + justify=True, justify_kwargs={"width": width}) + +def md_to_evennia(text, utf, width): + brk = '─' if utf else '-' + no_data = sub(r">.*[\n\r]+", "", text, MULTILINE) + return sub(r"#.*", '|W' + (brk * width) + '|n', + no_data, MULTILINE) class CmdTake(CmdGet, NumberedTargetCommand): """ diff --git a/typeclasses/objects.py b/typeclasses/objects.py index 49de87b..fddd417 100755 --- a/typeclasses/objects.py +++ b/typeclasses/objects.py @@ -11,6 +11,7 @@ with a location in the game world (like Characters, Rooms, Exits). from re import split, match, sub, IGNORECASE +from django.conf import settings from evennia.contrib.rpg.rpsystem.rpsystem import ContribRPObject from evennia.prototypes.spawner import spawn from evennia.utils import delay, logger @@ -252,6 +253,22 @@ class Object(ObjectParent, ContribRPObject): return None + def client_height(self): + """ + Get the client screenheight for the session using this command. + + Returns: + client height (int): The height (in characters) of the client window. + + Not sure why this isn't part of the engine. + """ + if self.sessions: + session = self.sessions.get()[0] + return session.protocol_flags.get( + "SCREENHEIGHT", {0: settings.CLIENT_DEFAULT_HEIGHT} + )[0] + return settings.CLIENT_DEFAULT_HEIGHT + def characters_here(self, puppets=False): """ Return a list of characters in the current location. diff --git a/world/alchemy-book.md b/world/alchemy-book.md index f6f348c..9fd7086 100644 --- a/world/alchemy-book.md +++ b/world/alchemy-book.md @@ -1,13 +1,14 @@ Covered in herbs, mushrooms and moss, this land invites me to produce potions. To work such craft, one must have the proper equipment, which I imported from Twilight Spire outside Mistwood. Most experiments have been successful, and I record them here. -|BElixir Risorium|n +# -------------------------------------------------------------------------------- + |bElixir Risorium|n +# -------------------------------------------------------------------------------- -While saccharine, this potion induces a state of euphoria finding everything, including one's own thoughts, hilarious. -Enjoyable, and easy to make. +While saccharine, this potion induces a state of euphoria finding everything, including one's own thoughts, hilarious. Enjoyable, and easy to make. |wIngredients|n - - 1 Gigglecap mushrooms + - 1 Gigglecap mushroom - 1 Bunch of tickleweed - 2 cups of fizzy water @@ -15,14 +16,16 @@ I have found Gigglecaps growing on well drained, rocky soil and even on rocks. T |wProcedure|n -Step 1, |gempty|n the cauldron -Step 2. |gadd|n all ingredients to the caldron -Step 3. |gcreate|n the potion using a low heat and the magic incantation -Step 4. |gbottle|n the concoction into a vial + Step 1. |gempty|n the cauldron + Step 2. |gadd|n all ingredients to the cauldron + Step 3. |gcreate|n the potion with the magic incantation + Step 4. |gbottle|n the concoction into a vial I have taught |mImpetus|n the incantation for the spell, which makes brewing potions much easier and quicker. Just remember to stir the potion with the wooden spoon. -|BSomnium Illusorium|n +# -------------------------------------------------------------------------------- + |bSomnium Illusorium|n +# -------------------------------------------------------------------------------- This fascinating potion can create a dream-like state while awake, including visions of sight and sound. One must be careful with this, as it could disrupt one perception of reality. @@ -42,8 +45,9 @@ I’ve noticed Pixies often dance in the marshlands to the East, but are often i |wProcedure|n Step 1, |gempty|n the cauldron -Step 2. |gadd|n all ingredients to the caldron +Step 2. |gadd|n all ingredients to the cauldron Step 3. |gcreate|n the potion by stirring the potion clockwise for exactly seven rotations Step 4. |gbottle|n the concoction into a vial +# End |bNote: This is still a work in progress... What do you think of this idea? You would gather herbs and whatnot, put them in the cauldron and then make potions! Seems intriguing...|n