"""log~~~An object for displaying a history of updating messages, such as alog."""fromcollectionsimportdequefromdataclassesimportdataclassfromtypingimportOptional,Sequencefromthurible.panelimportContent,Message,Title# Available update message.
[docs]@dataclassclassUpdate(Message):"""Create a new :class:`thurible.log.Update` object. This object is a command message used to instruct the currently displayed :class:`thurible.Log` to add the text given in the message. :param text: The message to add to the panel. :return: An :class:`thurible.Update` object. :rtype: thurible.Update :usage: To create a new :class:`thurible.Update` object: .. testcode:: import thurible update = thurible.Update('spam') """text:str
# Class.
[docs]classLog(Content,Title):"""Create a new :class:`thurible.Log` object. This class displays messages from the application in "last in first out" (LIFO) format. It's intended for situations were you want to provide the user a rolling display of status messages. As a subclass of :class:`thurible.panel.Content` and :class:`thurible.panel.Title`, it can also take those parameters and has those public methods and properties. :param content: (Optional.) A sequence of strings to display in the panel when it is first displayed in the terminal. The first item in the sequence is considered the most recent. :param maxlen: (Optional.) The total number of entries the :class:`thurible.Log` will store. This is used to allow the terminal window to be resized without causing the loss of any messages. It's not intended for the user to be able to scroll to view messages that have rolled off the terminal. :return: A :class:`Log` object. :rtype: thurible.Log :usage: To create a new :class:`thurible.Log` object: .. testcode:: import thurible dialog = thurible.Log() To create a new :class:`thurible.Log` object that will show a maximum of three messages at a time and starts with a welcome message: .. testsetup:: log import thurible log = thurible.Log(['Welcome!',], maxlen=3) .. testcode:: log log = thurible.Log(['Welcome!',], maxlen=3) To update the messages in a log use a :class:`thurible.Update` message: .. testcode:: log update = thurible.Update('spam') log.update(update) Information on the sizing of :class:`thurible.Log` objects can be found in the :ref:`sizing` section below. """def__init__(self,content:Optional[Sequence[str]]=None,maxlen:int=50,*args,**kwargs)->None:super().__init__(*args,**kwargs)self.maxlen=maxlenifcontentisNone:content=deque(maxlen=self.maxlen)elifnotisinstance(content,deque):d:deque[str]=deque(maxlen=self.maxlen)foritemincontent:d.appendleft(item)content=dself.content=contentself._wrapped_width=-1def__str__(self)->str:"""Return a string that will draw the entire panel."""# Set up.inner_height=self.inner_heighty=self.inner_yx=self.inner_xresult=super().__str__()# Write the contents of the log.result+=self._visible(self.lines,self.inner_height,self.inner_x,self.inner_y)returnresult# Properties.@propertydeflines(self)->list[str]:"""The lines of text available to be displayed in the panel after they have been wrapped to fit the width of the interior of the panel. A message from the application may be split into multiple lines. :return: A :class:`list` object containing each line of text as a :class:`str`. :rtype: list """width=self.inner_widthifwidth!=self._wrapped_width:wrapped=[]forlineinself.content:wrapped.extend(self.term.wrap(line,width=width))self._lines=wrappedself._wrapped_width=widthreturnself._lines# Public methods.