feat: add lifetime to symbol sets (#1935)

This makes it possible to create symbol sets at runtime with non-static
lifetimes.

Fixes: https://github.com/ratatui/ratatui/issues/1722
This commit is contained in:
Josh McKinney
2025-06-25 01:02:09 -07:00
committed by GitHub
parent 21e3b598ce
commit b6fbfcdd1c
11 changed files with 63 additions and 63 deletions

View File

@@ -8,19 +8,19 @@ pub const ONE_QUARTER: &str = "▂";
pub const ONE_EIGHTH: &str = "";
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Set {
pub full: &'static str,
pub seven_eighths: &'static str,
pub three_quarters: &'static str,
pub five_eighths: &'static str,
pub half: &'static str,
pub three_eighths: &'static str,
pub one_quarter: &'static str,
pub one_eighth: &'static str,
pub empty: &'static str,
pub struct Set<'a> {
pub full: &'a str,
pub seven_eighths: &'a str,
pub three_quarters: &'a str,
pub five_eighths: &'a str,
pub half: &'a str,
pub three_eighths: &'a str,
pub one_quarter: &'a str,
pub one_eighth: &'a str,
pub empty: &'a str,
}
impl Default for Set {
impl Default for Set<'_> {
fn default() -> Self {
NINE_LEVELS
}

View File

@@ -8,19 +8,19 @@ pub const ONE_QUARTER: &str = "▎";
pub const ONE_EIGHTH: &str = "";
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Set {
pub full: &'static str,
pub seven_eighths: &'static str,
pub three_quarters: &'static str,
pub five_eighths: &'static str,
pub half: &'static str,
pub three_eighths: &'static str,
pub one_quarter: &'static str,
pub one_eighth: &'static str,
pub empty: &'static str,
pub struct Set<'a> {
pub full: &'a str,
pub seven_eighths: &'a str,
pub three_quarters: &'a str,
pub five_eighths: &'a str,
pub half: &'a str,
pub three_eighths: &'a str,
pub one_quarter: &'a str,
pub one_eighth: &'a str,
pub empty: &'a str,
}
impl Default for Set {
impl Default for Set<'_> {
fn default() -> Self {
NINE_LEVELS
}

View File

@@ -1,25 +1,25 @@
use crate::symbols::{block, line};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Set {
pub top_left: &'static str,
pub top_right: &'static str,
pub bottom_left: &'static str,
pub bottom_right: &'static str,
pub vertical_left: &'static str,
pub vertical_right: &'static str,
pub horizontal_top: &'static str,
pub horizontal_bottom: &'static str,
pub struct Set<'a> {
pub top_left: &'a str,
pub top_right: &'a str,
pub bottom_left: &'a str,
pub bottom_right: &'a str,
pub vertical_left: &'a str,
pub vertical_right: &'a str,
pub horizontal_top: &'a str,
pub horizontal_bottom: &'a str,
}
impl Default for Set {
impl Default for Set<'_> {
fn default() -> Self {
PLAIN
}
}
// Helper function to convert a line set to a border set
const fn from_line_set(line_set: line::Set) -> Set {
const fn from_line_set(line_set: line::Set<'_>) -> Set<'_> {
Set {
top_left: line_set.top_left,
top_right: line_set.top_right,

View File

@@ -59,21 +59,21 @@ pub const DOUBLE_CROSS: &str = "╬";
pub const THICK_CROSS: &str = "";
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Set {
pub vertical: &'static str,
pub horizontal: &'static str,
pub top_right: &'static str,
pub top_left: &'static str,
pub bottom_right: &'static str,
pub bottom_left: &'static str,
pub vertical_left: &'static str,
pub vertical_right: &'static str,
pub horizontal_down: &'static str,
pub horizontal_up: &'static str,
pub cross: &'static str,
pub struct Set<'a> {
pub vertical: &'a str,
pub horizontal: &'a str,
pub top_right: &'a str,
pub top_left: &'a str,
pub bottom_right: &'a str,
pub bottom_left: &'a str,
pub vertical_left: &'a str,
pub vertical_right: &'a str,
pub horizontal_down: &'a str,
pub horizontal_up: &'a str,
pub cross: &'a str,
}
impl Default for Set {
impl Default for Set<'_> {
fn default() -> Self {
NORMAL
}

View File

@@ -10,11 +10,11 @@ use crate::symbols::{block, line};
/// └─────────── begin
/// ```
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct Set {
pub track: &'static str,
pub thumb: &'static str,
pub begin: &'static str,
pub end: &'static str,
pub struct Set<'a> {
pub track: &'a str,
pub thumb: &'a str,
pub begin: &'a str,
pub end: &'a str,
}
pub const DOUBLE_VERTICAL: Set = Set {

View File

@@ -90,7 +90,7 @@ pub struct BarChart<'a> {
/// The gap between each group
group_gap: u16,
/// Set of symbols used to display the data
bar_set: symbols::bar::Set,
bar_set: symbols::bar::Set<'a>,
/// Style of the bars
bar_style: Style,
/// Style of the values printed at the bottom of each bar
@@ -323,7 +323,7 @@ impl<'a> BarChart<'a> {
///
/// If not set, the default is [`bar::NINE_LEVELS`](ratatui_core::symbols::bar::NINE_LEVELS).
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> Self {
pub const fn bar_set(mut self, bar_set: symbols::bar::Set<'a>) -> Self {
self.bar_set = bar_set;
self
}

View File

@@ -124,7 +124,7 @@ pub struct Block<'a> {
border_style: Style,
/// The symbols used to render the border. The default is plain lines but one can choose to
/// have rounded or doubled lines instead or a custom set of symbols
border_set: border::Set,
border_set: border::Set<'a>,
/// Widget style
style: Style,
/// Block padding
@@ -497,7 +497,7 @@ impl<'a> Block<'a> {
/// // ║ ║
/// // ╚═════╝
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn border_set(mut self, border_set: border::Set) -> Self {
pub const fn border_set(mut self, border_set: border::Set<'a>) -> Self {
self.border_set = border_set;
self
}

View File

@@ -149,7 +149,7 @@ pub enum BorderType {
impl BorderType {
/// Convert this `BorderType` into the corresponding [`Set`](border::Set) of border symbols.
pub const fn border_symbols(border_type: Self) -> border::Set {
pub const fn border_symbols<'a>(border_type: Self) -> border::Set<'a> {
match border_type {
Self::Plain => border::PLAIN,
Self::Rounded => border::ROUNDED,
@@ -167,7 +167,7 @@ impl BorderType {
}
/// Convert this `BorderType` into the corresponding [`Set`](border::Set) of border symbols.
pub const fn to_border_set(self) -> border::Set {
pub const fn to_border_set<'a>(self) -> border::Set<'a> {
Self::border_symbols(self)
}
}

View File

@@ -330,7 +330,7 @@ impl<'a> LineGauge<'a> {
since = "0.30.0",
note = "use `filled_symbol()` and `unfilled_symbol()` instead"
)]
pub const fn line_set(mut self, set: symbols::line::Set) -> Self {
pub const fn line_set(mut self, set: symbols::line::Set<'a>) -> Self {
self.filled_symbol = set.horizontal;
self.unfilled_symbol = set.horizontal;
self

View File

@@ -195,7 +195,7 @@ impl<'a> Scrollbar<'a> {
/// Creates a new scrollbar with the given orientation and symbol set.
#[must_use = "creates the Scrollbar"]
const fn new_with_symbols(orientation: ScrollbarOrientation, symbols: &Set) -> Self {
const fn new_with_symbols(orientation: ScrollbarOrientation, symbols: &Set<'a>) -> Self {
Self {
orientation,
thumb_symbol: symbols.thumb,
@@ -238,7 +238,7 @@ impl<'a> Scrollbar<'a> {
pub const fn orientation_and_symbol(
mut self,
orientation: ScrollbarOrientation,
symbols: Set,
symbols: Set<'a>,
) -> Self {
self.orientation = orientation;
self.symbols(symbols)
@@ -372,7 +372,7 @@ impl<'a> Scrollbar<'a> {
/// This is a fluent setter method which must be chained or used as it consumes self
#[expect(clippy::needless_pass_by_value)] // Breaking change
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn symbols(mut self, symbols: Set) -> Self {
pub const fn symbols(mut self, symbols: Set<'a>) -> Self {
self.thumb_symbol = symbols.thumb;
if self.track_symbol.is_some() {
self.track_symbol = Some(symbols.track);

View File

@@ -78,7 +78,7 @@ pub struct Sparkline<'a> {
/// widget uses the max of the dataset)
max: Option<u64>,
/// A set of bar symbols used to represent the give data
bar_set: symbols::bar::Set,
bar_set: symbols::bar::Set<'a>,
/// The direction to render the sparkline, either from left to right, or from right to left
direction: RenderDirection,
}
@@ -230,7 +230,7 @@ impl<'a> Sparkline<'a> {
/// Can be [`symbols::bar::THREE_LEVELS`], [`symbols::bar::NINE_LEVELS`] (default) or a custom
/// [`Set`](symbols::bar::Set).
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> Self {
pub const fn bar_set(mut self, bar_set: symbols::bar::Set<'a>) -> Self {
self.bar_set = bar_set;
self
}