pub struct GradientText { /* private fields */ }Expand description
A Text whose characters are coloured along a gradient of two or
more colours, spaced evenly.
- The text is laid out as it would be on its own (wrapped, justified,
its styles kept), then each character takes the colour at its place:
its column across the widest line (
Horizontal), its line (Vertical), or both (Diagonal). - The gradient replaces the text’s own colour (or its background, with
background); bold, italic and the rest stay. - It degrades by the console’s colour system: each colour is the nearest the console has, so a 256-colour terminal shows steps and a 16-colour one a few bands, and neighbours that come out the same are written as one run. Without colour the text is drawn as it is.
use rich::{Color, ColorSystem, Console, Text};
use rich_ext::gradient::{GradientDirection, GradientText};
let stops = ["#ff0000", "#ffff00", "#0000ff"].map(|c| Color::parse(c).unwrap());
let banner = GradientText::new(Text::new("one\ntwo\nsix"), stops)
.direction(GradientDirection::Vertical);
let console = Console::builder()
.width(20)
.force_terminal(true)
.color_system(Some(ColorSystem::Standard))
.no_color(false)
.build();
// Red, yellow and blue, a line each, on 16 colours.
assert_eq!(
console.render_export(&banner),
"\x1b[31mone\x1b[0m\n\x1b[93mtwo\x1b[0m\n\x1b[34msix\x1b[0m\n"
);
let plain = Console::builder().width(20).color_system(None).build();
assert_eq!(plain.render_export(&banner), "one\ntwo\nsix\n");Implementations§
Source§impl GradientText
impl GradientText
Sourcepub fn new(text: Text, stops: impl IntoIterator<Item = Color>) -> Self
pub fn new(text: Text, stops: impl IntoIterator<Item = Color>) -> Self
text coloured along stops (the first at the start, the last at
the end). One stop colours it all; none leaves it as it is.
Sourcepub fn direction(self, direction: GradientDirection) -> Self
pub fn direction(self, direction: GradientDirection) -> Self
Which way the colours run (default GradientDirection::Horizontal).
Sourcepub fn background(self, background: bool) -> Self
pub fn background(self, background: bool) -> Self
Colour the background rather than the characters (default off).
Trait Implementations§
Source§impl AccessibleText for GradientText
The text’s own words: the gradient is decoration.
impl AccessibleText for GradientText
The text’s own words: the gradient is decoration.
Source§fn accessible_text(&self, _width: usize) -> String
fn accessible_text(&self, _width: usize) -> String
The content as plain lines, laid out for
width where layout matters.Source§impl Clone for GradientText
impl Clone for GradientText
Source§impl Debug for GradientText
impl Debug for GradientText
Source§impl Renderable for GradientText
impl Renderable for GradientText
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Source§fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
The
(minimum, maximum) cell width this renderable wants. Port of
__rich_measure__. The default is what upstream’s Measurement.get
returns for a renderable without one: anything from nothing to the
whole width, (0, max_width).Source§fn fit_to_measurement(&self) -> bool
fn fit_to_measurement(&self) -> bool
Whether a top-level
Console::print shrinks this renderable to its
measured width. Upstream renders every top-level renderable at the full
console width, so the default is false; an extension may opt in.Source§fn vertical(&self) -> Option<VerticalAlign>
fn vertical(&self) -> Option<VerticalAlign>
The vertical alignment a
Table cell holding this renderable uses in
place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.Source§fn print_end_line(&self, _options: &ConsoleOptions) -> bool
fn print_end_line(&self, _options: &ConsoleOptions) -> bool
Whether a top-level print ends this renderable’s last line. This
port’s renderables separate lines, where upstream’s end each line
with
"\n", so a print adds the final line break. A renderable
whose upstream output stops without one (Screen, LiveRender, a
Rule whose end has no newline) returns false, and the next
output continues its last line. options are the print’s options.Auto Trait Implementations§
impl Freeze for GradientText
impl RefUnwindSafe for GradientText
impl Send for GradientText
impl Sync for GradientText
impl Unpin for GradientText
impl UnsafeUnpin for GradientText
impl UnwindSafe for GradientText
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more