"""Common formatting utilities."""
import json
from typing import Any
from ..config import CHARACTER_LIMIT
def format_currency(amount: str, currency: str) -> str:
"""Format currency amount with symbol."""
symbols = {
"USD": "$", "EUR": "€", "GBP": "£", "CAD": "C$",
"AUD": "A$", "JPY": "¥", "CNY": "¥"
}
symbol = symbols.get(currency, currency)
return f"{symbol}{amount}"
def format_duration(duration: str) -> str:
"""
Format ISO 8601 duration into human-readable format.
Args:
duration: ISO 8601 duration string (e.g., "PT2H30M")
Returns:
Human-readable duration (e.g., "2h 30m")
"""
# Parse PT2H30M format
if not duration.startswith("PT"):
return duration
time_part = duration[2:] # Remove "PT"
hours = 0
minutes = 0
if "H" in time_part:
h_parts = time_part.split("H")
hours = int(h_parts[0])
time_part = h_parts[1] if len(h_parts) > 1 else ""
if "M" in time_part:
minutes = int(time_part.split("M")[0])
if hours and minutes:
return f"{hours}h {minutes}m"
elif hours:
return f"{hours}h"
elif minutes:
return f"{minutes}m"
return duration
def truncate_text(text: str, max_length: int = CHARACTER_LIMIT) -> str:
"""Truncate text to maximum character limit."""
if len(text) <= max_length:
return text
return text[:max_length - 50] + "\n\n... [Response truncated due to length]"
def format_json_response(data: Any) -> str:
"""Format data as pretty-printed JSON."""
return json.dumps(data, indent=2, ensure_ascii=False)