Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Dec 4, 2023
1 parent 1fee796 commit aa069cc
Show file tree
Hide file tree
Showing 29 changed files with 158 additions and 238 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions yazi-core/src/files/sorter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp::Ordering, collections::BTreeMap, mem};

use yazi_config::manager::SortBy;
use yazi_shared::{fs::File, fs::Url, natsort};
use yazi_shared::{fs::{File, Url}, natsort};

#[derive(Clone, Copy, Default, PartialEq)]
pub struct FilesSorter {
Expand Down Expand Up @@ -68,16 +68,16 @@ impl FilesSorter {
let mut entities = Vec::with_capacity(items.len());
for (i, file) in items.iter().enumerate() {
indices.push(i);
entities.push((file.url.to_string_lossy(), file));
entities.push(file.url.as_os_str().as_encoded_bytes());
}

indices.sort_unstable_by(|&a, &b| {
let promote = self.promote(entities[a].1, entities[b].1);
let promote = self.promote(&items[a], &items[b]);
if promote != Ordering::Equal {
return promote;
}

let ordering = natsort(&entities[a].0, &entities[b].0, !self.sensitive);
let ordering = natsort(entities[a], entities[b], !self.sensitive);
if self.reverse { ordering.reverse() } else { ordering }
});

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/manager/commands/peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ impl Manager {
return self.active_mut().preview.reset();
};

let (url, cha) = (hovered.url.clone(), hovered.cha);
let hovered = hovered.clone();
if opt.upper_bound {
self.active_mut().preview.arrow(0, &mime, Some(opt.step as usize));
} else if self.active().preview.same_url(&url) {
} else if self.active().preview.same_url(&hovered.url) {
self.active_mut().preview.arrow(opt.step, &mime, None);
} else {
self.active_mut().preview.arrow(0, &mime, Some(0));
self.active_mut().preview.reset();
}

self.active_mut().preview.go(&url, cha, &mime);
self.active_mut().preview.go(hovered, &mime);
false
}

Expand Down
24 changes: 13 additions & 11 deletions yazi-fm/src/lives/files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, UserDataMethods, UserDataRef};
use mlua::{AnyUserData, IntoLua, Lua, MetaMethod, UserDataMethods};
use yazi_config::THEME;
use yazi_plugin::{bindings::{Cast, File, Range}, layout::Style};

use super::{FolderRef, ManagerRef};

pub struct Files;

impl Files {
Expand All @@ -13,16 +15,16 @@ impl Files {
return Ok(Some(file.len));
}

let folder = me.named_user_value::<UserDataRef<yazi_core::tab::Folder>>("folder")?;
let folder = me.named_user_value::<FolderRef>("folder")?;
Ok(folder.files.sizes.get(&file.url).copied())
});
reg.add_function("mime", |_, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let file = me.borrow::<yazi_shared::fs::File>()?;
Ok(manager.mimetype.get(&file.url).cloned())
});
reg.add_function("prefix", |lua, me: AnyUserData| {
let folder = me.named_user_value::<UserDataRef<yazi_core::tab::Folder>>("folder")?;
let folder = me.named_user_value::<FolderRef>("folder")?;
if !folder.cwd.is_search() {
return Ok(None);
}
Expand All @@ -42,7 +44,7 @@ impl Files {
)
});
reg.add_function("style", |_, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let file = me.borrow::<yazi_shared::fs::File>()?;
let mime = manager.mimetype.get(&file.url);
Ok(
Expand All @@ -54,12 +56,12 @@ impl Files {
)
});
reg.add_function("is_hovered", |_, me: AnyUserData| {
let folder = me.named_user_value::<UserDataRef<yazi_core::tab::Folder>>("folder")?;
let folder = me.named_user_value::<FolderRef>("folder")?;
let file = me.borrow::<yazi_shared::fs::File>()?;
Ok(matches!(folder.hovered(), Some(f) if f.url == file.url))
});
reg.add_function("is_yanked", |_, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let file = me.borrow::<yazi_shared::fs::File>()?;
Ok(if !manager.yanked.1.contains(&file.url) {
0u8
Expand All @@ -70,8 +72,8 @@ impl Files {
})
});
reg.add_function("is_selected", |_, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let folder = me.named_user_value::<UserDataRef<yazi_core::tab::Folder>>("folder")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let folder = me.named_user_value::<FolderRef>("folder")?;
let file = me.borrow::<yazi_shared::fs::File>()?;

let selected = folder.files.is_selected(&file.url);
Expand All @@ -83,7 +85,7 @@ impl Files {
})
});
reg.add_function("found", |lua, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let Some(finder) = &manager.active().finder else {
return Ok(None);
};
Expand All @@ -98,7 +100,7 @@ impl Files {
Ok(None)
});
reg.add_function("highlights", |_, me: AnyUserData| {
let manager = me.named_user_value::<UserDataRef<yazi_core::manager::Manager>>("manager")?;
let manager = me.named_user_value::<ManagerRef>("manager")?;
let Some(finder) = &manager.active().finder else {
return Ok(None);
};
Expand Down
3 changes: 3 additions & 0 deletions yazi-fm/src/lives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub(super) use files::*;
pub(super) use lives::*;
pub(super) use tabs::*;
pub(super) use tasks::*;

type FolderRef<'lua> = mlua::UserDataRef<'lua, yazi_core::tab::Folder>;
type ManagerRef<'lua> = mlua::UserDataRef<'lua, yazi_core::manager::Manager>;
5 changes: 3 additions & 2 deletions yazi-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi"

[dependencies]
yazi-config = { path = "../yazi-config", version = "0.1.5" }
yazi-shared = { path = "../yazi-shared", version = "0.1.5" }
yazi-adaptor = { path = "../yazi-adaptor", version = "0.1.5" }
yazi-config = { path = "../yazi-config", version = "0.1.5" }
yazi-shared = { path = "../yazi-shared", version = "0.1.5" }

# External dependencies
ansi-to-tui = "^3"
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/plugins/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end
function Image:preload(file)
if not cache_exists(file.url .. tostring(file.modified)) then
local cache = cache_path(file.url .. file.modified)
return image_downscale(file.url, cache) and 1 or 0
return image_precache(file.url, cache) and 1 or 0
end
return 1
end
Expand Down
6 changes: 3 additions & 3 deletions yazi-plugin/src/bindings/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use mlua::{AnyUserData, Lua, MetaMethod, UserDataFields, UserDataMethods, UserDa

use super::Cast;

pub type UrlRef<'lua> = UserDataRef<'lua, yazi_shared::fs::Url>;

pub struct Url;

impl Url {
Expand All @@ -12,9 +14,7 @@ impl Url {
reg.add_field_method_get("is_search", |_, me| Ok(me.is_search()));
reg.add_field_method_get("is_archive", |_, me| Ok(me.is_archive()));

reg.add_meta_method(MetaMethod::Eq, |_, me, other: UserDataRef<yazi_shared::fs::Url>| {
Ok(me == &*other)
});
reg.add_meta_method(MetaMethod::Eq, |_, me, other: UrlRef| Ok(me == &*other));

reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
lua.create_string(me.as_os_str().as_encoded_bytes())
Expand Down
5 changes: 5 additions & 0 deletions yazi-plugin/src/isolate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod peek;
mod preload;

pub use peek::*;
pub use preload::*;
20 changes: 20 additions & 0 deletions yazi-plugin/src/isolate/peek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use mlua::{ExternalError, Lua, Table, TableExt};

use crate::bindings::{Cast, File};

pub async fn peek(file: yazi_shared::fs::File, skip: usize) -> mlua::Result<()> {
tokio::task::spawn_local(async move {
let lua = Lua::new();
crate::bindings::Url::register(&lua)?;
crate::bindings::File::register(&lua, |_| {})?;
crate::Utils::install(&lua)?;

let plugin: Table =
lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?;

let file = File::cast(&lua, file)?;
plugin.call_async_method::<_, ()>("peek", (file, skip)).await
})
.await
.map_err(|_| "peek error".into_lua_err())?
}
19 changes: 19 additions & 0 deletions yazi-plugin/src/isolate/preload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use mlua::{Lua, Table, TableExt};

use crate::bindings::{Cast, File};

pub async fn preload(files: &[yazi_shared::fs::File]) -> mlua::Result<()> {
let lua = Lua::new();
crate::bindings::Url::register(&lua)?;
crate::bindings::File::register(&lua, |_| {})?;
crate::Utils::install(&lua)?;

let plugin: Table =
lua.load(include_str!("../../../yazi-plugin/preset/plugins/image.lua")).call(())?;

for file in files {
let file = File::cast(&lua, file.clone())?;
plugin.call_async_method::<_, mlua::Error>("preload", file).await.ok();
}
Ok(())
}
7 changes: 3 additions & 4 deletions yazi-plugin/src/layout/bar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataRef, Value};
use ratatui::layout::Rect;
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value};

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

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

Expand Down
8 changes: 5 additions & 3 deletions yazi-plugin/src/layout/base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use mlua::{Lua, Table, UserData, UserDataRef};
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
use mlua::{Lua, Table, UserData};
use ratatui::{buffer::Buffer, widgets::Widget};

use super::RectRef;

#[derive(Clone, Copy)]
pub struct Base {
Expand All @@ -23,7 +25,7 @@ impl Base {
let base: Table = ui.get("Base")?;
base.set(
"new",
lua.create_function(|_, (area, kind): (UserDataRef<Rect>, u8)| {
lua.create_function(|_, (area, kind): (RectRef, u8)| {
Ok(Self { area: *area, kind: kind.into() })
})?,
)
Expand Down
8 changes: 4 additions & 4 deletions yazi-plugin/src/layout/border.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, UserDataRef, Value};
use ratatui::{layout::Rect, widgets::Widget};
use mlua::{AnyUserData, ExternalError, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;

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

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

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

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

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

use super::{Constraint, Rect};
use super::{Constraint, Rect, RectRef};
use crate::bindings::Cast;

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

let mut layout = ratatui::layout::Layout::new()
.direction(if me.direction {
ratatui::layout::Direction::Vertical
} else {
ratatui::layout::Direction::Horizontal
})
.constraints(me.constraints.as_slice());
let mut layout = ratatui::layout::Layout::new()
.direction(if me.direction {
ratatui::layout::Direction::Vertical
} else {
ratatui::layout::Direction::Horizontal
})
.constraints(me.constraints.as_slice());

if let Some(margin) = me.margin {
layout = layout.horizontal_margin(margin.horizontal);
layout = layout.vertical_margin(margin.vertical);
}
if let Some(margin) = me.margin {
layout = layout.horizontal_margin(margin.horizontal);
layout = layout.vertical_margin(margin.vertical);
}

let mut chunks = Vec::with_capacity(me.constraints.len());
for chunk in &*layout.split(*value) {
chunks.push(Rect::cast(lua, *chunk)?);
}
Ok(chunks)
},
);
let mut chunks = Vec::with_capacity(me.constraints.len());
for chunk in &*layout.split(*value) {
chunks.push(Rect::cast(lua, *chunk)?);
}
Ok(chunks)
});
}
}
Loading

0 comments on commit aa069cc

Please sign in to comment.