Skip to content

Commit

Permalink
Improve CallLua, mime_essence, permissions
Browse files Browse the repository at this point in the history
Refs:
- #187
- #194
- #195
  • Loading branch information
sayanarijit committed May 25, 2021
1 parent 9b02ef3 commit b4247a7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xplr"
version = "0.11.1" # Update lua.rs
version = "0.12.0" # Update lua.rs
authors = ["Arijit Basu <sayanarijit@gmail.com>"]
edition = "2018"
description = "A hackable, minimal, fast TUI file explorer"
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 7 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct NodeTypesConfig {
pub symlink: NodeTypeConfig,

#[serde(default)]
pub mime_essence: HashMap<String, NodeTypeConfig>,
pub mime_essence: HashMap<String, HashMap<String, NodeTypeConfig>>,

#[serde(default)]
pub extension: HashMap<String, NodeTypeConfig>,
Expand All @@ -110,7 +110,7 @@ impl NodeTypesConfig {
}

/// Get a reference to the node types config's mime essence.
pub fn mime_essence(&self) -> &HashMap<String, NodeTypeConfig> {
pub fn mime_essence(&self) -> &HashMap<String, HashMap<String, NodeTypeConfig>> {
&self.mime_essence
}

Expand Down
53 changes: 42 additions & 11 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
}
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ pub fn call<'lua, A: Serialize, R: Deserialize<'lua>>(
) -> Result<R> {
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)]
Expand All @@ -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());
}
}
8 changes: 5 additions & 3 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn call_lua(
lua: &mlua::Lua,
func: &str,
silent: bool,
) -> Result<Vec<app::ExternalMsg>> {
) -> Result<Option<Vec<app::ExternalMsg>>> {
let _focus_index = app
.directory_buffer()
.map(|d| d.focus())
Expand Down Expand Up @@ -195,14 +195,15 @@ 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),
None,
))?;
}
}
Ok(None) => {}
Err(err) => {
app = app.log_error(err.to_string())?;
}
Expand Down Expand Up @@ -260,14 +261,15 @@ 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),
None,
))?;
}
}
Ok(None) => {}
Err(err) => {
app = app.log_error(err.to_string())?;
}
Expand Down
22 changes: 7 additions & 15 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ fn draw_table<B: Backend>(
})
.unwrap_or_default();

let (mimetype, mimesub) =
node.mime_essence().split_once("/").unwrap_or_default();

let node_type = app_config
.node_types()
.special()
Expand All @@ -484,7 +487,8 @@ fn draw_table<B: Backend>(
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() {
Expand Down Expand Up @@ -556,20 +560,8 @@ fn draw_table<B: Backend>(
c.format().as_ref().map(|f| {
let out: Result<String> = 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()),
}
})
Expand Down

0 comments on commit b4247a7

Please sign in to comment.