Add prompt command for customization

This commit is contained in:
Jared Miller 2026-02-13 22:39:09 -05:00
parent 4930f1408b
commit 593bfd3028
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
3 changed files with 131 additions and 0 deletions

View file

@ -0,0 +1,4 @@
name = "prompt"
help = "view or customize your prompt"
mode = "*"
handler = "mudlib.commands.prompt:cmd_prompt"

View file

@ -0,0 +1,51 @@
"""Prompt customization command."""
from mudlib.player import Player
from mudlib.prompt import DEFAULT_TEMPLATES
async def cmd_prompt(player: Player, args: str) -> None:
"""View or customize the player's prompt.
With no arguments, shows the current template and available variables.
With a template string, sets the player's custom prompt.
With 'reset', clears the custom template back to mode default.
"""
args = args.strip()
if not args:
# Show current template and available variables
current = player.prompt_template or DEFAULT_TEMPLATES.get(
player.mode, "{stamina_gauge} {pl} > "
)
msg = f"Current prompt: {current}\r\n\r\n"
msg += "Available variables:\r\n"
msg += " stamina_pct - stamina percentage (0-100)\r\n"
msg += " stamina - current stamina value\r\n"
msg += " max_stamina - maximum stamina\r\n"
msg += " stamina_gauge - colored stamina percentage\r\n"
msg += " pl - current power level\r\n"
msg += " max_pl - maximum power level\r\n"
msg += " name - your character name\r\n"
msg += " mode - current mode (normal, combat, etc)\r\n"
msg += " x, y - coordinates\r\n"
msg += " opponent - combat opponent name\r\n"
msg += " move - current attack/defense move\r\n"
msg += " combat_state - combat state (idle/fighting)\r\n"
msg += "\r\n"
msg += "Examples:\r\n"
msg += " prompt {stamina_gauge} {pl} > (simple, default)\r\n"
msg += " prompt {stamina_gauge} {pl}/{max_pl} [{name}] > (full)\r\n"
msg += " prompt reset (back to default)\r\n"
await player.send(msg)
return
if args.lower() == "reset":
# Clear custom template
player.prompt_template = None
await player.send("Prompt reset to default.\r\n")
return
# Set custom template
player.prompt_template = args
await player.send(f"Prompt set to: {args}\r\n")

View file

@ -0,0 +1,76 @@
"""Tests for the prompt command."""
from unittest.mock import AsyncMock, MagicMock
import pytest
from mudlib.commands.prompt import cmd_prompt
from mudlib.prompt import render_prompt
@pytest.fixture
def mock_writer():
writer = MagicMock()
writer.write = MagicMock()
writer.drain = AsyncMock()
return writer
@pytest.fixture
def mock_reader():
return MagicMock()
@pytest.fixture
def player(mock_reader, mock_writer):
from mudlib.player import Player
return Player(name="TestPlayer", x=5, y=5, reader=mock_reader, writer=mock_writer)
@pytest.mark.asyncio
async def test_prompt_bare_shows_current_and_variables(player):
"""prompt with no args shows current template and available variables."""
await cmd_prompt(player, "")
output = "".join([call[0][0] for call in player.writer.write.call_args_list])
assert "current" in output.lower()
assert "variables" in output.lower() or "available" in output.lower()
# Should list some variables
assert "stamina" in output.lower()
@pytest.mark.asyncio
async def test_prompt_set_custom_template(player):
"""prompt <template> sets player.prompt_template."""
custom = "{pl} >"
await cmd_prompt(player, custom)
assert player.prompt_template == custom
# Should confirm it was set
output = "".join([call[0][0] for call in player.writer.write.call_args_list])
assert "set" in output.lower() or "prompt" in output.lower()
@pytest.mark.asyncio
async def test_prompt_reset_clears_template(player):
"""prompt reset clears player.prompt_template back to None."""
player.prompt_template = "{pl} > "
await cmd_prompt(player, "reset")
assert player.prompt_template is None
# Should confirm reset
output = "".join([call[0][0] for call in player.writer.write.call_args_list])
assert "reset" in output.lower() or "default" in output.lower()
@pytest.mark.asyncio
async def test_prompt_custom_template_used_in_render(player):
"""After setting custom template, render_prompt uses it."""
custom = "[{stamina}] > "
player.prompt_template = custom
player.stamina = 50
player.max_stamina = 100
player.pl = 10
player.max_pl = 20
result = render_prompt(player)
# Should contain the stamina value from our custom template
assert "[50]" in result or "[50.0]" in result