Skip to content

Commit

Permalink
feat: add a compatibility layer for ui.Paragraph to help users tran…
Browse files Browse the repository at this point in the history
…sition more smoothly to the new `ui.Text` (#1794)
  • Loading branch information
sxyazi authored Oct 16, 2024
1 parent 0824e11 commit e58cf55
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions yazi-plugin/src/elements/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn pour(lua: &Lua) -> mlua::Result<()> {
super::Line::install(lua, &ui)?;
super::List::install(lua, &ui)?;
super::Padding::install(lua, &ui)?;
super::Paragraph::install(lua, &ui)?;
super::Position::install(lua, &ui)?;
super::Rect::install(lua, &ui)?;
super::Span::install(lua, &ui)?;
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/src/elements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![allow(clippy::module_inception)]

yazi_macro::mod_flat!(bar border clear constraint elements gauge layout line list padding position rect span style table text);
yazi_macro::mod_flat!(bar border clear constraint elements gauge layout line list padding paragraph position rect span style table text);
65 changes: 65 additions & 0 deletions yazi-plugin/src/elements/paragraph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::time::Duration;

use mlua::{FromLua, Lua, MultiValue, Table, Value};

use super::Rect;
use crate::RtRef;

static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

#[inline]
fn warn_deprecated(id: Option<&str>) {
if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
let id = match id {
Some(id) => format!("`{id}.yazi` plugin"),
None => "`init.lua` config".to_owned(),
};
let s = "The `ui.Paragraph` and `ui.ListItem` elements have been deprecated in Yazi v0.4.
Please use the new `ui.Text` instead, in your {id}. See https://github.com/sxyazi/yazi/pull/1776 for details.";
yazi_proxy::AppProxy::notify(yazi_proxy::options::NotifyOpt {
title: "Deprecated API".to_owned(),
content: s.replace("{id}", &id),
level: yazi_proxy::options::NotifyLevel::Warn,
timeout: Duration::from_secs(20),
});
}
}

// TODO: remove this after v0.4 release
#[derive(Clone, Default, FromLua)]
pub struct Paragraph;

impl Paragraph {
pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> {
let mt = lua.create_table_from([
(
"__call",
lua.create_function(|lua, (_, area, lines): (Table, Rect, Value)| {
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua
.load(mlua::chunk! {
return ui.Text($lines):area($area)
})
.call::<_, MultiValue>(())
})?,
),
(
"__index",
lua.create_function(|lua, (_, key): (Table, mlua::String)| {
warn_deprecated(lua.named_registry_value::<RtRef>("rt")?.current());
lua
.load(mlua::chunk! {
return ui.Text[$key]
})
.call::<_, MultiValue>(())
})?,
),
])?;

let paragraph = lua.create_table()?;
paragraph.set_metatable(Some(mt));

ui.raw_set("Paragraph", paragraph)
}
}
1 change: 1 addition & 0 deletions yazi-plugin/src/isolate/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn slim_lua(name: &str) -> mlua::Result<Lua> {
// Elements
let ui = lua.create_table()?;
elements::Line::install(&lua, &ui)?;
elements::Paragraph::install(&lua, &ui)?;
elements::Rect::install(&lua, &ui)?;
elements::Span::install(&lua, &ui)?;
elements::Text::install(&lua, &ui)?;
Expand Down

0 comments on commit e58cf55

Please sign in to comment.