Skip to content

Commit b4247a7

Browse files
committed
Improve CallLua, mime_essence, permissions
Refs: - #187 - #194 - #195
1 parent 9b02ef3 commit b4247a7

File tree

8 files changed

+75
-47
lines changed

8 files changed

+75
-47
lines changed

Diff for: Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "xplr"
3-
version = "0.11.1" # Update lua.rs
3+
version = "0.12.0" # Update lua.rs
44
authors = ["Arijit Basu <[email protected]>"]
55
edition = "2018"
66
description = "A hackable, minimal, fast TUI file explorer"
@@ -29,7 +29,7 @@ indexmap = { version = "1.6.2", features = ["serde"] }
2929
natord = "1.0.9"
3030
humansize = "1.1.0"
3131
mlua = { version = "0.5.4", features = ["luajit", "vendored", "serialize", "send"] }
32-
ansi-to-tui = "0.1.9"
32+
ansi-to-tui = "0.2.0"
3333
libc = "0.2.94"
3434

3535
[dev-dependencies]

Diff for: src/app.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1139,15 +1139,19 @@ pub enum ExternalMsg {
11391139
/// **Example:** `CallSilently: {command: tput, args: ["bell"]}`
11401140
CallSilently(Command),
11411141

1142-
/// Call a Lua function
1142+
/// Call a Lua function.
1143+
/// The complete app state (exportable using `PrintAppStateAndQuit` or `Debug`)
1144+
/// will be passed to the function as argument.
1145+
/// The function can optionally return a list of messages for xplr to handle
1146+
/// after the executing the function.
11431147
///
1144-
/// **Example:** `CallLua: custom.foo_funtion`
1148+
/// **Example:** `CallLua: custom.some_custom_funtion`
11451149
CallLua(String),
11461150

11471151
/// Like `CallLua` but without the flicker. The stdin, stdout
11481152
/// stderr will be piped to null. So it's non-interactive.
11491153
///
1150-
/// **Example:** `CallLuaSilently: custom.bar_function`
1154+
/// **Example:** `CallLuaSilently: custom.some_custom_function`
11511155
CallLuaSilently(String),
11521156

11531157
/// An alias to `Call: {command: bash, args: ["-c", "${command}"], silent: false}`

Diff for: src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct NodeTypesConfig {
8484
pub symlink: NodeTypeConfig,
8585

8686
#[serde(default)]
87-
pub mime_essence: HashMap<String, NodeTypeConfig>,
87+
pub mime_essence: HashMap<String, HashMap<String, NodeTypeConfig>>,
8888

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

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

Diff for: src/init.lua

+42-11
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ xplr.config.general.table.col_widths = {
279279
xplr.config.general.table.header.cols = {
280280
{ format = " index", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
281281
{ format = "╭──── path", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
282-
{ format = "permissions", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
282+
{ format = " permissions", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
283283
{ format = "size", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
284284
{ format = "type", style = { add_modifiers = nil, bg = nil, fg = nil, sub_modifiers = nil } },
285285
}
@@ -2083,21 +2083,52 @@ xplr.fn.builtin.fmt_general_table_row_cols_2 = function(m)
20832083
end
20842084
end
20852085

2086-
local r = ""
2086+
local p = m.permissions
2087+
2088+
-- TODO: Track https://github.com/uttarayan21/ansi-to-tui/issues/3
2089+
local r = " "
2090+
20872091
-- User
2088-
r = r .. bit("r", green, m.permissions.user_read)
2089-
r = r .. bit("w", yellow, m.permissions.user_write)
2090-
r = r .. bit("x", red, m.permissions.user_execute)
2092+
r = r .. bit("r", green, p.user_read)
2093+
r = r .. bit("w", yellow, p.user_write)
2094+
2095+
if p.user_execute == false and p.setuid == false then
2096+
r = r .. bit("-", red, p.user_execute)
2097+
elseif p.user_execute == true and p.setuid == false then
2098+
r = r .. bit("x", red, p.user_execute)
2099+
elseif p.user_execute == false and p.setuid == true then
2100+
r = r .. bit("S", red, p.user_execute)
2101+
else
2102+
r = r .. bit("s", red, p.user_execute)
2103+
end
20912104

20922105
-- Group
2093-
r = r .. bit("r", green, m.permissions.group_read)
2094-
r = r .. bit("w", yellow, m.permissions.group_write)
2095-
r = r .. bit("x", red, m.permissions.group_execute)
2106+
r = r .. bit("r", green, p.group_read)
2107+
r = r .. bit("w", yellow, p.group_write)
2108+
2109+
if p.group_execute == false and p.setuid == false then
2110+
r = r .. bit("-", red, p.group_execute)
2111+
elseif p.group_execute == true and p.setuid == false then
2112+
r = r .. bit("x", red, p.group_execute)
2113+
elseif p.group_execute == false and p.setuid == true then
2114+
r = r .. bit("S", red, p.group_execute)
2115+
else
2116+
r = r .. bit("s", red, p.group_execute)
2117+
end
20962118

20972119
-- Other
2098-
r = r .. bit("r", green, m.permissions.other_read)
2099-
r = r .. bit("w", yellow, m.permissions.other_write)
2100-
r = r .. bit("x", red, m.permissions.other_execute)
2120+
r = r .. bit("r", green, p.other_read)
2121+
r = r .. bit("w", yellow, p.other_write)
2122+
2123+
if p.other_execute == false and p.setuid == false then
2124+
r = r .. bit("-", red, p.other_execute)
2125+
elseif p.other_execute == true and p.setuid == false then
2126+
r = r .. bit("x", red, p.other_execute)
2127+
elseif p.other_execute == false and p.setuid == true then
2128+
r = r .. bit("S", red, p.other_execute)
2129+
else
2130+
r = r .. bit("s", red, p.other_execute)
2131+
end
21012132

21022133
return r
21032134
end

Diff for: src/lua.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ pub fn call<'lua, A: Serialize, R: Deserialize<'lua>>(
120120
) -> Result<R> {
121121
let func = resolve_fn(&lua.globals(), func)?;
122122
let args = lua.to_value(args)?;
123-
let msgs: mlua::Value = func.call(args)?;
124-
let msgs: R = lua.from_value(msgs)?;
125-
Ok(msgs)
123+
let res: mlua::Value = func.call(args)?;
124+
let res: R = lua.from_value(res)?;
125+
Ok(res)
126126
}
127127

128128
#[cfg(test)]
@@ -133,11 +133,10 @@ mod test {
133133
#[test]
134134
fn test_compatibility() {
135135
assert!(check_version(VERSION, "foo path").is_ok());
136-
assert!(check_version("0.11.0", "foo path").is_ok());
137-
assert!(check_version("0.11.1", "foo path").is_ok());
136+
assert!(check_version("0.12.0", "foo path").is_ok());
138137

139-
assert!(check_version("0.11.2", "foo path").is_err());
140-
assert!(check_version("0.10.1", "foo path").is_err());
141-
assert!(check_version("1.12.1", "foo path").is_err());
138+
assert!(check_version("0.12.1", "foo path").is_err());
139+
assert!(check_version("0.10.0", "foo path").is_err());
140+
assert!(check_version("1.12.0", "foo path").is_err());
142141
}
143142
}

Diff for: src/runner.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn call_lua(
2626
lua: &mlua::Lua,
2727
func: &str,
2828
silent: bool,
29-
) -> Result<Vec<app::ExternalMsg>> {
29+
) -> Result<Option<Vec<app::ExternalMsg>>> {
3030
let _focus_index = app
3131
.directory_buffer()
3232
.map(|d| d.focus())
@@ -195,14 +195,15 @@ pub fn run(
195195
tx_event_reader.send(true)?;
196196

197197
match call_lua(&app, &lua, &func, false) {
198-
Ok(msgs) => {
198+
Ok(Some(msgs)) => {
199199
for msg in msgs {
200200
app = app.handle_task(app::Task::new(
201201
app::MsgIn::External(msg),
202202
None,
203203
))?;
204204
}
205205
}
206+
Ok(None) => {}
206207
Err(err) => {
207208
app = app.log_error(err.to_string())?;
208209
}
@@ -260,14 +261,15 @@ pub fn run(
260261
terminal.show_cursor()?;
261262

262263
match call_lua(&app, &lua, &func, false) {
263-
Ok(msgs) => {
264+
Ok(Some(msgs)) => {
264265
for msg in msgs {
265266
app = app.handle_task(app::Task::new(
266267
app::MsgIn::External(msg),
267268
None,
268269
))?;
269270
}
270271
}
272+
Ok(None) => {}
271273
Err(err) => {
272274
app = app.log_error(err.to_string())?;
273275
}

Diff for: src/ui.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ fn draw_table<B: Backend>(
475475
})
476476
.unwrap_or_default();
477477

478+
let (mimetype, mimesub) =
479+
node.mime_essence().split_once("/").unwrap_or_default();
480+
478481
let node_type = app_config
479482
.node_types()
480483
.special()
@@ -484,7 +487,8 @@ fn draw_table<B: Backend>(
484487
app_config
485488
.node_types()
486489
.mime_essence()
487-
.get(node.mime_essence())
490+
.get(mimetype)
491+
.and_then(|t| t.get(mimesub).or_else(|| t.get("*")))
488492
})
489493
.unwrap_or_else(|| {
490494
if node.is_symlink() {
@@ -556,20 +560,8 @@ fn draw_table<B: Backend>(
556560
c.format().as_ref().map(|f| {
557561
let out: Result<String> = lua::call(lua, f, &v);
558562
match out {
559-
Ok(o) => {
560-
let text =
561-
ansi_to_text(o.bytes()).unwrap_or_else(|e| {
562-
Text::raw(format!("{:?}", e))
563-
});
564-
565-
// TODO: Track https://github.com/uttarayan21/ansi-to-tui/issues/2
566-
// And https://github.com/fdehau/tui-rs/issues/304
567-
if text.lines.is_empty() {
568-
Text::raw(o.to_string())
569-
} else {
570-
text
571-
}
572-
}
563+
Ok(o) => ansi_to_text(o.bytes())
564+
.unwrap_or_else(|e| Text::raw(format!("{:?}", e))),
573565
Err(e) => Text::raw(e.to_string()),
574566
}
575567
})

0 commit comments

Comments
 (0)