Skip to content

Commit 9b376e8

Browse files
authored
Make use of enrich (#31)
This ease use of rich by hiding lots of hacks.
1 parent ca5ba12 commit 9b376e8

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ repos:
5151
additional_dependencies:
5252
- click-help-colors
5353
- click-option-group
54+
- enrich
5455
- packaging
5556
- pygithub
56-
- rich
5757
- repo: https://github.com/pre-commit/mirrors-pylint
5858
rev: v2.6.0
5959
hooks:

lib/gri/__main__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from click_help_colors import HelpColorsGroup
1212
from requests.exceptions import HTTPError
1313
from rich import box
14-
from rich.logging import RichHandler
1514
from rich.markdown import Markdown
1615
from rich.table import Table
1716

@@ -39,12 +38,9 @@ def command_line_wrapper(func):
3938
@wraps(func)
4039
def inner_func(*args, **kwargs):
4140
# before
42-
handler = RichHandler(show_time=False, show_path=False)
43-
LOG.addHandler(handler)
44-
4541
ctx = args[0]
4642
LOG.setLevel(get_logging_level(ctx))
47-
LOG.info("Called with %s", ctx.params)
43+
LOG.debug("Called with %s", ctx.params)
4844

4945
if " " in ctx.params["user"]:
5046
ctx.params["user"] = f"\"{ctx.params['user']}\""

lib/gri/console.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import logging
2+
import sys
23

34
import rich
4-
from rich.console import Console, ConsoleOptions, RenderResult
5+
from enrich.console import Console
6+
from enrich.logging import RichHandler
7+
from rich.console import ConsoleOptions, RenderResult
58
from rich.markdown import CodeBlock, Markdown
69
from rich.syntax import Syntax
710
from rich.terminal_theme import TerminalTheme
@@ -46,8 +49,26 @@
4649

4750
def bootstrap() -> Console:
4851
Markdown.elements["code_block"] = MyCodeBlock
52+
53+
# We also initialize the logging console
54+
logging_console = Console(file=sys.stderr, force_terminal=1, theme=theme)
55+
56+
logger = logging.getLogger() # type: logging.Logger
57+
# logger.setLevel(logging.DEBUG)
58+
59+
handler = RichHandler(
60+
console=logging_console, show_time=False, show_path=False, markup=True
61+
) # type: ignore
62+
# logger.addHandler(handler)
63+
logger.handlers = [handler]
64+
logger.propagate = False
65+
4966
return Console(
50-
theme=theme, highlighter=rich.highlighter.ReprHighlighter(), record=True
67+
theme=theme,
68+
highlighter=rich.highlighter.ReprHighlighter(),
69+
record=True,
70+
soft_wrap=True,
71+
redirect=True,
5172
)
5273

5374

@@ -83,7 +104,7 @@ def get_logging_level(ctx) -> int:
83104
class MyCodeBlock(CodeBlock):
84105
# pylint: disable=unused-argument
85106
def __rich_console__(
86-
self, console: Console, options: ConsoleOptions
107+
self, console: rich.console.Console, options: ConsoleOptions
87108
) -> RenderResult:
88109
code = str(self.text).rstrip()
89110
syntax = Syntax(code, self.lexer_name, theme=self.theme)

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ setup_requires =
5656

5757
# These are required in actual runtime:
5858
install_requires =
59-
click>=7.1.2
6059
click-help-colors>=0.6
60+
click>=7.1.2
6161
dataclasses; python_version<"3.7"
62+
enrich>=1.2.1
6263
pygithub
6364
pyyaml>=5.3.1
6465
requests
65-
rich>=6.2.0
6666

6767
[options.entry_points]
6868
console_scripts =

0 commit comments

Comments
 (0)