From 3be4370b2f9cbac60c9fa5f0af81c2f2ed6cab78 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Wed, 11 Feb 2026 20:43:04 -0500 Subject: [PATCH] Show container state in look and inventory display Containers now display their state when viewed: - Closed containers show "(closed)" - Open empty containers show "(open, empty)" - Open containers with items show "(open, containing: item1, item2)" This applies to both ground items in the look command and inventory items. Added _format_thing_name helper to both look.py and things.py to handle the display formatting consistently. --- src/mudlib/commands/look.py | 3 ++- src/mudlib/commands/things.py | 20 +++++++++++++++++++- tests/test_container_display.py | 4 +--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/mudlib/commands/look.py b/src/mudlib/commands/look.py index 3e96e9e..1b0e6c7 100644 --- a/src/mudlib/commands/look.py +++ b/src/mudlib/commands/look.py @@ -1,6 +1,7 @@ """Look command for viewing the world.""" from mudlib.commands import CommandDefinition, register +from mudlib.commands.things import _format_thing_name from mudlib.effects import get_effects_at from mudlib.entity import Entity from mudlib.player import Player @@ -106,7 +107,7 @@ async def cmd_look(player: Player, args: str) -> None: obj for obj in zone.contents_at(player.x, player.y) if isinstance(obj, Thing) ] if ground_items: - names = ", ".join(item.name for item in ground_items) + names = ", ".join(_format_thing_name(item) for item in ground_items) player.writer.write(f"On the ground: {names}\r\n") await player.writer.drain() diff --git a/src/mudlib/commands/things.py b/src/mudlib/commands/things.py index 44bce1b..f602ad7 100644 --- a/src/mudlib/commands/things.py +++ b/src/mudlib/commands/things.py @@ -1,6 +1,7 @@ """Get, drop, and inventory commands for items.""" from mudlib.commands import CommandDefinition, register +from mudlib.container import Container from mudlib.player import Player from mudlib.thing import Thing from mudlib.zone import Zone @@ -32,6 +33,23 @@ def _find_thing_in_inventory(name: str, player: Player) -> Thing | None: return None +def _format_thing_name(thing: Thing) -> str: + """Format a thing's name with container state if applicable.""" + if not isinstance(thing, Container): + return thing.name + + if thing.closed: + return f"{thing.name} (closed)" + + # Container is open + contents = [obj for obj in thing.contents if isinstance(obj, Thing)] + if not contents: + return f"{thing.name} (open, empty)" + + names = ", ".join(item.name for item in contents) + return f"{thing.name} (open, containing: {names})" + + async def cmd_get(player: Player, args: str) -> None: """Pick up an item from the ground.""" if not args.strip(): @@ -86,7 +104,7 @@ async def cmd_inventory(player: Player, args: str) -> None: lines = ["You are carrying:\r\n"] for thing in things: - lines.append(f" {thing.name}\r\n") + lines.append(f" {_format_thing_name(thing)}\r\n") await player.send("".join(lines)) diff --git a/tests/test_container_display.py b/tests/test_container_display.py index 6ff9050..b333da6 100644 --- a/tests/test_container_display.py +++ b/tests/test_container_display.py @@ -74,9 +74,7 @@ async def test_look_shows_open_empty_container(player, test_zone, mock_writer): @pytest.mark.asyncio -async def test_look_shows_open_container_with_contents( - player, test_zone, mock_writer -): +async def test_look_shows_open_container_with_contents(player, test_zone, mock_writer): """look shows open containers with their contents.""" from mudlib.commands.look import cmd_look