From b4247a7d03a492a8427e08ad40ada8fa6b5b50e4 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Tue, 25 May 2021 14:44:26 +0530 Subject: [PATCH] Improve CallLua, mime_essence, permissions Refs: - https://github.com/sayanarijit/xplr/issues/187 - https://github.com/sayanarijit/xplr/issues/194 - https://github.com/sayanarijit/xplr/issues/195 --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- src/app.rs | 10 +++++++--- src/config.rs | 4 ++-- src/init.lua | 53 ++++++++++++++++++++++++++++++++++++++++----------- src/lua.rs | 15 +++++++-------- src/runner.rs | 8 +++++--- src/ui.rs | 22 +++++++-------------- 8 files changed, 75 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 078b699..5877627 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ansi-to-tui" -version = "0.1.9" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5091b918c2bf8daa9031e1cef129eafc45272352b885f114fb36a9aec512fcf2" +checksum = "bc671d766fb75259578ba58f2e492333693c5b028e6b345cc1ac49dfd4d71b95" dependencies = [ "tui", ] @@ -1108,7 +1108,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.11.1" +version = "0.12.0" dependencies = [ "ansi-to-tui", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 2159289..f1b9cbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.11.1" # Update lua.rs +version = "0.12.0" # Update lua.rs authors = ["Arijit Basu "] edition = "2018" description = "A hackable, minimal, fast TUI file explorer" @@ -29,7 +29,7 @@ indexmap = { version = "1.6.2", features = ["serde"] } natord = "1.0.9" humansize = "1.1.0" mlua = { version = "0.5.4", features = ["luajit", "vendored", "serialize", "send"] } -ansi-to-tui = "0.1.9" +ansi-to-tui = "0.2.0" libc = "0.2.94" [dev-dependencies] diff --git a/src/app.rs b/src/app.rs index 0071734..c53a0c2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1139,15 +1139,19 @@ pub enum ExternalMsg { /// **Example:** `CallSilently: {command: tput, args: ["bell"]}` CallSilently(Command), - /// Call a Lua function + /// Call a Lua function. + /// The complete app state (exportable using `PrintAppStateAndQuit` or `Debug`) + /// will be passed to the function as argument. + /// The function can optionally return a list of messages for xplr to handle + /// after the executing the function. /// - /// **Example:** `CallLua: custom.foo_funtion` + /// **Example:** `CallLua: custom.some_custom_funtion` CallLua(String), /// Like `CallLua` but without the flicker. The stdin, stdout /// stderr will be piped to null. So it's non-interactive. /// - /// **Example:** `CallLuaSilently: custom.bar_function` + /// **Example:** `CallLuaSilently: custom.some_custom_function` CallLuaSilently(String), /// An alias to `Call: {command: bash, args: ["-c", "${command}"], silent: false}` diff --git a/src/config.rs b/src/config.rs index 0d9f1d8..1155cbb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,7 +84,7 @@ pub struct NodeTypesConfig { pub symlink: NodeTypeConfig, #[serde(default)] - pub mime_essence: HashMap, + pub mime_essence: HashMap>, #[serde(default)] pub extension: HashMap, @@ -110,7 +110,7 @@ impl NodeTypesConfig { } /// Get a reference to the node types config's mime essence. - pub fn mime_essence(&self) -> &HashMap { + pub fn mime_essence(&self) -> &HashMap> { &self.mime_essence } diff --git a/src/init.lua b/src/init.lua index 4c315e2..383a65e 100644 --- a/src/init.lua +++ b/src/init.lua @@ -279,7 +279,7 @@ xplr.config.general.table.col_widths = { xplr.config.general.table.header.cols = { { format = " index", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, { format = "╭──── path", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, - { format = "permissions", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, + { format = " permissions", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, { format = "size", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, { format = "type", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } }, } @@ -2083,21 +2083,52 @@ xplr.fn.builtin.fmt_general_table_row_cols_2 = function(m) end end - local r = "" + local p = m.permissions + + -- TODO: Track https://github.com/uttarayan21/ansi-to-tui/issues/3 + local r = " " + -- User - r = r .. bit("r", green, m.permissions.user_read) - r = r .. bit("w", yellow, m.permissions.user_write) - r = r .. bit("x", red, m.permissions.user_execute) + r = r .. bit("r", green, p.user_read) + r = r .. bit("w", yellow, p.user_write) + + if p.user_execute == false and p.setuid == false then + r = r .. bit("-", red, p.user_execute) + elseif p.user_execute == true and p.setuid == false then + r = r .. bit("x", red, p.user_execute) + elseif p.user_execute == false and p.setuid == true then + r = r .. bit("S", red, p.user_execute) + else + r = r .. bit("s", red, p.user_execute) + end -- Group - r = r .. bit("r", green, m.permissions.group_read) - r = r .. bit("w", yellow, m.permissions.group_write) - r = r .. bit("x", red, m.permissions.group_execute) + r = r .. bit("r", green, p.group_read) + r = r .. bit("w", yellow, p.group_write) + + if p.group_execute == false and p.setuid == false then + r = r .. bit("-", red, p.group_execute) + elseif p.group_execute == true and p.setuid == false then + r = r .. bit("x", red, p.group_execute) + elseif p.group_execute == false and p.setuid == true then + r = r .. bit("S", red, p.group_execute) + else + r = r .. bit("s", red, p.group_execute) + end -- Other - r = r .. bit("r", green, m.permissions.other_read) - r = r .. bit("w", yellow, m.permissions.other_write) - r = r .. bit("x", red, m.permissions.other_execute) + r = r .. bit("r", green, p.other_read) + r = r .. bit("w", yellow, p.other_write) + + if p.other_execute == false and p.setuid == false then + r = r .. bit("-", red, p.other_execute) + elseif p.other_execute == true and p.setuid == false then + r = r .. bit("x", red, p.other_execute) + elseif p.other_execute == false and p.setuid == true then + r = r .. bit("S", red, p.other_execute) + else + r = r .. bit("s", red, p.other_execute) + end return r end diff --git a/src/lua.rs b/src/lua.rs index 46da35b..710546b 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -120,9 +120,9 @@ pub fn call<'lua, A: Serialize, R: Deserialize<'lua>>( ) -> Result { let func = resolve_fn(&lua.globals(), func)?; let args = lua.to_value(args)?; - let msgs: mlua::Value = func.call(args)?; - let msgs: R = lua.from_value(msgs)?; - Ok(msgs) + let res: mlua::Value = func.call(args)?; + let res: R = lua.from_value(res)?; + Ok(res) } #[cfg(test)] @@ -133,11 +133,10 @@ mod test { #[test] fn test_compatibility() { assert!(check_version(VERSION, "foo path").is_ok()); - assert!(check_version("0.11.0", "foo path").is_ok()); - assert!(check_version("0.11.1", "foo path").is_ok()); + assert!(check_version("0.12.0", "foo path").is_ok()); - assert!(check_version("0.11.2", "foo path").is_err()); - assert!(check_version("0.10.1", "foo path").is_err()); - assert!(check_version("1.12.1", "foo path").is_err()); + assert!(check_version("0.12.1", "foo path").is_err()); + assert!(check_version("0.10.0", "foo path").is_err()); + assert!(check_version("1.12.0", "foo path").is_err()); } } diff --git a/src/runner.rs b/src/runner.rs index 0eba62d..bb89b0a 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -26,7 +26,7 @@ fn call_lua( lua: &mlua::Lua, func: &str, silent: bool, -) -> Result> { +) -> Result>> { let _focus_index = app .directory_buffer() .map(|d| d.focus()) @@ -195,7 +195,7 @@ pub fn run( tx_event_reader.send(true)?; match call_lua(&app, &lua, &func, false) { - Ok(msgs) => { + Ok(Some(msgs)) => { for msg in msgs { app = app.handle_task(app::Task::new( app::MsgIn::External(msg), @@ -203,6 +203,7 @@ pub fn run( ))?; } } + Ok(None) => {} Err(err) => { app = app.log_error(err.to_string())?; } @@ -260,7 +261,7 @@ pub fn run( terminal.show_cursor()?; match call_lua(&app, &lua, &func, false) { - Ok(msgs) => { + Ok(Some(msgs)) => { for msg in msgs { app = app.handle_task(app::Task::new( app::MsgIn::External(msg), @@ -268,6 +269,7 @@ pub fn run( ))?; } } + Ok(None) => {} Err(err) => { app = app.log_error(err.to_string())?; } diff --git a/src/ui.rs b/src/ui.rs index 9443240..ad7ab26 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -475,6 +475,9 @@ fn draw_table( }) .unwrap_or_default(); + let (mimetype, mimesub) = + node.mime_essence().split_once("/").unwrap_or_default(); + let node_type = app_config .node_types() .special() @@ -484,7 +487,8 @@ fn draw_table( app_config .node_types() .mime_essence() - .get(node.mime_essence()) + .get(mimetype) + .and_then(|t| t.get(mimesub).or_else(|| t.get("*"))) }) .unwrap_or_else(|| { if node.is_symlink() { @@ -556,20 +560,8 @@ fn draw_table( c.format().as_ref().map(|f| { let out: Result = lua::call(lua, f, &v); match out { - Ok(o) => { - let text = - ansi_to_text(o.bytes()).unwrap_or_else(|e| { - Text::raw(format!("{:?}", e)) - }); - - // TODO: Track https://github.com/uttarayan21/ansi-to-tui/issues/2 - // And https://github.com/fdehau/tui-rs/issues/304 - if text.lines.is_empty() { - Text::raw(o.to_string()) - } else { - text - } - } + Ok(o) => ansi_to_text(o.bytes()) + .unwrap_or_else(|e| Text::raw(format!("{:?}", e))), Err(e) => Text::raw(e.to_string()), } })