Skip to content

Commit b6fbfcd

Browse files
authored
feat: add lifetime to symbol sets (#1935)
This makes it possible to create symbol sets at runtime with non-static lifetimes. Fixes: #1722
1 parent 21e3b59 commit b6fbfcd

11 files changed

Lines changed: 63 additions & 63 deletions

File tree

ratatui-core/src/symbols/bar.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ pub const ONE_QUARTER: &str = "▂";
88
pub const ONE_EIGHTH: &str = "▁";
99

1010
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
11-
pub struct Set {
12-
pub full: &'static str,
13-
pub seven_eighths: &'static str,
14-
pub three_quarters: &'static str,
15-
pub five_eighths: &'static str,
16-
pub half: &'static str,
17-
pub three_eighths: &'static str,
18-
pub one_quarter: &'static str,
19-
pub one_eighth: &'static str,
20-
pub empty: &'static str,
11+
pub struct Set<'a> {
12+
pub full: &'a str,
13+
pub seven_eighths: &'a str,
14+
pub three_quarters: &'a str,
15+
pub five_eighths: &'a str,
16+
pub half: &'a str,
17+
pub three_eighths: &'a str,
18+
pub one_quarter: &'a str,
19+
pub one_eighth: &'a str,
20+
pub empty: &'a str,
2121
}
2222

23-
impl Default for Set {
23+
impl Default for Set<'_> {
2424
fn default() -> Self {
2525
NINE_LEVELS
2626
}

ratatui-core/src/symbols/block.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ pub const ONE_QUARTER: &str = "▎";
88
pub const ONE_EIGHTH: &str = "▏";
99

1010
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
11-
pub struct Set {
12-
pub full: &'static str,
13-
pub seven_eighths: &'static str,
14-
pub three_quarters: &'static str,
15-
pub five_eighths: &'static str,
16-
pub half: &'static str,
17-
pub three_eighths: &'static str,
18-
pub one_quarter: &'static str,
19-
pub one_eighth: &'static str,
20-
pub empty: &'static str,
11+
pub struct Set<'a> {
12+
pub full: &'a str,
13+
pub seven_eighths: &'a str,
14+
pub three_quarters: &'a str,
15+
pub five_eighths: &'a str,
16+
pub half: &'a str,
17+
pub three_eighths: &'a str,
18+
pub one_quarter: &'a str,
19+
pub one_eighth: &'a str,
20+
pub empty: &'a str,
2121
}
2222

23-
impl Default for Set {
23+
impl Default for Set<'_> {
2424
fn default() -> Self {
2525
NINE_LEVELS
2626
}

ratatui-core/src/symbols/border.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
use crate::symbols::{block, line};
22

33
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
4-
pub struct Set {
5-
pub top_left: &'static str,
6-
pub top_right: &'static str,
7-
pub bottom_left: &'static str,
8-
pub bottom_right: &'static str,
9-
pub vertical_left: &'static str,
10-
pub vertical_right: &'static str,
11-
pub horizontal_top: &'static str,
12-
pub horizontal_bottom: &'static str,
4+
pub struct Set<'a> {
5+
pub top_left: &'a str,
6+
pub top_right: &'a str,
7+
pub bottom_left: &'a str,
8+
pub bottom_right: &'a str,
9+
pub vertical_left: &'a str,
10+
pub vertical_right: &'a str,
11+
pub horizontal_top: &'a str,
12+
pub horizontal_bottom: &'a str,
1313
}
1414

15-
impl Default for Set {
15+
impl Default for Set<'_> {
1616
fn default() -> Self {
1717
PLAIN
1818
}
1919
}
2020

2121
// Helper function to convert a line set to a border set
22-
const fn from_line_set(line_set: line::Set) -> Set {
22+
const fn from_line_set(line_set: line::Set<'_>) -> Set<'_> {
2323
Set {
2424
top_left: line_set.top_left,
2525
top_right: line_set.top_right,

ratatui-core/src/symbols/line.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ pub const DOUBLE_CROSS: &str = "╬";
5959
pub const THICK_CROSS: &str = "╋";
6060

6161
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
62-
pub struct Set {
63-
pub vertical: &'static str,
64-
pub horizontal: &'static str,
65-
pub top_right: &'static str,
66-
pub top_left: &'static str,
67-
pub bottom_right: &'static str,
68-
pub bottom_left: &'static str,
69-
pub vertical_left: &'static str,
70-
pub vertical_right: &'static str,
71-
pub horizontal_down: &'static str,
72-
pub horizontal_up: &'static str,
73-
pub cross: &'static str,
62+
pub struct Set<'a> {
63+
pub vertical: &'a str,
64+
pub horizontal: &'a str,
65+
pub top_right: &'a str,
66+
pub top_left: &'a str,
67+
pub bottom_right: &'a str,
68+
pub bottom_left: &'a str,
69+
pub vertical_left: &'a str,
70+
pub vertical_right: &'a str,
71+
pub horizontal_down: &'a str,
72+
pub horizontal_up: &'a str,
73+
pub cross: &'a str,
7474
}
7575

76-
impl Default for Set {
76+
impl Default for Set<'_> {
7777
fn default() -> Self {
7878
NORMAL
7979
}

ratatui-core/src/symbols/scrollbar.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use crate::symbols::{block, line};
1010
/// └─────────── begin
1111
/// ```
1212
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
13-
pub struct Set {
14-
pub track: &'static str,
15-
pub thumb: &'static str,
16-
pub begin: &'static str,
17-
pub end: &'static str,
13+
pub struct Set<'a> {
14+
pub track: &'a str,
15+
pub thumb: &'a str,
16+
pub begin: &'a str,
17+
pub end: &'a str,
1818
}
1919

2020
pub const DOUBLE_VERTICAL: Set = Set {

ratatui-widgets/src/barchart.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub struct BarChart<'a> {
9090
/// The gap between each group
9191
group_gap: u16,
9292
/// Set of symbols used to display the data
93-
bar_set: symbols::bar::Set,
93+
bar_set: symbols::bar::Set<'a>,
9494
/// Style of the bars
9595
bar_style: Style,
9696
/// Style of the values printed at the bottom of each bar
@@ -323,7 +323,7 @@ impl<'a> BarChart<'a> {
323323
///
324324
/// If not set, the default is [`bar::NINE_LEVELS`](ratatui_core::symbols::bar::NINE_LEVELS).
325325
#[must_use = "method moves the value of self and returns the modified value"]
326-
pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> Self {
326+
pub const fn bar_set(mut self, bar_set: symbols::bar::Set<'a>) -> Self {
327327
self.bar_set = bar_set;
328328
self
329329
}

ratatui-widgets/src/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub struct Block<'a> {
124124
border_style: Style,
125125
/// The symbols used to render the border. The default is plain lines but one can choose to
126126
/// have rounded or doubled lines instead or a custom set of symbols
127-
border_set: border::Set,
127+
border_set: border::Set<'a>,
128128
/// Widget style
129129
style: Style,
130130
/// Block padding
@@ -497,7 +497,7 @@ impl<'a> Block<'a> {
497497
/// // ║ ║
498498
/// // ╚═════╝
499499
#[must_use = "method moves the value of self and returns the modified value"]
500-
pub const fn border_set(mut self, border_set: border::Set) -> Self {
500+
pub const fn border_set(mut self, border_set: border::Set<'a>) -> Self {
501501
self.border_set = border_set;
502502
self
503503
}

ratatui-widgets/src/borders.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub enum BorderType {
149149

150150
impl BorderType {
151151
/// Convert this `BorderType` into the corresponding [`Set`](border::Set) of border symbols.
152-
pub const fn border_symbols(border_type: Self) -> border::Set {
152+
pub const fn border_symbols<'a>(border_type: Self) -> border::Set<'a> {
153153
match border_type {
154154
Self::Plain => border::PLAIN,
155155
Self::Rounded => border::ROUNDED,
@@ -167,7 +167,7 @@ impl BorderType {
167167
}
168168

169169
/// Convert this `BorderType` into the corresponding [`Set`](border::Set) of border symbols.
170-
pub const fn to_border_set(self) -> border::Set {
170+
pub const fn to_border_set<'a>(self) -> border::Set<'a> {
171171
Self::border_symbols(self)
172172
}
173173
}

ratatui-widgets/src/gauge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'a> LineGauge<'a> {
330330
since = "0.30.0",
331331
note = "use `filled_symbol()` and `unfilled_symbol()` instead"
332332
)]
333-
pub const fn line_set(mut self, set: symbols::line::Set) -> Self {
333+
pub const fn line_set(mut self, set: symbols::line::Set<'a>) -> Self {
334334
self.filled_symbol = set.horizontal;
335335
self.unfilled_symbol = set.horizontal;
336336
self

ratatui-widgets/src/scrollbar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a> Scrollbar<'a> {
195195

196196
/// Creates a new scrollbar with the given orientation and symbol set.
197197
#[must_use = "creates the Scrollbar"]
198-
const fn new_with_symbols(orientation: ScrollbarOrientation, symbols: &Set) -> Self {
198+
const fn new_with_symbols(orientation: ScrollbarOrientation, symbols: &Set<'a>) -> Self {
199199
Self {
200200
orientation,
201201
thumb_symbol: symbols.thumb,
@@ -238,7 +238,7 @@ impl<'a> Scrollbar<'a> {
238238
pub const fn orientation_and_symbol(
239239
mut self,
240240
orientation: ScrollbarOrientation,
241-
symbols: Set,
241+
symbols: Set<'a>,
242242
) -> Self {
243243
self.orientation = orientation;
244244
self.symbols(symbols)
@@ -372,7 +372,7 @@ impl<'a> Scrollbar<'a> {
372372
/// This is a fluent setter method which must be chained or used as it consumes self
373373
#[expect(clippy::needless_pass_by_value)] // Breaking change
374374
#[must_use = "method moves the value of self and returns the modified value"]
375-
pub const fn symbols(mut self, symbols: Set) -> Self {
375+
pub const fn symbols(mut self, symbols: Set<'a>) -> Self {
376376
self.thumb_symbol = symbols.thumb;
377377
if self.track_symbol.is_some() {
378378
self.track_symbol = Some(symbols.track);

0 commit comments

Comments
 (0)