Skip to content

Commit bd00a04

Browse files
committed
windows fix
1 parent 556ac61 commit bd00a04

File tree

8 files changed

+78
-33
lines changed

8 files changed

+78
-33
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.4] - 2020-05-16
9+
10+
### Fixed
11+
12+
- Fixed incorrect file and link in progress.log
13+
- Fixes for legacy windows: Bar, Panel, and Rule now use ASCII characters
14+
- show_cursor is now a no-op on legacy windows
15+
16+
### Added
17+
18+
- Added Console.input
19+
20+
### Changed
21+
22+
- Disable progress bars when not writing to a terminal
23+
824
## [1.1.3] - 2020-05-15
925

1026
### Fixed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "1.1.3"
5+
version = "1.1.4"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <[email protected]>"]
88
license = "MIT"

rich/bar.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ def update(self, completed: float, total: float = None) -> None:
5757
def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
5858
completed = min(self.total, max(0, self.completed))
5959
width = min(self.width or options.max_width, options.max_width)
60-
bar = "━"
61-
half_bar_right = "╸"
62-
half_bar_left = "╺"
60+
61+
legacy_windows = console.legacy_windows
62+
bar = "▓" if legacy_windows else "━"
63+
half_bar_right = "░" if legacy_windows else "╸"
64+
half_bar_left = " " if legacy_windows else "╺"
6365
complete_halves = int(width * 2 * completed / self.total)
6466
bar_count = complete_halves // 2
6567
half_bar_count = complete_halves % 2

rich/console.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,9 @@ def show_cursor(self, show: bool = True) -> None:
429429
Args:
430430
show (bool, optional): Set visibility of the cursor.
431431
"""
432-
self._check_buffer()
433-
self.file.write("\033[?25h" if show else "\033[?25l")
432+
if self.is_terminal and not self.legacy_windows:
433+
self._check_buffer()
434+
self.file.write("\033[?25h" if show else "\033[?25l")
434435

435436
def _render(
436437
self, renderable: RenderableType, options: Optional[ConsoleOptions],
@@ -815,16 +816,35 @@ def _render_buffer(self) -> str:
815816
with self._record_buffer_lock:
816817
self._record_buffer.extend(buffer)
817818
del self._buffer[:]
819+
not_terminal = not self.is_terminal
818820
for line in Segment.split_and_crop_lines(buffer, self.width, pad=False):
819821
for text, style, is_control in line:
820822
if style and not is_control:
821823
append(style.render(text, color_system=color_system))
822824
else:
823-
append(text)
825+
if not (not_terminal and is_control):
826+
append(text)
824827

825828
rendered = "".join(output)
826829
return rendered
827830

831+
def input(
832+
self, prompt: Union[str, Text] = "", *, markup: bool = True, emoji: bool = True
833+
) -> str:
834+
"""Displays a prompt and waits for input from the user. The prompt may contain color / style.
835+
836+
Args:
837+
prompt (Union[Str, Text]): Text to render in the prompt.
838+
markup (bool, optional): Enable console markup (requires a str prompt). Defaults to True.
839+
emoji (bool, optional): Enable emoji (requires a str prompt). Defaults to True.
840+
841+
Returns:
842+
str: Text read from stdin.
843+
"""
844+
self.print(prompt, markup=markup, emoji=emoji, end="")
845+
result = input()
846+
return result
847+
828848
def export_text(self, clear: bool = True, styles: bool = False) -> str:
829849
"""Generate text from console contents (requires record=True argument in constructor).
830850

rich/panel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, Tuple, Union
22

3-
from . import box
3+
from .box import Box, SQUARE, ROUNDED
4+
45
from .console import (
56
Console,
67
ConsoleOptions,
@@ -33,7 +34,7 @@ class Panel:
3334
def __init__(
3435
self,
3536
renderable: RenderableType,
36-
box: box.Box = box.ROUNDED,
37+
box: Box = None,
3738
expand: bool = True,
3839
style: Union[str, Style] = "none",
3940
width: Optional[int] = None,
@@ -59,8 +60,7 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
5960
width = child_width + 2
6061
child_options = options.update(width=child_width)
6162
lines = console.render_lines(self.renderable, child_options)
62-
63-
box = self.box
63+
box = SQUARE if console.legacy_windows else (self.box or ROUNDED)
6464
line_start = Segment(box.mid_left, style)
6565
line_end = Segment(f"{box.mid_right}\n", style)
6666
yield Segment(box.get_top([width - 2]), style)

rich/progress.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class Progress:
324324
"""Renders an auto-updating progress bar(s).
325325
326326
Args:
327-
console (Console, optional): Optional Console instance. Default will create own internal Console instance.
327+
console (Console, optional): Optional Console instance. Default will an internal Console instance writing to stderr.
328328
auto_refresh (bool, optional): Enable auto refresh. If disabled, you will need to call `refresh()`.
329329
refresh_per_second (int, optional): Number of times per second to refresh the progress information. Defaults to 10.
330330
speed_estimate_period: (float, optional): Period (in seconds) used to calculate the speed estimate. Defaults to 30.
@@ -544,9 +544,10 @@ def refresh(self) -> None:
544544
"""Refresh (render) the progress information."""
545545
with self._lock:
546546
self._live_render.set_renderable(self.get_renderable())
547-
with self.console:
548-
self.console.print(self._live_render.position_cursor())
549-
self.console.print(self._live_render)
547+
if self.console.is_terminal:
548+
with self.console:
549+
self.console.print(self._live_render.position_cursor())
550+
self.console.print(self._live_render)
550551
self._refresh_count += 1
551552

552553
def get_renderable(self) -> RenderableType:
@@ -652,9 +653,11 @@ def print(
652653
highlight: bool = None,
653654
) -> None:
654655
"""Print to the terminal and preserve progress display. Parameters identical to :class:`~rich.console.Console.print`."""
655-
with self.console:
656-
self.console.print(self._live_render.position_cursor())
657-
self.console.print(
656+
console = self.console
657+
with console:
658+
if console.is_terminal:
659+
console.print(self._live_render.position_cursor())
660+
console.print(
658661
*objects,
659662
sep=sep,
660663
end=end,
@@ -663,7 +666,8 @@ def print(
663666
markup=markup,
664667
highlight=highlight,
665668
)
666-
self.console.print(self._live_render)
669+
if console.is_terminal:
670+
console.print(self._live_render)
667671

668672
def log(
669673
self,
@@ -677,19 +681,22 @@ def log(
677681
_stack_offset=1,
678682
) -> None:
679683
"""Log to the terminal and preserve progress display. Parameters identical to :class:`~rich.console.Console.log`."""
680-
with self.console:
681-
self.console.print(self._live_render.position_cursor())
682-
self.console.log(
684+
console = self.console
685+
with console:
686+
if console.is_terminal:
687+
console.print(self._live_render.position_cursor())
688+
console.log(
683689
*objects,
684690
sep=sep,
685691
end=end,
686692
emoji=emoji,
687693
markup=markup,
688694
highlight=highlight,
689695
log_locals=log_locals,
690-
_stack_offset=_stack_offset,
696+
_stack_offset=_stack_offset + 1,
691697
)
692-
self.console.print(self._live_render)
698+
if console.is_terminal:
699+
console.print(self._live_render)
693700

694701

695702
if __name__ == "__main__": # pragma: no coverage

rich/rule.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class Rule:
1616
def __init__(
1717
self,
1818
title: Union[str, Text] = "",
19-
character: str = "━",
19+
character: str = None,
2020
style: Union[str, Style] = "rule.line",
2121
) -> None:
22-
if len(character) != 1:
22+
if character and len(character) != 1:
2323
raise ValueError(
2424
"Rule requires character argument to be a string of length 1"
2525
)
@@ -33,8 +33,10 @@ def __repr__(self) -> str:
3333
def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
3434
width = options.max_width
3535

36+
character = "-" if console.legacy_windows else (self.character or "━")
37+
3638
if not self.title:
37-
yield Text(self.character * width, self.style)
39+
yield Text(character * width, self.style)
3840
else:
3941
if isinstance(self.title, Text):
4042
title_text = self.title
@@ -45,11 +47,9 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
4547

4648
rule_text = Text()
4749
center = (width - len(title_text)) // 2
48-
rule_text.append(self.character * (center - 1) + " ", self.style)
50+
rule_text.append(character * (center - 1) + " ", self.style)
4951
rule_text.append(title_text)
50-
rule_text.append(
51-
" " + self.character * (width - len(rule_text) - 1), self.style
52-
)
52+
rule_text.append(" " + character * (width - len(rule_text) - 1), self.style)
5353
yield rule_text
5454

5555

tests/test_console.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_print_style():
8282

8383

8484
def test_show_cursor():
85-
console = Console(file=io.StringIO())
85+
console = Console(file=io.StringIO(), force_terminal=True)
8686
console.show_cursor(False)
8787
console.print("foo")
8888
console.show_cursor(True)
@@ -109,7 +109,7 @@ def test_render_error():
109109

110110

111111
def test_control():
112-
console = Console(file=io.StringIO())
112+
console = Console(file=io.StringIO(), force_terminal=True)
113113
console.control("FOO")
114114
console.print("BAR")
115115
assert console.file.getvalue() == "FOOBAR\n"

0 commit comments

Comments
 (0)