Skip to content

Commit

Permalink
Merge branch 'tui' of github.com:willmcgugan/rich into tui
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jun 9, 2021
2 parents 74efdcd + c5f286d commit 74ba2e1
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 29 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ jobs:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: Install and configure Poetry
uses: snok/install-poetry@v1.1.1
uses: snok/install-poetry@v1.1.6
with:
version: 1.1.4
virtualenvs-create: false
version: 1.1.6
virtualenvs-in-project: true
- name: Install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- name: Format check with black
run: make format-check
run: |
source $VENV
make format-check
- name: Typecheck with mypy
run: make typecheck
run: |
source $VENV
make typecheck
- name: Test with pytest
run: |
pip install .
python -m pytest tests -v --cov=./rich --cov-report=xml:./coverage.xml --cov-report term-missing
source $VENV
pytest tests -v --cov=./rich --cov-report=xml:./coverage.xml --cov-report term-missing
- name: Upload code coverage
uses: codecov/codecov-action@v1.0.10
with:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Console.height setter
- Added angular style Rich reprs

### Changed

- Changed the logic for retrieving the calling frame in console logs to a faster one for the Python implementations that support it.

## [10.2.2] - 2021-05-19

### Fixed
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ The following people have contributed to the development of Rich:
- [Will McGugan](https://github.com/willmcgugan)
- [Nathan Page](https://github.com/nathanrpage97)
- [Clément Robert](https://github.com/neutrinoceros)
- [Gabriele N. Tornetta](https://github.com/p403n1x87)
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
alabaster==0.7.12
Sphinx==4.0.1
Sphinx==4.0.2
sphinx-rtd-theme==0.5.2
sphinx-copybutton==0.3.1
32 changes: 23 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include = ["rich/py.typed"]
[tool.poetry.dependencies]
python = "^3.6"
typing-extensions = {version = "^3.7.4", python = "<3.8"}
dataclasses = {version=">=0.7,<0.9", python = "~3.6"}
dataclasses = {version=">=0.7,<0.9", python = "~3.6"}
pygments = "^2.6.0"
commonmark = "^0.9.0"
colorama = "^0.4.0"
Expand All @@ -41,7 +41,7 @@ jupyter = ["ipywidgets"]
pytest = "^6.2.3"
black = "^20.8b1"
mypy = "^0.812"
pytest-cov = "^2.12.0"
pytest-cov = "^2.12.1"
attrs = "^21.2.0"

[build-system]
Expand Down
51 changes: 41 additions & 10 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from getpass import getpass
from itertools import islice
from time import monotonic
from types import TracebackType
from types import FrameType, TracebackType
from typing import (
IO,
TYPE_CHECKING,
Expand All @@ -25,6 +25,7 @@
NamedTuple,
Optional,
TextIO,
Tuple,
Type,
Tuple,
Union,
Expand Down Expand Up @@ -1665,6 +1666,41 @@ def print_exception(
)
self.print(traceback)

@staticmethod
def _caller_frame_info(
offset: int,
currentframe: Callable[[], Optional[FrameType]] = inspect.currentframe,
) -> Tuple[str, int, Dict[str, Any]]:
"""Get caller frame information.
Args:
offset (int): the caller offset within the current frame stack.
currentframe (Callable[[], Optional[FrameType]], optional): the callable to use to
retrieve the current frame. Defaults to ``inspect.currentframe``.
Returns:
Tuple[str, int, Dict[str, Any]]: A tuple containing the filename, the line number and
the dictionary of local variables associated with the caller frame.
Raises:
RuntimeError: If the stack offset is invalid.
"""
# Ignore the frame of this local helper
offset += 1

frame = currentframe()
if frame is not None:
# Use the faster currentframe where implemented
while offset and frame:
frame = frame.f_back
offset -= 1
assert frame is not None
return frame.f_code.co_filename, frame.f_lineno, frame.f_locals
else:
# Fallback to the slower stack
frame_info = inspect.stack()[offset]
return frame_info.filename, frame_info.lineno, frame_info.frame.f_locals

def log(
self,
*objects: Any,
Expand Down Expand Up @@ -1710,18 +1746,13 @@ def log(
if style is not None:
renderables = [Styled(renderable, style) for renderable in renderables]

caller = inspect.stack()[_stack_offset]
link_path = (
None
if caller.filename.startswith("<")
else os.path.abspath(caller.filename)
)
path = caller.filename.rpartition(os.sep)[-1]
line_no = caller.lineno
filename, line_no, locals = self._caller_frame_info(_stack_offset)
link_path = None if filename.startswith("<") else os.path.abspath(filename)
path = filename.rpartition(os.sep)[-1]
if log_locals:
locals_map = {
key: value
for key, value in caller.frame.f_locals.items()
for key, value in locals.items()
if not key.startswith("__")
}
renderables.append(render_scope(locals_map, title="[i]locals"))
Expand Down
7 changes: 7 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def test_log():
assert rendered == expected


def test_log_caller_frame_info():
for i in range(2):
assert Console._caller_frame_info(i) == Console._caller_frame_info(
i, lambda: None
)


def test_justify():
console = Console(width=20, log_path=False, log_time=False, color_system=None)
console.begin_capture()
Expand Down

0 comments on commit 74ba2e1

Please sign in to comment.