49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from typing import List, Reversible, Tuple
|
|
from textwrap import TextWrapper
|
|
|
|
from tcod.console import Console
|
|
|
|
import colors
|
|
|
|
#util class
|
|
class Message:
|
|
def __init__(self, text: str, color: Tuple[int, int, int] = colors.white, count: int = 1):
|
|
self.raw_text = text
|
|
self.color = color
|
|
self.count = count
|
|
|
|
@property
|
|
def full_text(self) -> str:
|
|
if self.count > 1:
|
|
return f"{self.raw_text} (x{self.count})"
|
|
return self.raw_text
|
|
|
|
|
|
class MessageLog:
|
|
def __init__(self):
|
|
self.messages: List[Message] = []
|
|
|
|
def add_message(self, text: str, color: Tuple[int, int, int] = colors.terminal_light, *, stack: bool = True) -> None:
|
|
if stack and self.messages and text == self.messages[-1].raw_text:
|
|
self.messages[-1].count += 1
|
|
else:
|
|
self.messages.append(Message(text, color))
|
|
|
|
def push_messages(self, msg_list: List[Message]) -> None:
|
|
self.messages.extend(msg_list)
|
|
|
|
def render(self, console: Console, x: int, y: int, width: int, height: int) -> None:
|
|
self.render_messages(console, x, y, width, height, self.messages)
|
|
|
|
@staticmethod
|
|
def render_messages(console: Console, x: int, y: int, width: int, height: int, messages: Reversible[Message]) -> None:
|
|
y_offset = height - 1
|
|
|
|
wrapper = TextWrapper(width=width, subsequent_indent = " ")
|
|
|
|
for message in reversed(messages):
|
|
for line in reversed(wrapper.wrap(message.full_text)): #oh, neat
|
|
console.print(x=x,y=y + y_offset,string=line,fg=message.color)
|
|
y_offset -= 1
|
|
if y_offset < 0:
|
|
return |