Add timestamps to output messages

This commit is contained in:
DevilXD
2022-12-07 13:49:52 +01:00
parent 57baa4fc1e
commit 51b2da3e42
2 changed files with 23 additions and 19 deletions

20
gui.py
View File

@@ -673,7 +673,7 @@ class CampaignProgress:
class ConsoleOutput:
def __init__(self, manager: GUIManager, master: ttk.Widget):
frame = ttk.LabelFrame(master, text=_("gui", "output"), padding=(4, 0, 4, 4))
frame.grid(column=0, row=3, columnspan=2, sticky="nsew", padx=2)
frame.grid(column=0, row=3, columnspan=3, sticky="nsew", padx=2)
# tell master frame that the containing row can expand
master.rowconfigure(3, weight=1)
frame.rowconfigure(0, weight=1) # let the frame expand
@@ -696,9 +696,12 @@ class ConsoleOutput:
xscroll.grid(column=0, row=1, sticky="ew")
yscroll.grid(column=1, row=0, sticky="ns")
def print(self, *values, sep: str = ' ', end: str = '\n'):
def print(self, message: str):
stamp = datetime.now().strftime("%X")
if '\n' in message:
message = message.replace('\n', f"\n{stamp}: ")
self._text.config(state="normal")
self._text.insert("end", f"{sep.join(values)}{end}")
self._text.insert("end", f"{stamp}: {message}\n")
self._text.see("end") # scroll to the newly added line
self._text.config(state="disabled")
@@ -713,7 +716,7 @@ class ChannelList:
def __init__(self, manager: GUIManager, master: ttk.Widget):
self._manager = manager
frame = ttk.LabelFrame(master, text=_("gui", "channels", "name"), padding=(4, 0, 4, 4))
frame.grid(column=2, row=1, rowspan=3, sticky="nsew", padx=2)
frame.grid(column=2, row=1, rowspan=2, sticky="nsew", padx=2)
# tell master frame that the containing column can expand
master.columnconfigure(2, weight=1)
frame.rowconfigure(1, weight=1)
@@ -1993,9 +1996,9 @@ class GUIManager:
self.progress.display(None)
self.tray.update_title(None)
def print(self, *args, **kwargs):
def print(self, message: str):
# print to our custom output
self.output.print(*args, **kwargs)
self.output.print(message)
###################
@@ -2193,6 +2196,11 @@ if __name__ == "__main__":
)
campaign = drop.campaign
await gui.inv.add_campaign(campaign)
gui.print("Single-line test message")
await asyncio.sleep(1)
gui.print("Multi-line\ntest\nmessage")
# Tray
# gui.tray.minimize()
await asyncio.sleep(2)

View File

@@ -639,11 +639,11 @@ class Twitch:
"""
self.gui.prevent_close()
def print(self, *args, **kwargs):
def print(self, message: str):
"""
Can be used to print messages within the GUI.
"""
self.gui.print(*args, **kwargs)
self.gui.print(message)
def save(self, *, force: bool = False) -> None:
"""
@@ -785,7 +785,7 @@ class Twitch:
self.change_state(State.CHANNELS_FETCH)
else:
# with no games available, we switch to IDLE after cleanup
self.gui.print(_("status", "no_campaign"))
self.print(_("status", "no_campaign"))
self.change_state(State.IDLE)
elif self._state is State.CHANNELS_FETCH:
self.gui.status.update(_("gui", "status", "gathering"))
@@ -908,7 +908,7 @@ class Twitch:
self._state_change.clear()
else:
# not watching anything and there isn't anything to watch either
self.gui.print(_("status", "no_channel"))
self.print(_("status", "no_channel"))
self.change_state(State.IDLE)
elif self._state is State.EXIT:
self.gui.status.update(_("gui", "status", "exiting"))
@@ -1095,7 +1095,7 @@ class Twitch:
and self.should_switch(channel) # and we should!
):
self.watch(channel)
self.gui.print(_("status", "goes_online").format(channel=channel.name))
self.print(_("status", "goes_online").format(channel=channel.name))
self.gui.status.update(
_("gui", "status", "watching").format(channel=channel.name)
)
@@ -1107,7 +1107,7 @@ class Twitch:
# change the channel if we're currently watching it
watching_channel = self.watching_channel.get_with_default(None)
if watching_channel is not None and watching_channel == channel:
self.gui.print(_("status", "goes_offline").format(channel=channel.name))
self.print(_("status", "goes_offline").format(channel=channel.name))
self.change_state(State.CHANNEL_SWITCH)
else:
logger.debug(f"{channel.name} goes OFFLINE")
@@ -1140,9 +1140,7 @@ class Twitch:
)
# two different claim texts, becase a new line after the game name
# looks ugly in the output window - replace it with a space
self.gui.print(
_("status", "claimed_drop").format(drop=claim_text.replace('\n', ' '))
)
self.print(_("status", "claimed_drop").format(drop=claim_text.replace('\n', ' ')))
self.gui.tray.notify(claim_text, _("gui", "tray", "notification_title"))
else:
logger.error(f"Drop claim failed! Drop ID: {drop_id}")
@@ -1233,14 +1231,12 @@ class Twitch:
if channel is not None:
channel.points = balance
channel.display()
self.gui.print(
_("status", "earned_points").format(points=f"{points:3}", balance=balance)
)
self.print(_("status", "earned_points").format(points=f"{points:3}", balance=balance))
elif msg_type == "claim-available":
claim_data = message["data"]["claim"]
points = claim_data["point_gain"]["total_points"]
await self.claim_points(claim_data["channel_id"], claim_data["id"])
self.gui.print(_("status", "claimed_points").format(points=points))
self.print(_("status", "claimed_points").format(points=points))
async def get_auth(self) -> _AuthState:
await self._auth_state.validate()