Skip to content

Commit ed1fba2

Browse files
committed
edge case and fix
1 parent 6bc4c31 commit ed1fba2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/textual/renderables/bar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __rich_console__(
8686
if half_end:
8787
highlight_bar.append(Text(half_bar_right, style=highlight_style, end=""))
8888

89-
if self.gradient:
89+
if self.gradient is not None:
9090
_apply_gradient(highlight_bar, self.gradient, width)
9191
output_bar.append(highlight_bar)
9292

@@ -112,13 +112,14 @@ def _apply_gradient(text: Text, gradient: Gradient, width: int) -> None:
112112
Args:
113113
text: A Text object.
114114
gradient: A Textual gradient.
115-
width: Width to apply gradient to, or None for full width.
115+
width: Width of gradient.
116116
"""
117117
from_color = Style.from_color
118118
get_rich_color = gradient.get_rich_color
119119

120120
max_width = width - 1
121121
if not max_width:
122+
text.stylize(from_color(gradient.get_color(0).rich_color))
122123
return
123124
text_length = len(text)
124125
for offset in range(text_length):

tests/test_progress_bar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import pytest
22
from pytest import approx
3+
from rich.console import Console
4+
from rich.text import Text
35

46
from textual.app import App
7+
from textual.color import Gradient
58
from textual.css.query import NoMatches
9+
from textual.renderables.bar import _apply_gradient
610
from textual.widget import Widget
711
from textual.widgets import ProgressBar
812

@@ -169,3 +173,11 @@ def compose(self):
169173
else:
170174
with pytest.raises(NoMatches):
171175
app.pb.query_one("#eta")
176+
177+
178+
def test_apply_gradient():
179+
text = Text("foo")
180+
gradient = Gradient.from_colors("red", "blue")
181+
_apply_gradient(text, gradient, 1)
182+
console = Console()
183+
assert text.get_style_at_offset(console, 0).color.triplet == (255, 0, 0)

0 commit comments

Comments
 (0)