Skip to content

Commit

Permalink
Make use of enrich (#31)
Browse files Browse the repository at this point in the history
This ease use of rich by hiding lots of hacks.
  • Loading branch information
ssbarnea authored Nov 23, 2020
1 parent ca5ba12 commit 9b376e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ repos:
additional_dependencies:
- click-help-colors
- click-option-group
- enrich
- packaging
- pygithub
- rich
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
hooks:
Expand Down
6 changes: 1 addition & 5 deletions lib/gri/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from click_help_colors import HelpColorsGroup
from requests.exceptions import HTTPError
from rich import box
from rich.logging import RichHandler
from rich.markdown import Markdown
from rich.table import Table

Expand Down Expand Up @@ -39,12 +38,9 @@ def command_line_wrapper(func):
@wraps(func)
def inner_func(*args, **kwargs):
# before
handler = RichHandler(show_time=False, show_path=False)
LOG.addHandler(handler)

ctx = args[0]
LOG.setLevel(get_logging_level(ctx))
LOG.info("Called with %s", ctx.params)
LOG.debug("Called with %s", ctx.params)

if " " in ctx.params["user"]:
ctx.params["user"] = f"\"{ctx.params['user']}\""
Expand Down
27 changes: 24 additions & 3 deletions lib/gri/console.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
import sys

import rich
from rich.console import Console, ConsoleOptions, RenderResult
from enrich.console import Console
from enrich.logging import RichHandler
from rich.console import ConsoleOptions, RenderResult
from rich.markdown import CodeBlock, Markdown
from rich.syntax import Syntax
from rich.terminal_theme import TerminalTheme
Expand Down Expand Up @@ -46,8 +49,26 @@

def bootstrap() -> Console:
Markdown.elements["code_block"] = MyCodeBlock

# We also initialize the logging console
logging_console = Console(file=sys.stderr, force_terminal=1, theme=theme)

logger = logging.getLogger() # type: logging.Logger
# logger.setLevel(logging.DEBUG)

handler = RichHandler(
console=logging_console, show_time=False, show_path=False, markup=True
) # type: ignore
# logger.addHandler(handler)
logger.handlers = [handler]
logger.propagate = False

return Console(
theme=theme, highlighter=rich.highlighter.ReprHighlighter(), record=True
theme=theme,
highlighter=rich.highlighter.ReprHighlighter(),
record=True,
soft_wrap=True,
redirect=True,
)


Expand Down Expand Up @@ -83,7 +104,7 @@ def get_logging_level(ctx) -> int:
class MyCodeBlock(CodeBlock):
# pylint: disable=unused-argument
def __rich_console__(
self, console: Console, options: ConsoleOptions
self, console: rich.console.Console, options: ConsoleOptions
) -> RenderResult:
code = str(self.text).rstrip()
syntax = Syntax(code, self.lexer_name, theme=self.theme)
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ setup_requires =

# These are required in actual runtime:
install_requires =
click>=7.1.2
click-help-colors>=0.6
click>=7.1.2
dataclasses; python_version<"3.7"
enrich>=1.2.1
pygithub
pyyaml>=5.3.1
requests
rich>=6.2.0

[options.entry_points]
console_scripts =
Expand Down

0 comments on commit 9b376e8

Please sign in to comment.