diff --git a/Cargo.lock b/Cargo.lock index 66644537..6911e683 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2390,9 +2390,9 @@ checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" [[package]] name = "socket2" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" dependencies = [ "libc", "windows-sys 0.52.0", diff --git a/yazi-plugin/preset/components/current.lua b/yazi-plugin/preset/components/current.lua index a9979e21..b7222a52 100644 --- a/yazi-plugin/preset/components/current.lua +++ b/yazi-plugin/preset/components/current.lua @@ -20,7 +20,7 @@ function Current:empty() end return { - ui.Line(s):area(self._area):align(ui.Line.CENTER), + ui.Line(s):area(self._area):align(ui.Align.CENTER), } end @@ -43,7 +43,7 @@ function Current:redraw() return { ui.List(left):area(self._area), - ui.Text(right):area(self._area):align(ui.Text.RIGHT), + ui.Text(right):area(self._area):align(ui.Align.RIGHT), } end diff --git a/yazi-plugin/preset/components/header.lua b/yazi-plugin/preset/components/header.lua index ddb810c1..09d5d84c 100644 --- a/yazi-plugin/preset/components/header.lua +++ b/yazi-plugin/preset/components/header.lua @@ -1,4 +1,5 @@ Header = { + -- TODO: remove these two constants LEFT = 0, RIGHT = 1, @@ -83,7 +84,7 @@ function Header:redraw() return { ui.Line(left):area(self._area), - ui.Line(right):area(self._area):align(ui.Line.RIGHT), + ui.Line(right):area(self._area):align(ui.Align.RIGHT), } end diff --git a/yazi-plugin/preset/components/marker.lua b/yazi-plugin/preset/components/marker.lua index 28658210..00bd31a9 100644 --- a/yazi-plugin/preset/components/marker.lua +++ b/yazi-plugin/preset/components/marker.lua @@ -29,7 +29,7 @@ function Marker:redraw() w = 1, h = math.min(1 + last[2] - last[1], self._area.y + self._area.h - y), } - elements[#elements + 1] = ui.Bar(ui.Bar.LEFT):area(rect):style(last[3]) + elements[#elements + 1] = ui.Bar(ui.Edge.LEFT):area(rect):style(last[3]) end local last = { 0, 0, nil } -- start, end, style diff --git a/yazi-plugin/preset/components/rail.lua b/yazi-plugin/preset/components/rail.lua index ec79af70..d932de45 100644 --- a/yazi-plugin/preset/components/rail.lua +++ b/yazi-plugin/preset/components/rail.lua @@ -10,8 +10,8 @@ end function Rail:build() self._base = { - ui.Bar(ui.Bar.RIGHT):area(self._chunks[1]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), - ui.Bar(ui.Bar.LEFT):area(self._chunks[3]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), + ui.Bar(ui.Edge.RIGHT):area(self._chunks[1]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), + ui.Bar(ui.Edge.LEFT):area(self._chunks[3]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), } self._children = { Marker:new(self._chunks[1], self._tab.parent), diff --git a/yazi-plugin/preset/components/status.lua b/yazi-plugin/preset/components/status.lua index 4dfa2aff..323091e9 100644 --- a/yazi-plugin/preset/components/status.lua +++ b/yazi-plugin/preset/components/status.lua @@ -1,4 +1,5 @@ Status = { + -- TODO: remove these two constants LEFT = 0, RIGHT = 1, @@ -141,7 +142,7 @@ function Status:redraw() return { ui.Text(""):area(self._area):style(th.status.overall), ui.Line(left):area(self._area), - ui.Line(right):area(self._area):align(ui.Line.RIGHT), + ui.Line(right):area(self._area):align(ui.Align.RIGHT), table.unpack(ui.redraw(Progress:new(self._area, right_width))), } end diff --git a/yazi-plugin/preset/plugins/archive.lua b/yazi-plugin/preset/plugins/archive.lua index cd18c972..bc3dd8c7 100644 --- a/yazi-plugin/preset/plugins/archive.lua +++ b/yazi-plugin/preset/plugins/archive.lua @@ -45,7 +45,7 @@ function M:peek(job) else ya.preview_widget(job, { ui.Text(left):area(job.area), - ui.Text(right):area(job.area):align(ui.Text.RIGHT), + ui.Text(right):area(job.area):align(ui.Align.RIGHT), }) end end diff --git a/yazi-plugin/preset/plugins/empty.lua b/yazi-plugin/preset/plugins/empty.lua index 87cd0adf..6d4db81b 100644 --- a/yazi-plugin/preset/plugins/empty.lua +++ b/yazi-plugin/preset/plugins/empty.lua @@ -1,6 +1,6 @@ local M = {} -function M.msg(job, s) ya.preview_widget(job, ui.Text(ui.Line(s):reverse()):area(job.area):wrap(ui.Text.WRAP)) end +function M.msg(job, s) ya.preview_widget(job, ui.Text(ui.Line(s):reverse()):area(job.area):wrap(ui.Wrap.YES)) end function M:peek(job) local path = tostring(job.file.url) diff --git a/yazi-plugin/preset/plugins/file.lua b/yazi-plugin/preset/plugins/file.lua index 50694ab3..1399f474 100644 --- a/yazi-plugin/preset/plugins/file.lua +++ b/yazi-plugin/preset/plugins/file.lua @@ -11,7 +11,7 @@ function M:peek(job) text = ui.Text(string.format("Failed to start `%s`, error: %s", cmd, err)) end - ya.preview_widget(job, text:area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, text:area(job.area):wrap(ui.Wrap.YES)) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/folder.lua b/yazi-plugin/preset/plugins/folder.lua index 9d8b339d..6f03ed29 100644 --- a/yazi-plugin/preset/plugins/folder.lua +++ b/yazi-plugin/preset/plugins/folder.lua @@ -16,7 +16,7 @@ function M:peek(job) if #folder.files == 0 then local done, err = folder.stage() local s = not done and "Loading..." or not err and "No items" or string.format("Error: %s", err) - return ya.preview_widget(job, ui.Line(s):area(job.area):align(ui.Line.CENTER)) + return ya.preview_widget(job, ui.Line(s):area(job.area):align(ui.Align.CENTER)) end local items = {} diff --git a/yazi-plugin/preset/plugins/font.lua b/yazi-plugin/preset/plugins/font.lua index 0aa01d8d..3fe57fd8 100644 --- a/yazi-plugin/preset/plugins/font.lua +++ b/yazi-plugin/preset/plugins/font.lua @@ -16,7 +16,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(cache, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/image.lua b/yazi-plugin/preset/plugins/image.lua index 56bad52f..65a2e3ce 100644 --- a/yazi-plugin/preset/plugins/image.lua +++ b/yazi-plugin/preset/plugins/image.lua @@ -9,7 +9,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(url, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/json.lua b/yazi-plugin/preset/plugins/json.lua index bb56179b..dfccd22a 100644 --- a/yazi-plugin/preset/plugins/json.lua +++ b/yazi-plugin/preset/plugins/json.lua @@ -34,7 +34,7 @@ function M:peek(job) lines = lines:gsub("\t", string.rep(" ", rt.preview.tab_size)) ya.preview_widget( job, - ui.Text.parse(lines):area(job.area):wrap(rt.preview.wrap == "yes" and ui.Text.WRAP or ui.Text.WRAP_NO) + ui.Text.parse(lines):area(job.area):wrap(rt.preview.wrap == "yes" and ui.Wrap.YES or ui.Wrap.NO) ) end end diff --git a/yazi-plugin/preset/plugins/magick.lua b/yazi-plugin/preset/plugins/magick.lua index 28328739..4a2648b1 100644 --- a/yazi-plugin/preset/plugins/magick.lua +++ b/yazi-plugin/preset/plugins/magick.lua @@ -14,7 +14,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(cache, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/pdf.lua b/yazi-plugin/preset/plugins/pdf.lua index c40b7dce..df0c0de0 100644 --- a/yazi-plugin/preset/plugins/pdf.lua +++ b/yazi-plugin/preset/plugins/pdf.lua @@ -14,7 +14,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(cache, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek(job) diff --git a/yazi-plugin/preset/plugins/svg.lua b/yazi-plugin/preset/plugins/svg.lua index 415445b0..770a8557 100644 --- a/yazi-plugin/preset/plugins/svg.lua +++ b/yazi-plugin/preset/plugins/svg.lua @@ -14,7 +14,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(cache, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek() end diff --git a/yazi-plugin/preset/plugins/video.lua b/yazi-plugin/preset/plugins/video.lua index c5385758..f228ad82 100644 --- a/yazi-plugin/preset/plugins/video.lua +++ b/yazi-plugin/preset/plugins/video.lua @@ -14,7 +14,7 @@ function M:peek(job) ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock())) local _, err = ya.image_show(cache, job.area) - ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Text.WRAP)) + ya.preview_widget(job, err and ui.Text(tostring(err)):area(job.area):wrap(ui.Wrap.YES)) end function M:seek(job) diff --git a/yazi-plugin/src/elements/align.rs b/yazi-plugin/src/elements/align.rs new file mode 100644 index 00000000..564cfa70 --- /dev/null +++ b/yazi-plugin/src/elements/align.rs @@ -0,0 +1,40 @@ +use std::ops::Deref; + +use mlua::{FromLua, IntoLua, Lua, Value}; + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct Align(pub(super) ratatui::layout::Alignment); + +impl Deref for Align { + type Target = ratatui::layout::Alignment; + + fn deref(&self) -> &Self::Target { &self.0 } +} + +impl Align { + pub fn compose(lua: &Lua) -> mlua::Result { + lua.create_table_from([("LEFT", 0), ("CENTER", 1), ("RIGHT", 2)])?.into_lua(lua) + } +} + +impl FromLua for Align { + fn from_lua(value: Value, _: &Lua) -> mlua::Result { + let Value::Integer(n) = value else { + return Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Align".to_string(), + message: Some("expected an integer representation of Align".to_string()), + }); + }; + Ok(Self(match n { + 0 => ratatui::layout::Alignment::Left, + 1 => ratatui::layout::Alignment::Center, + 2 => ratatui::layout::Alignment::Right, + _ => Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Align".to_string(), + message: Some("invalid value for Align".to_string()), + })?, + })) + } +} diff --git a/yazi-plugin/src/elements/bar.rs b/yazi-plugin/src/elements/bar.rs index 8b30dc7a..ff306b3f 100644 --- a/yazi-plugin/src/elements/bar.rs +++ b/yazi-plugin/src/elements/bar.rs @@ -1,25 +1,24 @@ use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::Borders; -use super::Area; +use super::{Area, Edge}; #[derive(Clone, Debug, Default)] pub struct Bar { area: Area, - direction: ratatui::widgets::Borders, - symbol: String, - style: ratatui::style::Style, + edge: Edge, + symbol: String, + style: ratatui::style::Style, } impl Bar { pub fn compose(lua: &Lua) -> mlua::Result { - let new = lua.create_function(|_, (_, direction): (Table, u8)| { - Ok(Self { direction: Borders::from_bits_truncate(direction), ..Default::default() }) - })?; + let new = + lua.create_function(|_, (_, edge): (Table, Edge)| Ok(Self { edge, ..Default::default() }))?; let bar = lua.create_table_from([ - // Direction + // TODO: remove these constants ("NONE", Borders::NONE.bits()), ("TOP", Borders::TOP.bits()), ("RIGHT", Borders::RIGHT.bits()), @@ -44,31 +43,31 @@ impl Bar { let symbol = if !self.symbol.is_empty() { &self.symbol - } else if self.direction.intersects(Borders::TOP | Borders::BOTTOM) { + } else if self.edge.intersects(Borders::TOP | Borders::BOTTOM) { "─" - } else if self.direction.intersects(Borders::LEFT | Borders::RIGHT) { + } else if self.edge.intersects(Borders::LEFT | Borders::RIGHT) { "│" } else { " " }; - if self.direction.contains(Borders::LEFT) { + if self.edge.contains(Borders::LEFT) { for y in rect.top()..rect.bottom() { buf[(rect.left(), y)].set_style(self.style).set_symbol(symbol); } } - if self.direction.contains(Borders::TOP) { + if self.edge.contains(Borders::TOP) { for x in rect.left()..rect.right() { buf[(x, rect.top())].set_style(self.style).set_symbol(symbol); } } - if self.direction.contains(Borders::RIGHT) { + if self.edge.contains(Borders::RIGHT) { let x = rect.right() - 1; for y in rect.top()..rect.bottom() { buf[(x, y)].set_style(self.style).set_symbol(symbol); } } - if self.direction.contains(Borders::BOTTOM) { + if self.edge.contains(Borders::BOTTOM) { let y = rect.bottom() - 1; for x in rect.left()..rect.right() { buf[(x, y)].set_style(self.style).set_symbol(symbol); @@ -82,8 +81,8 @@ impl UserData for Bar { crate::impl_area_method!(methods); crate::impl_style_method!(methods, style); - methods.add_function_mut("direction", |_, (ud, symbol): (AnyUserData, u8)| { - ud.borrow_mut::()?.direction = Borders::from_bits_truncate(symbol); + methods.add_function_mut("edge", |_, (ud, edge): (AnyUserData, Edge)| { + ud.borrow_mut::()?.edge = edge; Ok(ud) }); methods.add_function_mut("symbol", |_, (ud, symbol): (AnyUserData, String)| { diff --git a/yazi-plugin/src/elements/border.rs b/yazi-plugin/src/elements/border.rs index d1ff3705..e069a8d4 100644 --- a/yazi-plugin/src/elements/border.rs +++ b/yazi-plugin/src/elements/border.rs @@ -1,7 +1,7 @@ use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::{Borders, Widget}; -use super::Area; +use super::{Area, Edge}; use crate::elements::Line; // Type @@ -16,24 +16,20 @@ const QUADRANT_OUTSIDE: u8 = 5; pub struct Border { pub(crate) area: Area, - pub(crate) position: ratatui::widgets::Borders, - pub(crate) r#type: ratatui::widgets::BorderType, - pub(crate) style: ratatui::style::Style, + pub(crate) edge: Edge, + pub(crate) r#type: ratatui::widgets::BorderType, + pub(crate) style: ratatui::style::Style, pub(crate) titles: Vec<(ratatui::widgets::block::Position, ratatui::text::Line<'static>)>, } impl Border { pub fn compose(lua: &Lua) -> mlua::Result { - let new = lua.create_function(|_, (_, position): (Table, u8)| { - Ok(Border { - position: ratatui::widgets::Borders::from_bits_truncate(position), - ..Default::default() - }) - })?; + let new = lua + .create_function(|_, (_, edge): (Table, Edge)| Ok(Border { edge, ..Default::default() }))?; let border = lua.create_table_from([ - // Position + // TODO: remove these constants ("NONE", Borders::NONE.bits()), ("TOP", Borders::TOP.bits()), ("RIGHT", Borders::RIGHT.bits()), @@ -59,7 +55,7 @@ impl Border { trans: impl FnOnce(yazi_config::popup::Position) -> ratatui::layout::Rect, ) { let mut block = ratatui::widgets::Block::default() - .borders(self.position) + .borders(self.edge.0) .border_type(self.r#type) .border_style(self.style); @@ -103,8 +99,8 @@ impl UserData for Border { Ok(ud) }, ); - methods.add_function_mut("position", |_, (ud, position): (AnyUserData, u8)| { - ud.borrow_mut::()?.position = ratatui::widgets::Borders::from_bits_truncate(position); + methods.add_function_mut("edge", |_, (ud, edge): (AnyUserData, Edge)| { + ud.borrow_mut::()?.edge = edge; Ok(ud) }); } diff --git a/yazi-plugin/src/elements/edge.rs b/yazi-plugin/src/elements/edge.rs new file mode 100644 index 00000000..a239ae60 --- /dev/null +++ b/yazi-plugin/src/elements/edge.rs @@ -0,0 +1,48 @@ +use std::ops::Deref; + +use mlua::{FromLua, IntoLua, Lua, Value}; +use ratatui::widgets::Borders; + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct Edge(pub Borders); + +impl Deref for Edge { + type Target = Borders; + + fn deref(&self) -> &Self::Target { &self.0 } +} + +impl Edge { + pub fn compose(lua: &Lua) -> mlua::Result { + lua + .create_table_from([ + ("NONE", Borders::NONE.bits()), + ("TOP", Borders::TOP.bits()), + ("RIGHT", Borders::RIGHT.bits()), + ("BOTTOM", Borders::BOTTOM.bits()), + ("LEFT", Borders::LEFT.bits()), + ("ALL", Borders::ALL.bits()), + ])? + .into_lua(lua) + } +} + +impl FromLua for Edge { + fn from_lua(value: Value, _: &Lua) -> mlua::Result { + let Value::Integer(n) = value else { + return Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Edge".to_string(), + message: Some("expected an integer representation of an Edge".to_string()), + }); + }; + let Ok(Some(b)) = u8::try_from(n).map(Borders::from_bits) else { + return Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Edge".to_string(), + message: Some("invalid bits for Edge".to_string()), + }); + }; + Ok(Self(b)) + } +} diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs index b2bd2e22..fd744ada 100644 --- a/yazi-plugin/src/elements/elements.rs +++ b/yazi-plugin/src/elements/elements.rs @@ -7,10 +7,12 @@ use crate::Composer; pub fn compose(lua: &Lua) -> mlua::Result { Composer::make(lua, 20, |lua, key| { match key { + b"Align" => super::Align::compose(lua)?, b"Bar" => super::Bar::compose(lua)?, b"Border" => super::Border::compose(lua)?, b"Clear" => super::Clear::compose(lua)?, b"Constraint" => super::Constraint::compose(lua)?, + b"Edge" => super::Edge::compose(lua)?, b"Gauge" => super::Gauge::compose(lua)?, b"Layout" => super::Layout::compose(lua)?, b"Line" => super::Line::compose(lua)?, diff --git a/yazi-plugin/src/elements/line.rs b/yazi-plugin/src/elements/line.rs index e5806bdb..8e9e80ec 100644 --- a/yazi-plugin/src/elements/line.rs +++ b/yazi-plugin/src/elements/line.rs @@ -6,10 +6,7 @@ use ratatui::widgets::Widget; use unicode_width::UnicodeWidthChar; use super::{Area, Span}; - -const LEFT: u8 = 0; -const CENTER: u8 = 1; -const RIGHT: u8 = 2; +use crate::elements::Align; const EXPECTED: &str = "expected a string, Span, Line, or a table of them"; @@ -40,10 +37,10 @@ impl Line { let line = lua.create_table_from([ ("parse", parse.into_lua(lua)?), - // Alignment - ("LEFT", LEFT.into_lua(lua)?), - ("CENTER", CENTER.into_lua(lua)?), - ("RIGHT", RIGHT.into_lua(lua)?), + // TODO: remove these constants + ("LEFT", 0.into_lua(lua)?), + ("CENTER", 1.into_lua(lua)?), + ("RIGHT", 2.into_lua(lua)?), ])?; line.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); @@ -120,12 +117,8 @@ impl UserData for Line { crate::impl_style_shorthands!(methods, inner.style); methods.add_method("width", |_, me, ()| Ok(me.inner.width())); - methods.add_function_mut("align", |_, (ud, align): (AnyUserData, u8)| { - ud.borrow_mut::()?.inner.alignment = Some(match align { - CENTER => ratatui::layout::Alignment::Center, - RIGHT => ratatui::layout::Alignment::Right, - _ => ratatui::layout::Alignment::Left, - }); + methods.add_function_mut("align", |_, (ud, align): (AnyUserData, Align)| { + ud.borrow_mut::()?.inner.alignment = Some(align.0); Ok(ud) }); methods.add_method("visible", |_, me, ()| { diff --git a/yazi-plugin/src/elements/mod.rs b/yazi-plugin/src/elements/mod.rs index 5987df66..a315c098 100644 --- a/yazi-plugin/src/elements/mod.rs +++ b/yazi-plugin/src/elements/mod.rs @@ -1,3 +1,3 @@ #![allow(clippy::module_inception)] -yazi_macro::mod_flat!(area bar border cell clear constraint elements gauge layout line list pad pos rect renderable row span style table text utils); +yazi_macro::mod_flat!(align area bar border cell clear constraint edge elements gauge layout line list pad pos rect renderable row span style table text utils wrap); diff --git a/yazi-plugin/src/elements/text.rs b/yazi-plugin/src/elements/text.rs index 4bff0438..a546901c 100644 --- a/yazi-plugin/src/elements/text.rs +++ b/yazi-plugin/src/elements/text.rs @@ -4,17 +4,8 @@ use ansi_to_tui::IntoText; use mlua::{AnyUserData, ExternalError, ExternalResult, IntoLua, Lua, MetaMethod, Table, UserData, Value}; use ratatui::widgets::Widget; -use super::{Area, Line, Span}; - -// Alignment -pub(super) const LEFT: u8 = 0; -pub(super) const CENTER: u8 = 1; -pub(super) const RIGHT: u8 = 2; - -// Wrap -pub const WRAP_NO: u8 = 0; -pub const WRAP: u8 = 1; -pub const WRAP_TRIM: u8 = 2; +use super::{Area, Line, Span, Wrap}; +use crate::elements::Align; const EXPECTED: &str = "expected a string, Line, Span, or a table of them"; @@ -24,7 +15,7 @@ pub struct Text { // TODO: block pub inner: ratatui::text::Text<'static>, - pub wrap: u8, + pub wrap: Wrap, pub scroll: ratatui::layout::Position, } @@ -38,14 +29,13 @@ impl Text { let text = lua.create_table_from([ ("parse", parse.into_lua(lua)?), - // Alignment - ("LEFT", LEFT.into_lua(lua)?), - ("CENTER", CENTER.into_lua(lua)?), - ("RIGHT", RIGHT.into_lua(lua)?), - // Wrap - ("WRAP_NO", WRAP_NO.into_lua(lua)?), - ("WRAP", WRAP.into_lua(lua)?), - ("WRAP_TRIM", WRAP_TRIM.into_lua(lua)?), + // TODO: remove these constants + ("LEFT", 0.into_lua(lua)?), + ("CENTER", 1.into_lua(lua)?), + ("RIGHT", 2.into_lua(lua)?), + ("WRAP_NO", 0.into_lua(lua)?), + ("WRAP", 1.into_lua(lua)?), + ("WRAP_TRIM", 2.into_lua(lua)?), ])?; text.set_metatable(Some(lua.create_table_from([(MetaMethod::Call.name(), new)])?)); @@ -58,7 +48,7 @@ impl Text { trans: impl Fn(yazi_config::popup::Position) -> ratatui::layout::Rect, ) { let rect = self.area.transform(trans); - if self.wrap == WRAP_NO && self.scroll == Default::default() { + if self.wrap.is_none() && self.scroll == Default::default() { self.inner.render(rect, buf); } else { ratatui::widgets::Paragraph::from(self).render(rect, buf); @@ -127,8 +117,8 @@ impl From for ratatui::widgets::Paragraph<'static> { if let Some(align) = align { p = p.alignment(align); } - if value.wrap != WRAP_NO { - p = p.wrap(ratatui::widgets::Wrap { trim: value.wrap == WRAP_TRIM }); + if let Some(wrap) = value.wrap.0 { + p = p.wrap(wrap); } p.scroll((value.scroll.y, value.scroll.x)) } @@ -140,19 +130,12 @@ impl UserData for Text { crate::impl_style_method!(methods, inner.style); crate::impl_style_shorthands!(methods, inner.style); - methods.add_function_mut("align", |_, (ud, align): (AnyUserData, u8)| { - ud.borrow_mut::()?.inner.alignment = Some(match align { - CENTER => ratatui::layout::Alignment::Center, - RIGHT => ratatui::layout::Alignment::Right, - _ => ratatui::layout::Alignment::Left, - }); + methods.add_function_mut("align", |_, (ud, align): (AnyUserData, Align)| { + ud.borrow_mut::()?.inner.alignment = Some(align.0); Ok(ud) }); - methods.add_function_mut("wrap", |_, (ud, wrap): (AnyUserData, u8)| { - ud.borrow_mut::()?.wrap = match wrap { - w @ (WRAP | WRAP_TRIM | WRAP_NO) => w, - _ => return Err("expected a WRAP, WRAP_TRIM or WRAP_NO".into_lua_err()), - }; + methods.add_function_mut("wrap", |_, (ud, wrap): (AnyUserData, Wrap)| { + ud.borrow_mut::()?.wrap = wrap; Ok(ud) }); methods.add_function_mut("scroll", |_, (ud, x, y): (AnyUserData, u16, u16)| { diff --git a/yazi-plugin/src/elements/wrap.rs b/yazi-plugin/src/elements/wrap.rs new file mode 100644 index 00000000..127e8bbf --- /dev/null +++ b/yazi-plugin/src/elements/wrap.rs @@ -0,0 +1,50 @@ +use std::ops::Deref; + +use mlua::{FromLua, IntoLua, Lua, Value}; +use yazi_config::preview::PreviewWrap; + +#[derive(Clone, Copy, Debug, Default)] +pub struct Wrap(pub(super) Option); + +impl Deref for Wrap { + type Target = Option; + + fn deref(&self) -> &Self::Target { &self.0 } +} + +impl Wrap { + pub fn compose(lua: &Lua) -> mlua::Result { + lua.create_table_from([("NO", 0), ("YES", 1), ("TRIM", 2)])?.into_lua(lua) + } +} + +impl From for Wrap { + fn from(value: PreviewWrap) -> Self { + Self(match value { + PreviewWrap::No => None, + PreviewWrap::Yes => Some(ratatui::widgets::Wrap { trim: false }), + }) + } +} + +impl FromLua for Wrap { + fn from_lua(value: Value, _: &Lua) -> mlua::Result { + let Value::Integer(n) = value else { + return Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Wrap".to_string(), + message: Some("expected an integer representation of a Wrap".to_string()), + }); + }; + Ok(Self(match n { + 0 => None, + 1 => Some(ratatui::widgets::Wrap { trim: false }), + 2 => Some(ratatui::widgets::Wrap { trim: true }), + _ => Err(mlua::Error::FromLuaConversionError { + from: value.type_name(), + to: "Wrap".to_string(), + message: Some("invalid value for Wrap".to_string()), + })?, + })) + } +} diff --git a/yazi-plugin/src/utils/preview.rs b/yazi-plugin/src/utils/preview.rs index e0ed3093..4ae11dde 100644 --- a/yazi-plugin/src/utils/preview.rs +++ b/yazi-plugin/src/utils/preview.rs @@ -1,10 +1,10 @@ use mlua::{AnyUserData, ExternalError, Function, IntoLuaMulti, Lua, Table, Value}; -use yazi_config::{YAZI, preview::PreviewWrap}; +use yazi_config::YAZI; use yazi_macro::emit; use yazi_shared::{errors::PeekError, event::Cmd}; use super::Utils; -use crate::{elements::{Area, Rect, Renderable, Text, WRAP, WRAP_NO}, external::Highlighter, file::FileRef}; +use crate::{elements::{Area, Rect, Renderable, Text}, external::Highlighter, file::FileRef}; #[derive(Debug, Default)] pub struct PreviewLock { @@ -51,7 +51,7 @@ impl Utils { lock.data = vec![Renderable::Text(Text { area, inner, - wrap: if YAZI.preview.wrap == PreviewWrap::Yes { WRAP } else { WRAP_NO }, + wrap: YAZI.preview.wrap.into(), scroll: Default::default(), })]; diff --git a/yazi-plugin/src/utils/spot.rs b/yazi-plugin/src/utils/spot.rs index 00cf6eb1..6899a611 100644 --- a/yazi-plugin/src/utils/spot.rs +++ b/yazi-plugin/src/utils/spot.rs @@ -4,7 +4,7 @@ use yazi_macro::emit; use yazi_shared::{Id, event::Cmd}; use super::Utils; -use crate::{elements::Renderable, file::FileRef}; +use crate::{elements::{Edge, Renderable}, file::FileRef}; pub struct SpotLock { pub url: yazi_shared::url::Url, @@ -73,7 +73,7 @@ impl Utils { Renderable::Clear(crate::elements::Clear { area }), Renderable::Border(crate::elements::Border { area, - position: ratatui::widgets::Borders::ALL, + edge: Edge(ratatui::widgets::Borders::ALL), r#type: ratatui::widgets::BorderType::Rounded, style: THEME.spot.border.into(), titles: vec![(