Skip to content

Commit

Permalink
rich.logging
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 26, 2020
1 parent 505b45c commit d4a60fe
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 14 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1] - 1020-01-22
## [0.3.2] - 2020-01-26

### Added

- Added rich.logging

## [0.3.1] - 2020-01-22

### Added

Expand Down
10 changes: 6 additions & 4 deletions examples/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class RequestHighlighter(RegexHighlighter):

time.sleep(1)

request_highlighter = RequestHighlighter()

console.log(
"HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]",
highlighter=RequestHighlighter(),
request_highlighter("HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]"),
)

console.log(
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]",
highlighter=RequestHighlighter(),
request_highlighter(
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]"
),
)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "0.3.1"
version = "0.3.2"
description = "Render rich text, tables, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <[email protected]>"]
license = "MIT"
Expand Down
12 changes: 10 additions & 2 deletions rich/_log_render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any, Iterable, List, Optional, TYPE_CHECKING
from typing import Any, Iterable, List, Optional, TYPE_CHECKING, Union

from .text import Text

Expand All @@ -12,10 +12,12 @@ class LogRender:
def __init__(
self,
show_time: bool = True,
show_level: bool = False,
show_path: bool = True,
time_format: str = "[%x %X] ",
) -> None:
self.show_time = show_time
self.show_level = show_level
self.show_path = show_path
self.time_format = time_format
self._last_time: Optional[str] = None
Expand All @@ -25,6 +27,8 @@ def __call__(
console: "Console",
renderables: Iterable["ConsoleRenderable"],
log_time: datetime = None,
time_format: str = None,
level: Union[str, Text] = "",
path: str = None,
line_no: int = None,
) -> "Table":
Expand All @@ -34,19 +38,23 @@ def __call__(
output = Table(show_header=False, expand=True, box=None, padding=0)
if self.show_time:
output.add_column(style="log.time")
if self.show_level:
output.add_column(style="log.level", width=9)
output.add_column(ratio=1, style="log.message")
if self.show_path and path:
output.add_column(style="log.path")
row: List["RenderableType"] = []
if self.show_time:
if log_time is None:
log_time = datetime.now()
log_time_display = log_time.strftime(self.time_format)
log_time_display = log_time.strftime(time_format or self.time_format)
if log_time_display == self._last_time:
row.append(Text(" " * len(log_time_display)))
else:
row.append(Text(log_time_display))
self._last_time = log_time_display
if self.show_level:
row.append(level)
row.append(Renderables(renderables))
if self.show_path and path:
if line_no is None:
Expand Down
18 changes: 14 additions & 4 deletions rich/default_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@
"magenta": Style(color="magenta"),
"cyan": Style(color="cyan"),
"white": Style(color="white"),
"logging.keyword": Style(bold=True, color="yellow"),
"logging.level.notset": Style(dim=True),
"logging.level.debug": Style(color="green"),
"logging.level.info": Style(color="blue"),
"logging.level.warning": Style(color="red"),
"logging.level.error": Style(color="red", bold=True),
"logging.level.critical": Style(color="red", bold=True, reverse=True),
"log.level": Style(),
"log.time": Style(color="cyan", dim=True),
"log.message": Style(),
"log.path": Style(dim=True),
"repr.str": Style(color="green", italic=False),
"repr.str": Style(color="green", italic=False, bold=False),
"repr.brace": Style(bold=True),
"repr.tag_start": Style(bold=True),
"repr.tag_name": Style(color="bright_magenta"),
"repr.tag_contents": Style(color="default", italic=True),
"repr.tag_name": Style(color="bright_magenta", bold=True),
"repr.tag_contents": Style(color="default"),
"repr.tag_end": Style(bold=True),
"repr.attrib_name": Style(color="yellow", italic=True),
"repr.attrib_equal": Style(bold=True),
Expand All @@ -56,9 +64,11 @@
"repr.bool_true": Style(color="bright_green", italic=True),
"repr.bool_false": Style(color="bright_red", italic=True),
"repr.none": Style(color="magenta", italic=True),
"repr.url": Style(underline=True, color="default"),
"repr.url": Style(underline=True, color="bright_blue", bold=False),
"rule.line": Style(color="green"),
"rule.text": Style(),
"repr.path": Style(color="magenta"),
"repr.filename": Style(color="bright_magenta", bold=True),
"table.header": Style(bold=True),
"table.footer": Style(bold=True),
"table.cell": Style(),
Expand Down
2 changes: 2 additions & 0 deletions rich/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class ReprHighlighter(RegexHighlighter):
r"(?P<bool_true>True)|(?P<bool_false>False)|(?P<none>None)",
r"(?P<number>\-?[0-9]+\.?[0-9]*)",
r"(?P<number>0x[0-9a-f]*)",
r"(?P<path>(\/\w+)+\/)",
r"(?P<filename>\/\w*\..{3,4})\s",
r"(?P<str>b?\'\'\'.*?\'\'\'|b?\'.*?\'|b?\"\"\".*?\"\"\"|b?\".*?\")",
r"(?P<url>https?:\/\/\S*)",
]
Expand Down
24 changes: 22 additions & 2 deletions rich/text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from operator import itemgetter
import re
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -189,9 +190,28 @@ def stylize(self, start: int, end: int, style: Union[str, Style]) -> None:
return
self._spans.append(Span(max(0, start), min(length, end), style))

def rstrip(self) -> None:
"""Trip whitespace from end of text
def highlight_words(self, words: Iterable[str], style: Union[str, Style]) -> int:
"""Highlight words with a style.
Args:
words (Iterable[str]): Worlds to highlight.
style (Union[str, Style]): Style to apply.
Returns:
int: Number of words highlighted.
"""
re_words = "|".join(re.escape(word) for word in words)
add_span = self._spans.append
count = 0
_Span = Span
for match in re.finditer(re_words, self.text):
start, end = match.span(0)
add_span(_Span(start, end, style))
count += 1
return count

def rstrip(self) -> None:
"""Trip whitespace from end of text."""
self.text = self.text.rstrip()

def set_length(self, new_length: int) -> None:
Expand Down

0 comments on commit d4a60fe

Please sign in to comment.