-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67a09cb
commit d162112
Showing
5 changed files
with
105 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from datetime import datetime | ||
from typing import ( | ||
Any, | ||
) | ||
|
||
from rich.console import Group, RenderableType | ||
from rich.pretty import Pretty | ||
from rich.text import Text | ||
from textual.app import ComposeResult | ||
from textual.events import Print | ||
from textual.widgets import ( | ||
RichLog, | ||
Static, | ||
) | ||
|
||
|
||
class Logger(Static): | ||
def compose(self) -> ComposeResult: | ||
yield RichLog(highlight=True, markup=True, wrap=True) | ||
|
||
def on_mount(self): | ||
self.begin_capture_print() | ||
|
||
def on_print(self, event: Print) -> None: | ||
self.wite(event.text, event.stderr) | ||
|
||
def wite(self, message: Any, is_stderr: bool): | ||
if isinstance(message, str) and message.strip() == "": | ||
# FIXME: Why do we need this hack?! | ||
return | ||
logger: RichLog = self.query_one(RichLog) | ||
if isinstance(message, (RenderableType, str)): | ||
logger.write( | ||
Group( | ||
Text( | ||
datetime.now().strftime("[%H:%M] "), | ||
style="dim cyan" if not is_stderr else "bold red", | ||
end="", | ||
), | ||
message, | ||
), | ||
) | ||
else: | ||
ppable, pp_msg = True, None | ||
try: | ||
pp_msg = Pretty(message) | ||
except Exception: | ||
ppable = False | ||
if ppable and pp_msg is not None: | ||
logger.write( | ||
Group( | ||
Text( | ||
datetime.now().strftime("[%H:%M] "), | ||
style="dim cyan", | ||
end="", | ||
), | ||
Text(str(type(message)) + " ", style="italic blue", end=""), | ||
pp_msg, | ||
) | ||
) | ||
else: | ||
try: | ||
logger.write( | ||
Group( | ||
Text( | ||
datetime.now().strftime("[%H:%M] "), | ||
style="dim cyan", | ||
end="", | ||
), | ||
message, | ||
), | ||
) | ||
except Exception as e: | ||
logger.write( | ||
Group( | ||
Text( | ||
datetime.now().strftime("[%H:%M] "), | ||
style="dim cyan", | ||
end="", | ||
), | ||
Text("Logging error: ", style="bold red"), | ||
Text(str(e), style="bold red"), | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters