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.
This commit is contained in:
Jared Miller 2026-02-11 20:43:04 -05:00
parent d18f21a031
commit 3be4370b2f
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
3 changed files with 22 additions and 5 deletions

View file

@ -1,6 +1,7 @@
"""Look command for viewing the world.""" """Look command for viewing the world."""
from mudlib.commands import CommandDefinition, register from mudlib.commands import CommandDefinition, register
from mudlib.commands.things import _format_thing_name
from mudlib.effects import get_effects_at from mudlib.effects import get_effects_at
from mudlib.entity import Entity from mudlib.entity import Entity
from mudlib.player import Player 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) obj for obj in zone.contents_at(player.x, player.y) if isinstance(obj, Thing)
] ]
if ground_items: 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") player.writer.write(f"On the ground: {names}\r\n")
await player.writer.drain() await player.writer.drain()

View file

@ -1,6 +1,7 @@
"""Get, drop, and inventory commands for items.""" """Get, drop, and inventory commands for items."""
from mudlib.commands import CommandDefinition, register from mudlib.commands import CommandDefinition, register
from mudlib.container import Container
from mudlib.player import Player from mudlib.player import Player
from mudlib.thing import Thing from mudlib.thing import Thing
from mudlib.zone import Zone from mudlib.zone import Zone
@ -32,6 +33,23 @@ def _find_thing_in_inventory(name: str, player: Player) -> Thing | None:
return 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: async def cmd_get(player: Player, args: str) -> None:
"""Pick up an item from the ground.""" """Pick up an item from the ground."""
if not args.strip(): if not args.strip():
@ -86,7 +104,7 @@ async def cmd_inventory(player: Player, args: str) -> None:
lines = ["You are carrying:\r\n"] lines = ["You are carrying:\r\n"]
for thing in things: 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)) await player.send("".join(lines))

View file

@ -74,9 +74,7 @@ async def test_look_shows_open_empty_container(player, test_zone, mock_writer):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_look_shows_open_container_with_contents( async def test_look_shows_open_container_with_contents(player, test_zone, mock_writer):
player, test_zone, mock_writer
):
"""look shows open containers with their contents.""" """look shows open containers with their contents."""
from mudlib.commands.look import cmd_look from mudlib.commands.look import cmd_look