From 35f4c73ede71482bfef8d16fd5d5277ffc688fee Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 12 Oct 2023 00:01:42 +0800 Subject: [PATCH] .. --- Cargo.lock | 1 + app/src/header/layout.rs | 13 ------------- app/src/header/mod.rs | 3 --- app/src/main.rs | 2 -- app/src/manager/folder.rs | 18 +++++------------- app/src/manager/layout.rs | 6 +++--- app/src/manager/preview.rs | 4 ++-- app/src/root.rs | 11 ++++++++--- app/src/status/layout.rs | 14 -------------- app/src/status/mod.rs | 3 --- config/preset/theme.toml | 3 ++- config/preset/yazi.toml | 3 +++ config/src/bindings.rs | 0 config/src/lib.rs | 6 ++++-- config/src/plugins/mod.rs | 3 +++ config/src/plugins/plugins.rs | 29 +++++++++++++++++++++++++++++ config/src/theme/status.rs | 1 + plugin/preset/components/status.lua | 4 +++- plugin/src/plugin.rs | 15 ++++++++++++--- shared/Cargo.toml | 1 + 20 files changed, 77 insertions(+), 63 deletions(-) delete mode 100644 app/src/header/layout.rs delete mode 100644 app/src/header/mod.rs delete mode 100644 app/src/status/layout.rs delete mode 100644 app/src/status/mod.rs delete mode 100644 config/src/bindings.rs create mode 100644 config/src/plugins/mod.rs create mode 100644 config/src/plugins/plugins.rs diff --git a/Cargo.lock b/Cargo.lock index c2f2b3a2..51fc9a5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1649,6 +1649,7 @@ dependencies = [ "libc", "parking_lot", "ratatui", + "regex", "tokio", ] diff --git a/app/src/header/layout.rs b/app/src/header/layout.rs deleted file mode 100644 index 122102d8..00000000 --- a/app/src/header/layout.rs +++ /dev/null @@ -1,13 +0,0 @@ -use ratatui::{prelude::{Buffer, Rect}, widgets::Widget}; -use tracing::info; - -pub(crate) struct Layout; - -impl Widget for Layout { - fn render(self, area: Rect, buf: &mut Buffer) { - let x = plugin::Header.render(area, buf); - if x.is_err() { - info!("{:?}", x); - } - } -} diff --git a/app/src/header/mod.rs b/app/src/header/mod.rs deleted file mode 100644 index 03520470..00000000 --- a/app/src/header/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod layout; - -pub(super) use layout::*; diff --git a/app/src/main.rs b/app/src/main.rs index 9ceac5e7..d9b0e68c 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -2,7 +2,6 @@ mod app; mod executor; -mod header; mod help; mod input; mod logs; @@ -10,7 +9,6 @@ mod manager; mod root; mod select; mod signals; -mod status; mod tasks; mod which; diff --git a/app/src/manager/folder.rs b/app/src/manager/folder.rs index 23dd69f3..58b98464 100644 --- a/app/src/manager/folder.rs +++ b/app/src/manager/folder.rs @@ -1,25 +1,17 @@ use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; -use tracing::info; +use tracing::error; -pub(super) struct Folder { - kind: FolderKind, -} - -pub(super) enum FolderKind { +pub(super) enum Folder { Parent = 0, Current = 1, Preview = 2, } -impl Folder { - pub(super) fn new(kind: FolderKind) -> Self { Self { kind } } -} - impl Widget for Folder { fn render(self, area: Rect, buf: &mut Buffer) { - let x = plugin::Folder { kind: self.kind as u8 }.render(area, buf); - if x.is_err() { - info!("{:?}", x); + let folder = plugin::Folder { kind: self as u8 }; + if let Err(e) = folder.render(area, buf) { + error!("{:?}", e); } } } diff --git a/app/src/manager/layout.rs b/app/src/manager/layout.rs index f0e659ff..80c3bf5c 100644 --- a/app/src/manager/layout.rs +++ b/app/src/manager/layout.rs @@ -3,7 +3,7 @@ use core::Ctx; use config::MANAGER; use ratatui::{buffer::Buffer, layout::{self, Constraint, Direction, Rect}, widgets::{Block, Borders, Padding, Widget}}; -use super::{folder::FolderKind, Folder, Preview}; +use super::{Folder, Preview}; pub(crate) struct Layout<'a> { cx: &'a Ctx, @@ -30,12 +30,12 @@ impl<'a> Widget for Layout<'a> { // Parent let block = Block::new().borders(Borders::RIGHT).padding(Padding::new(1, 0, 0, 0)); if manager.parent().is_some() { - Folder::new(FolderKind::Parent).render(block.inner(chunks[0]), buf); + Folder::Parent.render(block.inner(chunks[0]), buf); } block.render(chunks[0], buf); // Current - Folder::new(FolderKind::Current).render(chunks[1], buf); + Folder::Current.render(chunks[1], buf); // Preview let block = Block::new().borders(Borders::LEFT).padding(Padding::new(0, 1, 0, 0)); diff --git a/app/src/manager/preview.rs b/app/src/manager/preview.rs index 455d1ca8..db3abc07 100644 --- a/app/src/manager/preview.rs +++ b/app/src/manager/preview.rs @@ -3,7 +3,7 @@ use core::{manager::PreviewData, Ctx}; use ansi_to_tui::IntoText; use ratatui::{buffer::Buffer, layout::Rect, widgets::{Paragraph, Widget}}; -use super::{folder::FolderKind, Folder}; +use super::Folder; pub(super) struct Preview<'a> { cx: &'a Ctx, @@ -27,7 +27,7 @@ impl<'a> Widget for Preview<'a> { match &preview.lock.as_ref().unwrap().data { PreviewData::Folder => { - Folder::new(FolderKind::Preview).render(area, buf); + Folder::Preview.render(area, buf); } PreviewData::Text(s) => { let p = Paragraph::new(s.as_bytes().into_text().unwrap()); diff --git a/app/src/root.rs b/app/src/root.rs index 68985030..7abd11ab 100644 --- a/app/src/root.rs +++ b/app/src/root.rs @@ -1,8 +1,9 @@ use core::Ctx; use ratatui::{buffer::Buffer, layout::{Constraint, Direction, Layout, Rect}, widgets::Widget}; +use tracing::error; -use super::{header, input, manager, select, status, tasks, which}; +use super::{input, manager, select, tasks, which}; use crate::help; pub(super) struct Root<'a> { @@ -20,9 +21,13 @@ impl<'a> Widget for Root<'a> { .constraints([Constraint::Length(1), Constraint::Min(0), Constraint::Length(1)]) .split(area); - header::Layout.render(chunks[0], buf); + if let Err(e) = plugin::Header.render(chunks[0], buf) { + error!("{:?}", e); + } manager::Layout::new(self.cx).render(chunks[1], buf); - status::Layout.render(chunks[2], buf); + if let Err(e) = plugin::Status.render(chunks[2], buf) { + error!("{:?}", e); + } if self.cx.tasks.visible { tasks::Layout::new(self.cx).render(area, buf); diff --git a/app/src/status/layout.rs b/app/src/status/layout.rs deleted file mode 100644 index d31ed5e1..00000000 --- a/app/src/status/layout.rs +++ /dev/null @@ -1,14 +0,0 @@ -use ratatui::{buffer::Buffer, prelude::Rect, widgets::Widget}; -use tracing::info; - -pub(crate) struct Layout; - -impl Widget for Layout { - fn render(self, area: Rect, buf: &mut Buffer) { - let x = plugin::Status.render(area, buf); - if x.is_err() { - info!("{:?}", x); - return; - } - } -} diff --git a/app/src/status/mod.rs b/app/src/status/mod.rs deleted file mode 100644 index 03520470..00000000 --- a/app/src/status/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod layout; - -pub(super) use layout::*; diff --git a/config/preset/theme.toml b/config/preset/theme.toml index 0213dcb5..0e0ed90f 100644 --- a/config/preset/theme.toml +++ b/config/preset/theme.toml @@ -19,10 +19,11 @@ progress_normal = { fg = "#FFA577", bg = "#484D66" } progress_error = { fg = "#FF84A9", bg = "#484D66" } # Permissions -permissions_t = { fg = "#6D738F" } +permissions_t = { fg = "#97DC8D" } permissions_r = { fg = "#F3D398" } permissions_w = { fg = "#FA7F94" } permissions_x = { fg = "#7AD9E5" } +permissions_s = { fg = "#6D738F" } [files] hovered = { fg = "#1E2031", bg = "#80AEFA" } diff --git a/config/preset/yazi.toml b/config/preset/yazi.toml index f5deb078..b650e1bc 100644 --- a/config/preset/yazi.toml +++ b/config/preset/yazi.toml @@ -70,5 +70,8 @@ micro_workers = 5 macro_workers = 10 bizarre_retry = 5 +[plugins] +preload = [] + [log] enabled = false diff --git a/config/src/bindings.rs b/config/src/bindings.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/config/src/lib.rs b/config/src/lib.rs index 8a081e30..4d6e45a6 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -2,16 +2,16 @@ use shared::RoCell; -mod bindings; mod boot; pub mod keymap; mod log; pub mod manager; pub mod open; mod pattern; +pub mod plugins; mod preset; pub mod preview; -pub mod tasks; +mod tasks; pub mod theme; mod validation; mod xdg; @@ -28,6 +28,7 @@ pub static KEYMAP: RoCell = RoCell::new(); pub static LOG: RoCell = RoCell::new(); pub static MANAGER: RoCell = RoCell::new(); pub static OPEN: RoCell = RoCell::new(); +pub static PLUGINS: RoCell = RoCell::new(); pub static PREVIEW: RoCell = RoCell::new(); pub static TASKS: RoCell = RoCell::new(); pub static THEME: RoCell = RoCell::new(); @@ -43,6 +44,7 @@ pub fn init() { LOG.with(Default::default); MANAGER.with(Default::default); OPEN.with(Default::default); + PLUGINS.with(Default::default); PREVIEW.with(Default::default); TASKS.with(Default::default); THEME.with(Default::default); diff --git a/config/src/plugins/mod.rs b/config/src/plugins/mod.rs new file mode 100644 index 00000000..1e08e1ac --- /dev/null +++ b/config/src/plugins/mod.rs @@ -0,0 +1,3 @@ +mod plugins; + +pub use plugins::*; diff --git a/config/src/plugins/plugins.rs b/config/src/plugins/plugins.rs new file mode 100644 index 00000000..75da1988 --- /dev/null +++ b/config/src/plugins/plugins.rs @@ -0,0 +1,29 @@ +use std::path::PathBuf; + +use serde::Deserialize; +use shared::expand_path; +use validator::Validate; + +use crate::MERGED_YAZI; + +#[derive(Debug, Deserialize, Validate)] +pub struct Plugins { + pub preload: Vec, +} + +impl Default for Plugins { + fn default() -> Self { + #[derive(Deserialize)] + struct Outer { + plugins: Plugins, + } + + let mut plugins = toml::from_str::(&MERGED_YAZI).unwrap().plugins; + + plugins.preload.iter_mut().for_each(|p| { + *p = expand_path(&p); + }); + + plugins + } +} diff --git a/config/src/theme/status.rs b/config/src/theme/status.rs index 8198b488..672f0e0b 100644 --- a/config/src/theme/status.rs +++ b/config/src/theme/status.rs @@ -23,6 +23,7 @@ pub struct Status { pub permissions_r: Style, pub permissions_w: Style, pub permissions_x: Style, + pub permissions_s: Style, } #[derive(Deserialize, Serialize)] diff --git a/plugin/preset/components/status.lua b/plugin/preset/components/status.lua index 40e66048..04b3a964 100644 --- a/plugin/preset/components/status.lua +++ b/plugin/preset/components/status.lua @@ -55,7 +55,9 @@ function Status:permissions() for i = 1, #h.permissions do local c = h.permissions:sub(i, i) local style = THEME.status.permissions_t - if c == "r" then + if c == "-" then + style = THEME.status.permissions_s + elseif c == "r" then style = THEME.status.permissions_r elseif c == "w" then style = THEME.status.permissions_w diff --git a/plugin/src/plugin.rs b/plugin/src/plugin.rs index 0f97e825..76669fbe 100644 --- a/plugin/src/plugin.rs +++ b/plugin/src/plugin.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use config::PLUGINS; use mlua::{Lua, Table}; use shared::RoCell; @@ -8,13 +9,13 @@ pub(crate) static LUA: RoCell = RoCell::new(); pub(crate) static GLOBALS: RoCell = RoCell::new(); pub fn init() { - fn inner() -> Result<()> { + fn stage_1() -> Result<()> { let lua = Lua::new(); // Base + lua.load(include_str!("../preset/inspect/inspect.lua")).exec()?; lua.load(include_str!("../preset/ui.lua")).exec()?; lua.load(include_str!("../preset/utils.lua")).exec()?; - lua.load(include_str!("../preset/inspect/inspect.lua")).exec()?; // Components lua.load(include_str!("../preset/components/folder.lua")).exec()?; @@ -44,5 +45,13 @@ pub fn init() { Ok(()) } - inner().expect("failed to initialize Lua"); + fn stage_2() { + PLUGINS.preload.iter().for_each(|p| { + let b = std::fs::read(p).unwrap_or_else(|_| panic!("failed to read plugin: {p:?}")); + LUA.load(&b).exec().unwrap_or_else(|_| panic!("failed to load plugin: {p:?}")); + }); + } + + stage_1().expect("failed to initialize Lua"); + stage_2(); } diff --git a/shared/Cargo.toml b/shared/Cargo.toml index d631eda0..3681f8b1 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -10,4 +10,5 @@ futures = "^0" libc = "^0" parking_lot = "^0" ratatui = "^0" +regex = "^1" tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs" ] }