Add permissions in Resolved Node (#592)

* Add permissions in Relolved Node

And handle application/x-executable mime type.

* Fix bench
pull/593/head
Arijit Basu 1 year ago committed by GitHub
parent 670f6596f1
commit 2ed800d552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,8 +18,9 @@ fn navigation_benchmark(c: &mut Criterion) {
});
let lua = mlua::Lua::new();
let mut app = app::App::create(PWD.into(), &lua, None, [].into())
.expect("failed to create app");
let mut app =
app::App::create("xplr".into(), None, PWD.into(), &lua, None, [].into())
.expect("failed to create app");
app = app
.clone()
@ -97,8 +98,9 @@ fn draw_benchmark(c: &mut Criterion) {
});
let lua = mlua::Lua::new();
let mut app = app::App::create(PWD.into(), &lua, None, [].into())
.expect("failed to create app");
let mut app =
app::App::create("xplr".into(), None, PWD.into(), &lua, None, [].into())
.expect("failed to create app");
app = app
.clone()

@ -297,6 +297,7 @@ It contains the following fields.
- [is_readonly][11]
- [mime_essence][12]
- [size][13]
- [permissions][15]
- [human_size][14]
- [created][34]
- [last_modified][35]

@ -166,7 +166,7 @@ pub struct InputBuffer {
pub prompt: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct App {
pub bin: String,
pub version: String,

@ -785,10 +785,10 @@ pub fn layout_replace<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
/// Example:
///
/// ```lua
/// xplr.util.permissions_rwx({ user_read = true }))
/// xplr.util.permissions_rwx({ user_read = true })
/// -- "r--------"
///
/// xplr.util.permissions_rwx(app.focused_node.permission))
/// xplr.util.permissions_rwx(app.focused_node.permission)
/// -- "rwxrwsrwT"
/// ```
pub fn permissions_rwx<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
@ -808,10 +808,10 @@ pub fn permissions_rwx<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
/// Example:
///
/// ```lua
/// xplr.util.permissions_octal({ user_read = true }))
/// xplr.util.permissions_octal({ user_read = true })
/// -- { 0, 4, 0, 0 }
///
/// xplr.util.permissions_octal(app.focused_node.permission))
/// xplr.util.permissions_octal(app.focused_node.permission)
/// -- { 0, 7, 5, 4 }
/// ```
pub fn permissions_octal<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {

@ -10,9 +10,16 @@ fn to_human_size(size: u64) -> String {
format_size(size, DECIMAL)
}
fn mime_essence(path: &Path, is_dir: bool) -> String {
fn mime_essence(
path: &Path,
is_dir: bool,
extension: &str,
is_executable: bool,
) -> String {
if is_dir {
String::from("inode/directory")
} else if extension.is_empty() && is_executable {
String::from("application/x-executable")
} else {
mime_guess::from_path(path)
.first()
@ -30,6 +37,7 @@ pub struct ResolvedNode {
pub is_readonly: bool,
pub mime_essence: String,
pub size: u64,
pub permissions: Permissions,
pub human_size: String,
pub created: Option<u128>,
pub last_modified: Option<u128>,
@ -44,29 +52,43 @@ impl ResolvedNode {
.map(|e| e.to_string_lossy().to_string())
.unwrap_or_default();
let (is_dir, is_file, is_readonly, size, created, last_modified, uid, gid) =
path.metadata()
.map(|m| {
(
m.is_dir(),
m.is_file(),
m.permissions().readonly(),
m.len(),
m.created()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_nanos()),
m.modified()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_nanos()),
m.uid(),
m.gid(),
)
})
.unwrap_or((false, false, false, 0, None, None, 0, 0));
let mime_essence = mime_essence(&path, is_dir);
let (
is_dir,
is_file,
is_readonly,
size,
permissions,
created,
last_modified,
uid,
gid,
) = path
.metadata()
.map(|m| {
(
m.is_dir(),
m.is_file(),
m.permissions().readonly(),
m.len(),
Permissions::from(&m),
m.created()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_nanos()),
m.modified()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_nanos()),
m.uid(),
m.gid(),
)
})
.unwrap_or((false, false, false, 0, Default::default(), None, None, 0, 0));
let is_executable = permissions.user_execute
|| permissions.group_execute
|| permissions.other_execute;
let mime_essence = mime_essence(&path, is_dir, &extension, is_executable);
let human_size = to_human_size(size);
Self {
@ -77,6 +99,7 @@ impl ResolvedNode {
is_readonly,
mime_essence,
size,
permissions,
human_size,
created,
last_modified,
@ -177,7 +200,11 @@ impl Node {
)
});
let mime_essence = mime_essence(&path, is_dir);
let is_executable = permissions.user_execute
|| permissions.group_execute
|| permissions.other_execute;
let mime_essence = mime_essence(&path, is_dir, &extension, is_executable);
let human_size = to_human_size(size);
Self {

Loading…
Cancel
Save