Skip to content

Commit bc68d1e

Browse files
committed
feat: complete and consistent support for the ui.Style() API (#1637)
1 parent 61c0db8 commit bc68d1e

File tree

9 files changed

+101
-182
lines changed

9 files changed

+101
-182
lines changed

Diff for: yazi-plugin/src/elements/bar.rs

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value};
1+
use mlua::{AnyUserData, Lua, Table, UserData};
22
use ratatui::widgets::Borders;
33

4-
use super::{RectRef, Renderable, Style};
4+
use super::{RectRef, Renderable};
55

66
#[derive(Clone)]
77
pub struct Bar {
88
area: ratatui::layout::Rect,
99

1010
direction: ratatui::widgets::Borders,
1111
symbol: String,
12-
style: Option<ratatui::style::Style>,
12+
style: ratatui::style::Style,
1313
}
1414

1515
impl Bar {
@@ -42,22 +42,12 @@ impl Bar {
4242

4343
impl UserData for Bar {
4444
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
45+
crate::impl_style_method!(methods, style);
46+
4547
methods.add_function("symbol", |_, (ud, symbol): (AnyUserData, String)| {
4648
ud.borrow_mut::<Self>()?.symbol = symbol;
4749
Ok(ud)
4850
});
49-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
50-
{
51-
let mut me = ud.borrow_mut::<Self>()?;
52-
match value {
53-
Value::Nil => me.style = None,
54-
Value::Table(tb) => me.style = Some(Style::try_from(tb)?.0),
55-
Value::UserData(ud) => me.style = Some(ud.borrow::<Style>()?.0),
56-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
57-
}
58-
}
59-
Ok(ud)
60-
});
6151
}
6252
}
6353

@@ -81,36 +71,24 @@ impl Renderable for Bar {
8171

8272
if self.direction.contains(Borders::LEFT) {
8373
for y in self.area.top()..self.area.bottom() {
84-
let cell = buf[(self.area.left(), y)].set_symbol(symbol);
85-
if let Some(style) = self.style {
86-
cell.set_style(style);
87-
}
74+
buf[(self.area.left(), y)].set_style(self.style).set_symbol(symbol);
8875
}
8976
}
9077
if self.direction.contains(Borders::TOP) {
9178
for x in self.area.left()..self.area.right() {
92-
let cell = buf[(x, self.area.top())].set_symbol(symbol);
93-
if let Some(style) = self.style {
94-
cell.set_style(style);
95-
}
79+
buf[(x, self.area.top())].set_style(self.style).set_symbol(symbol);
9680
}
9781
}
9882
if self.direction.contains(Borders::RIGHT) {
9983
let x = self.area.right() - 1;
10084
for y in self.area.top()..self.area.bottom() {
101-
let cell = buf[(x, y)].set_symbol(symbol);
102-
if let Some(style) = self.style {
103-
cell.set_style(style);
104-
}
85+
buf[(x, y)].set_style(self.style).set_symbol(symbol);
10586
}
10687
}
10788
if self.direction.contains(Borders::BOTTOM) {
10889
let y = self.area.bottom() - 1;
10990
for x in self.area.left()..self.area.right() {
110-
let cell = buf[(x, y)].set_symbol(symbol);
111-
if let Some(style) = self.style {
112-
cell.set_style(style);
113-
}
91+
buf[(x, y)].set_style(self.style).set_symbol(symbol);
11492
}
11593
}
11694
}

Diff for: yazi-plugin/src/elements/border.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value};
1+
use mlua::{AnyUserData, Lua, Table, UserData};
22
use ratatui::widgets::{Borders, Widget};
33

4-
use super::{RectRef, Renderable, Style};
4+
use super::{RectRef, Renderable};
55

66
// Type
77
const PLAIN: u8 = 0;
@@ -17,7 +17,7 @@ pub struct Border {
1717

1818
position: ratatui::widgets::Borders,
1919
type_: ratatui::widgets::BorderType,
20-
style: Option<ratatui::style::Style>,
20+
style: ratatui::style::Style,
2121
}
2222

2323
impl Border {
@@ -55,6 +55,8 @@ impl Border {
5555

5656
impl UserData for Border {
5757
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
58+
crate::impl_style_method!(methods, style);
59+
5860
methods.add_function("type", |_, (ud, value): (AnyUserData, u8)| {
5961
ud.borrow_mut::<Self>()?.type_ = match value {
6062
ROUNDED => ratatui::widgets::BorderType::Rounded,
@@ -66,33 +68,18 @@ impl UserData for Border {
6668
};
6769
Ok(ud)
6870
});
69-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
70-
{
71-
let mut me = ud.borrow_mut::<Self>()?;
72-
match value {
73-
Value::Nil => me.style = None,
74-
Value::Table(tb) => me.style = Some(Style::try_from(tb)?.0),
75-
Value::UserData(ud) => me.style = Some(ud.borrow::<Style>()?.0),
76-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
77-
}
78-
}
79-
Ok(ud)
80-
});
8171
}
8272
}
8373

8474
impl Renderable for Border {
8575
fn area(&self) -> ratatui::layout::Rect { self.area }
8676

8777
fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
88-
let mut block =
89-
ratatui::widgets::Block::default().borders(self.position).border_type(self.type_);
90-
91-
if let Some(style) = self.style {
92-
block = block.border_style(style);
93-
}
94-
95-
block.render(self.area, buf);
78+
ratatui::widgets::Block::default()
79+
.borders(self.position)
80+
.border_type(self.type_)
81+
.border_style(self.style)
82+
.render(self.area, buf);
9683
}
9784

9885
fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(self.clone()).render(buf); }

Diff for: yazi-plugin/src/elements/gauge.rs

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataMethods, Value};
22
use ratatui::widgets::Widget;
33

4-
use super::{RectRef, Renderable, Span, Style};
4+
use super::{RectRef, Renderable, Span};
5+
use crate::elements::Style;
56

67
#[derive(Clone, Default)]
78
pub struct Gauge {
89
area: ratatui::layout::Rect,
910

1011
ratio: f64,
1112
label: Option<ratatui::text::Span<'static>>,
12-
style: Option<ratatui::style::Style>,
13-
gauge_style: Option<ratatui::style::Style>,
13+
style: ratatui::style::Style,
14+
gauge_style: ratatui::style::Style,
1415
}
1516

1617
impl Gauge {
@@ -24,6 +25,8 @@ impl Gauge {
2425

2526
impl UserData for Gauge {
2627
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
28+
crate::impl_style_method!(methods, style);
29+
2730
methods.add_function("percent", |_, (ud, percent): (AnyUserData, u8)| {
2831
if percent > 100 {
2932
return Err("percent must be between 0 and 100".into_lua_err());
@@ -47,23 +50,8 @@ impl UserData for Gauge {
4750
Ok(ud)
4851
});
4952

50-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
51-
ud.borrow_mut::<Self>()?.style = match value {
52-
Value::Nil => None,
53-
Value::Table(tb) => Some(Style::try_from(tb)?.0),
54-
Value::UserData(ud) => Some(ud.borrow::<Style>()?.0),
55-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
56-
};
57-
Ok(ud)
58-
});
59-
6053
methods.add_function("gauge_style", |_, (ud, value): (AnyUserData, Value)| {
61-
ud.borrow_mut::<Self>()?.gauge_style = match value {
62-
Value::Nil => None,
63-
Value::Table(tb) => Some(Style::try_from(tb)?.0),
64-
Value::UserData(ud) => Some(ud.borrow::<Style>()?.0),
65-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
66-
};
54+
ud.borrow_mut::<Self>()?.gauge_style = Style::try_from(value)?.0;
6755
Ok(ud)
6856
});
6957
}
@@ -73,20 +61,16 @@ impl Renderable for Gauge {
7361
fn area(&self) -> ratatui::layout::Rect { self.area }
7462

7563
fn render(self: Box<Self>, buf: &mut ratatui::buffer::Buffer) {
76-
let mut gauge = ratatui::widgets::Gauge::default();
64+
let mut gauge = ratatui::widgets::Gauge::default()
65+
.ratio(self.ratio)
66+
.style(self.style)
67+
.gauge_style(self.gauge_style);
7768

78-
gauge = gauge.ratio(self.ratio);
79-
if let Some(label) = self.label {
80-
gauge = gauge.label(label);
81-
}
82-
if let Some(style) = self.style {
83-
gauge = gauge.style(style);
84-
}
85-
if let Some(gauge_style) = self.gauge_style {
86-
gauge = gauge.gauge_style(gauge_style);
69+
if let Some(s) = self.label {
70+
gauge = gauge.label(s)
8771
}
8872

89-
gauge.render(self.area, buf)
73+
gauge.render(self.area, buf);
9074
}
9175

9276
fn clone_render(&self, buf: &mut ratatui::buffer::Buffer) { Box::new(self.clone()).render(buf) }

Diff for: yazi-plugin/src/elements/line.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ansi_to_tui::IntoText;
44
use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, Table, UserData, UserDataMethods, Value};
55
use unicode_width::UnicodeWidthChar;
66

7-
use super::{Span, Style};
7+
use super::Span;
88

99
const LEFT: u8 = 0;
1010
const CENTER: u8 = 1;
@@ -83,21 +83,10 @@ impl Line {
8383

8484
impl UserData for Line {
8585
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
86+
crate::impl_style_method!(methods, 0.style);
8687
crate::impl_style_shorthands!(methods, 0.style);
8788

8889
methods.add_method("width", |_, me, ()| Ok(me.0.width()));
89-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
90-
{
91-
let mut me = ud.borrow_mut::<Self>()?;
92-
me.0.style = match value {
93-
Value::Nil => ratatui::style::Style::default(),
94-
Value::Table(tb) => Style::try_from(tb)?.0,
95-
Value::UserData(ud) => ud.borrow::<Style>()?.0,
96-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
97-
};
98-
}
99-
Ok(ud)
100-
});
10190
methods.add_function("align", |_, (ud, align): (AnyUserData, u8)| {
10291
ud.borrow_mut::<Self>()?.0.alignment = Some(match align {
10392
CENTER => ratatui::layout::Alignment::Center,

Diff for: yazi-plugin/src/elements/list.rs

+20-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, Value};
1+
use mlua::{ExternalError, FromLua, Lua, Table, UserData, Value};
22
use ratatui::widgets::Widget;
33

4-
use super::{Line, RectRef, Renderable, Span, Style};
4+
use super::{Line, RectRef, Renderable, Span};
55

66
// --- List
77
#[derive(Clone)]
@@ -35,59 +35,42 @@ impl Renderable for List {
3535
}
3636

3737
// --- ListItem
38-
#[derive(Clone, FromLua)]
38+
#[derive(Clone, Default, FromLua)]
3939
pub struct ListItem {
4040
content: ratatui::text::Text<'static>,
41-
style: Option<ratatui::style::Style>,
41+
style: ratatui::style::Style,
4242
}
4343

4444
impl ListItem {
4545
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
4646
ui.raw_set(
4747
"ListItem",
48-
lua.create_function(|_, value: Value| {
49-
match value {
50-
Value::UserData(ud) => {
51-
let content: ratatui::text::Text = if let Ok(line) = ud.take::<Line>() {
52-
line.0.into()
53-
} else if let Ok(span) = ud.take::<Span>() {
54-
span.0.into()
55-
} else {
56-
return Err("expected a String, Line or Span".into_lua_err());
57-
};
58-
return Ok(Self { content, style: None });
59-
}
60-
Value::String(s) => {
61-
return Ok(Self { content: s.to_string_lossy().into_owned().into(), style: None });
62-
}
63-
_ => {}
48+
lua.create_function(|_, value: Value| match value {
49+
Value::String(s) => {
50+
Ok(Self { content: s.to_string_lossy().into_owned().into(), ..Default::default() })
6451
}
65-
Err("expected a String, Line or Span".into_lua_err())
52+
Value::UserData(ud) => {
53+
let content: ratatui::text::Text = if let Ok(line) = ud.take::<Line>() {
54+
line.0.into()
55+
} else if let Ok(span) = ud.take::<Span>() {
56+
span.0.into()
57+
} else {
58+
return Err("expected a String, Line or Span".into_lua_err());
59+
};
60+
Ok(Self { content, ..Default::default() })
61+
}
62+
_ => Err("expected a String, Line or Span".into_lua_err()),
6663
})?,
6764
)
6865
}
6966
}
7067

7168
impl From<ListItem> for ratatui::widgets::ListItem<'static> {
72-
fn from(value: ListItem) -> Self {
73-
let mut item = Self::new(value.content);
74-
if let Some(style) = value.style {
75-
item = item.style(style)
76-
}
77-
item
78-
}
69+
fn from(value: ListItem) -> Self { Self::new(value.content).style(value.style) }
7970
}
8071

8172
impl UserData for ListItem {
8273
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
83-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
84-
ud.borrow_mut::<Self>()?.style = match value {
85-
Value::Nil => None,
86-
Value::Table(tb) => Some(Style::try_from(tb)?.0),
87-
Value::UserData(ud) => Some(ud.borrow::<Style>()?.0),
88-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
89-
};
90-
Ok(ud)
91-
});
74+
crate::impl_style_method!(methods, style);
9275
}
9376
}

Diff for: yazi-plugin/src/elements/paragraph.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use ansi_to_tui::IntoText;
2-
use mlua::{AnyUserData, ExternalError, ExternalResult, IntoLua, Lua, Table, UserData, Value};
2+
use mlua::{AnyUserData, ExternalError, ExternalResult, IntoLua, Lua, Table, UserData};
33
use ratatui::widgets::Widget;
44

5-
use super::{Line, RectRef, Renderable, Style};
5+
use super::{Line, RectRef, Renderable};
66

77
// Alignment
88
const LEFT: u8 = 0;
@@ -58,20 +58,9 @@ impl Paragraph {
5858

5959
impl UserData for Paragraph {
6060
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
61+
crate::impl_style_method!(methods, style);
6162
crate::impl_style_shorthands!(methods, style);
6263

63-
methods.add_function("style", |_, (ud, value): (AnyUserData, Value)| {
64-
{
65-
let mut me = ud.borrow_mut::<Self>()?;
66-
match value {
67-
Value::Nil => me.style = ratatui::style::Style::default(),
68-
Value::Table(tb) => me.style = Style::try_from(tb)?.0,
69-
Value::UserData(ud) => me.style = ud.borrow::<Style>()?.0,
70-
_ => return Err("expected a Style or Table or nil".into_lua_err()),
71-
}
72-
}
73-
Ok(ud)
74-
});
7564
methods.add_function("align", |_, (ud, align): (AnyUserData, u8)| {
7665
ud.borrow_mut::<Self>()?.alignment = match align {
7766
CENTER => ratatui::layout::Alignment::Center,

0 commit comments

Comments
 (0)