Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Dec 1, 2023
1 parent b199ab5 commit 10411c2
Show file tree
Hide file tree
Showing 24 changed files with 214 additions and 193 deletions.
10 changes: 6 additions & 4 deletions yazi-plugin/src/components/base.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{Table, UserData};
use ratatui::widgets::Widget;
use mlua::{Table, UserData, UserDataRef};
use ratatui::{layout::Rect, widgets::Widget};

use crate::{layout::Rect, LUA};
use crate::LUA;

#[derive(Clone)]
pub struct Base {
Expand All @@ -16,7 +16,9 @@ impl Base {
let base: Table = ui.get("Base")?;
base.set(
"new",
LUA.create_function(|_, (area, kind): (Rect, u8)| Ok(Self { area: area.0, kind }))?,
LUA.create_function(|_, (area, kind): (UserDataRef<Rect>, u8)| {
Ok(Self { area: *area, kind })
})?,
)
}

Expand Down
12 changes: 6 additions & 6 deletions yazi-plugin/src/components/folder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use ratatui::{layout::Rect, widgets::Widget};
use tracing::error;

use super::{layout, COMP_FOLDER};
use crate::{layout::Rect, LUA};
use crate::LUA;

pub struct Folder<'a> {
cx: &'a yazi_core::Ctx,
Expand All @@ -22,11 +22,11 @@ impl<'a> Folder<'a> {
}

impl<'a> Widget for Folder<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || {
let args = LUA.create_table()?;
args.set("kind", self.kind)?;
layout(COMP_FOLDER.call_method::<_, _>("render", (Rect(area), args))?, self.cx, buf)
let area = LUA.create_any_userdata(area)?;
let args = LUA.create_table_from([("kind", self.kind)])?;
layout(COMP_FOLDER.call_method::<_, _>("render", (area, args))?, self.cx, buf)
};
if let Err(e) = f() {
error!("{:?}", e);
Expand Down
11 changes: 7 additions & 4 deletions yazi-plugin/src/components/header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use ratatui::{prelude::Rect, widgets::Widget};
use tracing::error;

use super::{layout, COMP_HEADER};
use crate::layout::Rect;
use crate::LUA;

pub struct Header<'a> {
cx: &'a yazi_core::Ctx,
Expand All @@ -15,8 +15,11 @@ impl<'a> Header<'a> {
}

impl<'a> Widget for Header<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || layout(COMP_HEADER.call_method::<_, _>("render", Rect(area))?, self.cx, buf);
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || {
let area = LUA.create_any_userdata(area)?;
layout(COMP_HEADER.call_method::<_, _>("render", area)?, self.cx, buf)
};
if let Err(e) = f() {
error!("{:?}", e);
}
Expand Down
11 changes: 7 additions & 4 deletions yazi-plugin/src/components/manager.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use ratatui::{prelude::Rect, widgets::Widget};
use tracing::error;

use super::{layout, COMP_MANAGER};
use crate::layout::Rect;
use crate::LUA;

pub struct Manager<'a> {
cx: &'a yazi_core::Ctx,
Expand All @@ -14,8 +14,11 @@ impl<'a> Manager<'a> {
}

impl<'a> Widget for Manager<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || layout(COMP_MANAGER.call_method::<_, _>("render", Rect(area))?, self.cx, buf);
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || {
let area = LUA.create_any_userdata(area)?;
layout(COMP_MANAGER.call_method::<_, _>("render", area)?, self.cx, buf)
};
if let Err(e) = f() {
error!("{:?}", e);
}
Expand Down
11 changes: 7 additions & 4 deletions yazi-plugin/src/components/status.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use ratatui::{prelude::Rect, widgets::Widget};
use tracing::error;

use super::{layout, COMP_STATUS};
use crate::layout::Rect;
use crate::LUA;

pub struct Status<'a> {
cx: &'a yazi_core::Ctx,
Expand All @@ -15,8 +15,11 @@ impl<'a> Status<'a> {
}

impl<'a> Widget for Status<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || layout(COMP_STATUS.call_method::<_, _>("render", Rect(area))?, self.cx, buf);
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || {
let area = LUA.create_any_userdata(area)?;
layout(COMP_STATUS.call_method::<_, _>("render", area)?, self.cx, buf)
};
if let Err(e) = f() {
error!("{:?}", e);
}
Expand Down
10 changes: 5 additions & 5 deletions yazi-plugin/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use mlua::{Lua, LuaSerdeExt, SerializeOptions, Table};
use yazi_config::{MANAGER, THEME};

use crate::layout::Rect;

pub(super) struct Config;

impl Config {
Expand All @@ -26,12 +24,14 @@ impl Config {

layout.set(
"preview_rect",
lua.create_function(|_, ()| Ok(Rect(MANAGER.layout.preview_rect())))?,
lua.create_function(|lua, ()| lua.create_any_userdata(MANAGER.layout.preview_rect()))?,
)?;
layout
.set("preview_height", lua.create_function(|_, ()| Ok(MANAGER.layout.preview_height()))?)?;
layout
.set("folder_rect", lua.create_function(|_, ()| Ok(Rect(MANAGER.layout.folder_rect())))?)?;
layout.set(
"folder_rect",
lua.create_function(|lua, ()| lua.create_any_userdata(MANAGER.layout.folder_rect()))?,
)?;
layout
.set("folder_height", lua.create_function(|_, ()| Ok(MANAGER.layout.folder_height()))?)?;
}
Expand Down
9 changes: 5 additions & 4 deletions yazi-plugin/src/layout/bar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData, Value};
use mlua::{AnyUserData, Lua, Table, UserData, UserDataRef, Value};
use ratatui::layout::Rect;

use super::{Rect, Style};
use super::Style;

#[derive(Clone)]
pub struct Bar {
Expand All @@ -16,9 +17,9 @@ impl Bar {
let ui: Table = lua.globals().get("ui")?;
ui.set(
"Bar",
lua.create_function(|_, (area, direction): (Rect, u8)| {
lua.create_function(|_, (area, direction): (UserDataRef<Rect>, u8)| {
Ok(Self {
area: area.0,
area: *area,

position: match direction {
1 => ratatui::widgets::Borders::TOP,
Expand Down
10 changes: 5 additions & 5 deletions yazi-plugin/src/layout/border.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;
use mlua::{AnyUserData, Lua, Table, UserData, UserDataRef, Value};
use ratatui::{layout::Rect, widgets::Widget};

use super::{Rect, Style};
use super::Style;

#[derive(Clone)]
pub struct Border {
Expand All @@ -18,9 +18,9 @@ impl Border {
let border: Table = ui.get("Border")?;
border.set(
"new",
lua.create_function(|_, (area, position): (Rect, u8)| {
lua.create_function(|_, (area, position): (UserDataRef<Rect>, u8)| {
Ok(Self {
area: area.0,
area: *area,
position: ratatui::widgets::Borders::from_bits_truncate(position),
type_: Default::default(),
style: Default::default(),
Expand Down
10 changes: 6 additions & 4 deletions yazi-plugin/src/layout/gauge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods, Value};
use ratatui::widgets::Widget;
use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods, UserDataRef, Value};
use ratatui::{layout::Rect, widgets::Widget};

use super::{Rect, Span, Style};
use super::{Span, Style};

#[derive(Clone, Default)]
pub(crate) struct Gauge {
Expand All @@ -18,7 +18,9 @@ impl Gauge {
let ui: Table = lua.globals().get("ui")?;
ui.set(
"Gauge",
lua.create_function(|_, area: Rect| Ok(Gauge { area: area.0, ..Default::default() }))?,
lua.create_function(|_, area: UserDataRef<Rect>| {
Ok(Gauge { area: *area, ..Default::default() })
})?,
)
}

Expand Down
12 changes: 8 additions & 4 deletions yazi-plugin/src/layout/layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods};
use mlua::{AnyUserData, Lua, Table, UserData, UserDataMethods, UserDataRef};
use ratatui::layout::Rect;

use super::{Constraint, Rect};
use super::Constraint;

#[derive(Clone, Default)]
pub(crate) struct Layout {
Expand Down Expand Up @@ -52,7 +53,7 @@ impl UserData for Layout {
ud.borrow_mut::<Self>()?.constraints = value.into_iter().map(|c| c.0).collect();
Ok(ud)
});
methods.add_function("split", |_, (ud, value): (AnyUserData, Rect)| {
methods.add_function("split", |lua, (ud, value): (AnyUserData, UserDataRef<Rect>)| {
let me = ud.borrow::<Self>()?;

let mut layout = ratatui::layout::Layout::new()
Expand All @@ -68,7 +69,10 @@ impl UserData for Layout {
layout = layout.vertical_margin(margin.vertical);
}

let chunks: Vec<Rect> = layout.split(value.0).iter().copied().map(Rect).collect();
let mut chunks = Vec::with_capacity(me.constraints.len());
for chunk in &*layout.split(*value) {
chunks.push(lua.create_any_userdata(*chunk)?);
}
Ok(chunks)
});
}
Expand Down
10 changes: 5 additions & 5 deletions yazi-plugin/src/layout/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, FromLua, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;
use mlua::{AnyUserData, FromLua, Lua, Table, UserData, UserDataRef, Value};
use ratatui::{layout::Rect, widgets::Widget};

use super::{Line, Rect, Span, Style};
use super::{Line, Span, Style};

// --- List
#[derive(Clone)]
Expand All @@ -16,9 +16,9 @@ impl List {
let ui: Table = lua.globals().get("ui")?;
ui.set(
"List",
lua.create_function(|_, (area, items): (Rect, Vec<ListItem>)| {
lua.create_function(|_, (area, items): (UserDataRef<Rect>, Vec<ListItem>)| {
let items: Vec<_> = items.into_iter().map(|x| x.into()).collect();
Ok(Self { area: area.0, inner: ratatui::widgets::List::new(items) })
Ok(Self { area: *area, inner: ratatui::widgets::List::new(items) })
})?,
)
}
Expand Down
4 changes: 0 additions & 4 deletions yazi-plugin/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ mod gauge;
mod layout;
mod line;
mod list;
mod padding;
mod paragraph;
mod rect;
mod span;
mod style;

Expand All @@ -20,8 +18,6 @@ pub(super) use gauge::*;
pub(super) use layout::*;
pub(super) use line::*;
pub(super) use list::*;
pub(super) use padding::*;
pub(super) use paragraph::*;
pub(super) use rect::*;
pub(super) use span::*;
pub(super) use style::*;
26 changes: 0 additions & 26 deletions yazi-plugin/src/layout/padding.rs

This file was deleted.

10 changes: 5 additions & 5 deletions yazi-plugin/src/layout/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;
use mlua::{AnyUserData, Lua, Table, UserData, UserDataRef, Value};
use ratatui::{layout::Rect, widgets::Widget};

use super::{Rect, Style};
use super::Style;
use crate::layout::Line;

#[derive(Clone, Debug)]
Expand All @@ -18,9 +18,9 @@ impl Paragraph {
let ui: Table = lua.globals().get("ui")?;
ui.set(
"Paragraph",
lua.create_function(|_, (area, lines): (Rect, Vec<Line>)| {
lua.create_function(|_, (area, lines): (UserDataRef<Rect>, Vec<Line>)| {
Ok(Self {
area: area.0,
area: *area,

text: lines.into_iter().map(|s| s.0).collect::<Vec<_>>().into(),
style: None,
Expand Down
49 changes: 0 additions & 49 deletions yazi-plugin/src/layout/rect.rs

This file was deleted.

Loading

0 comments on commit 10411c2

Please sign in to comment.