From 1a11608e8b99f3e1233c97b5aed973d47ab7abe9 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 28 Mar 2020 15:41:15 +0000 Subject: [PATCH] add windows legacy support --- CHANGELOG.md | 14 ++++++++++++++ pyproject.toml | 5 +++-- rich/__main__.py | 2 +- rich/console.py | 29 +++++++++++++++++++++++------ rich/table.py | 38 ++++++++++++++++++++------------------ 5 files changed, 61 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16ed7403..a3ad5f73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ 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.8.4] - 2020-03-28 + +### Added + +- Added 'test card', run python -m rich + +### Changed + +- Detected windows terminal, defaulting to colorama support + +### Fixed + +- Fixed table scaling issue + ## [0.8.3] - 2020-03-27 ### Fixed diff --git a/pyproject.toml b/pyproject.toml index 83efb8f5..15ff9265 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/willmcgugan/rich" documentation = "https://rich.readthedocs.io/en/latest/" -version = "0.8.3" +version = "0.8.4" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" authors = ["Will McGugan "] license = "MIT" @@ -25,8 +25,9 @@ python = "^3.6" pprintpp = "^0.4.0" typing-extensions = "^3.7.4" dataclasses = {version="^0.7", python = "~3.6"} -pygments = "^2.5.0" +pygments = "^2.6.1" commonmark = "^0.9.0" +colorama = "^0.4.3" [tool.poetry.dev-dependencies] diff --git a/rich/__main__.py b/rich/__main__.py index ee34cb03..41ddbf26 100644 --- a/rich/__main__.py +++ b/rich/__main__.py @@ -149,7 +149,7 @@ def iter_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]: Supports much of the *markdown*, __syntax__! - Headers -- Basic formatting: bold, italic, code etc +- Basic formatting: **bold**, *italic*, `code` - Block quotes - Lists, and more... """ diff --git a/rich/console.py b/rich/console.py index df8c4a55..50a0e128 100644 --- a/rich/console.py +++ b/rich/console.py @@ -221,6 +221,13 @@ class ConsoleThreadLocals(threading.local): control: List[str] = field(default_factory=list) +def _enable_legacy_windows_support() -> None: + """Initialize Windows legacy support.""" + from colorama import init + + init() + + class Console: """A high level console interface. @@ -264,7 +271,7 @@ class Console: self._styles = ChainMap( themes.DEFAULT.styles if theme is None else theme.styles ) - self.file = file or sys.stdout + self._width = width self._height = height self.tab_size = tab_size @@ -272,13 +279,21 @@ class Console: self._markup = markup self._emoji = emoji self._highlight = highlight + self.legacy_windows = "WINDIR" in os.environ and not "WT_SESSION" in os.environ - if color_system is None: - self._color_system = None - elif color_system == "auto": - self._color_system = self._detect_color_system() + self._color_system: Optional[ColorSystem] + if self.legacy_windows: + _enable_legacy_windows_support() + self.file = file or sys.stdout + self._color_system = COLOR_SYSTEMS["windows"] else: - self._color_system = COLOR_SYSTEMS[color_system] + self.file = file or sys.stdout + if color_system is None: + self._color_system = None + elif color_system == "auto": + self._color_system = self._detect_color_system() + else: + self._color_system = COLOR_SYSTEMS[color_system] self._log_render = LogRender( show_time=log_time, show_path=log_path, time_format=log_time_format @@ -409,6 +424,8 @@ class Console: return ConsoleDimensions(self._width, self._height) width, height = shutil.get_terminal_size() + if self.legacy_windows: + width -= 1 return ConsoleDimensions( width if self._width is None else self._width, height if self._height is None else self._height, diff --git a/rich/table.py b/rich/table.py index cece8ee9..ef22f60b 100644 --- a/rich/table.py +++ b/rich/table.py @@ -419,7 +419,7 @@ class Table: for column_index, column in enumerate(self.columns) ) ) - box = self.box + _box = box.SQUARE if (console.legacy_windows and self.box) else self.box new_line = Segment.line() columns = self.columns @@ -427,8 +427,8 @@ class Table: show_footer = self.show_footer show_edge = self.show_edge - if box and show_edge: - yield Segment(box.get_top(widths), border_style) + if _box and show_edge: + yield Segment(_box.get_top(widths), border_style) yield new_line for first, last, row in loop_first_last(rows): @@ -448,24 +448,24 @@ class Table: for width, _cell in zip(widths, cells) ] - if box: + if _box: if last and show_footer: yield Segment( - box.get_row(widths, "foot", edge=show_edge), border_style + _box.get_row(widths, "foot", edge=show_edge), border_style ) yield new_line if first: - left = Segment(box.head_left, border_style) - right = Segment(box.head_right, border_style) - divider = Segment(box.head_vertical, border_style) + left = Segment(_box.head_left, border_style) + right = Segment(_box.head_right, border_style) + divider = Segment(_box.head_vertical, border_style) elif last: - left = Segment(box.foot_left, border_style) - right = Segment(box.foot_right, border_style) - divider = Segment(box.foot_vertical, border_style) + left = Segment(_box.foot_left, border_style) + right = Segment(_box.foot_right, border_style) + divider = Segment(_box.foot_vertical, border_style) else: - left = Segment(box.mid_left, border_style) - right = Segment(box.mid_right, border_style) - divider = Segment(box.mid_vertical, border_style) + left = Segment(_box.mid_left, border_style) + right = Segment(_box.mid_right, border_style) + divider = Segment(_box.mid_vertical, border_style) for line_no in range(max_height): if show_edge: @@ -482,12 +482,14 @@ class Table: for rendered_cell in cells: yield from rendered_cell[line_no] yield new_line - if box and first and show_header: - yield Segment(box.get_row(widths, "head", edge=show_edge), border_style) + if _box and first and show_header: + yield Segment( + _box.get_row(widths, "head", edge=show_edge), border_style + ) yield new_line - if box and show_edge: - yield Segment(box.get_bottom(widths), border_style) + if _box and show_edge: + yield Segment(_box.get_bottom(widths), border_style) yield new_line