Improve timestamp support

pull/477/head
Arijit Basu 2 years ago committed by Arijit Basu
parent 626a48cf88
commit 37e660e0a6

@ -33,11 +33,16 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";
pub const UNSUPPORTED_STR: &str = "???";
fn to_humansize(size: u64) -> String {
fn to_human_size(size: u64) -> String {
size.file_size(options::CONVENTIONAL)
.unwrap_or_else(|_| format!("{} B", size))
}
fn to_human_timestamp(t: Option<DateTime<Local>>) -> String {
t.map(|l: DateTime<Local>| l.format("%y-%m-%d %H:%M:%S").to_string())
.unwrap_or_default()
}
fn mime_essence(path: &Path, is_dir: bool) -> String {
if is_dir {
String::from("inode/directory")
@ -49,12 +54,6 @@ fn mime_essence(path: &Path, is_dir: bool) -> String {
}
}
fn to_human_modified(modified: Option<DateTime<Local>>) -> String {
modified.map_or("".to_string(), |l: DateTime<Local>| {
l.format("%b %d %R").to_string()
})
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipe {
pub path: String,
@ -116,8 +115,10 @@ pub struct ResolvedNode {
pub mime_essence: String,
pub size: u64,
pub human_size: String,
pub last_modified: Option<DateTime<Local>>,
pub human_modified: String,
pub created: Option<i64>,
pub human_created: String,
pub last_modified: Option<i64>,
pub human_last_modified: String,
}
impl ResolvedNode {
@ -127,7 +128,7 @@ impl ResolvedNode {
.map(|e| e.to_string_lossy().to_string())
.unwrap_or_default();
let (is_dir, is_file, is_readonly, size, last_modified) = path
let (is_dir, is_file, is_readonly, size, created, last_modified) = path
.metadata()
.map(|m| {
(
@ -135,16 +136,16 @@ impl ResolvedNode {
m.is_file(),
m.permissions().readonly(),
m.len(),
m.modified()
.map(|md| Some(md.into()))
.unwrap_or_else(|_| None),
m.created().ok().map(From::from),
m.modified().ok().map(From::from),
)
})
.unwrap_or((false, false, false, 0, None));
.unwrap_or((false, false, false, 0, None, None));
let human_modified = to_human_modified(last_modified);
let human_last_modified = to_human_timestamp(last_modified);
let human_created = to_human_timestamp(created);
let mime_essence = mime_essence(&path, is_dir);
let human_size = to_humansize(size);
let human_size = to_human_size(size);
Self {
absolute_path: path.to_string_lossy().to_string(),
@ -155,8 +156,10 @@ impl ResolvedNode {
mime_essence,
size,
human_size,
last_modified,
human_modified,
created: created.map(|t| t.timestamp()),
human_created,
last_modified: last_modified.map(|t| t.timestamp()),
human_last_modified,
}
}
}
@ -178,8 +181,10 @@ pub struct Node {
pub permissions: Permissions,
pub canonical: Option<ResolvedNode>,
pub symlink: Option<ResolvedNode>,
pub last_modified: Option<DateTime<Local>>,
pub human_modified: String,
pub created: Option<i64>,
pub human_created: String,
pub last_modified: Option<i64>,
pub human_last_modified: String,
}
impl Node {
@ -208,6 +213,7 @@ impl Node {
is_readonly,
size,
permissions,
created,
last_modified,
) = path
.symlink_metadata()
@ -219,18 +225,27 @@ impl Node {
m.permissions().readonly(),
m.len(),
Permissions::from(&m),
m.modified()
.map(|md| Some(md.into()))
.unwrap_or_else(|_| None),
m.created().ok().map(From::from),
m.modified().ok().map(From::from),
)
})
.unwrap_or_else(|_| {
(false, false, false, false, 0, Permissions::default(), None)
(
false,
false,
false,
false,
0,
Permissions::default(),
None,
None,
)
});
let human_modified = to_human_modified(last_modified);
let human_created = to_human_timestamp(created);
let human_last_modified = to_human_timestamp(last_modified);
let mime_essence = mime_essence(&path, is_dir);
let human_size = to_humansize(size);
let human_size = to_human_size(size);
Self {
parent,
@ -252,8 +267,10 @@ impl Node {
} else {
None
},
last_modified,
human_modified,
created: created.map(|t| t.timestamp()),
human_created,
last_modified: last_modified.map(|t| t.timestamp()),
human_last_modified,
}
}
}
@ -316,6 +333,8 @@ pub enum NodeSorter {
ByIsBroken,
ByIsReadonly,
ByMimeEssence,
ByCreated,
ByLastModified,
BySize,
ByCanonicalAbsolutePath,
@ -325,6 +344,8 @@ pub enum NodeSorter {
ByCanonicalIsFile,
ByCanonicalIsReadonly,
ByCanonicalMimeEssence,
ByCanonicalCreated,
ByCanonicalLastModified,
ByCanonicalSize,
BySymlinkAbsolutePath,
@ -334,6 +355,8 @@ pub enum NodeSorter {
BySymlinkIsFile,
BySymlinkIsReadonly,
BySymlinkMimeEssence,
BySymlinkCreated,
BySymlinkLastModified,
BySymlinkSize,
}
@ -378,6 +401,8 @@ impl NodeSorterApplicable {
NodeSorter::ByIsBroken => a.is_broken.cmp(&b.is_broken),
NodeSorter::ByIsReadonly => a.is_readonly.cmp(&b.is_readonly),
NodeSorter::ByMimeEssence => a.mime_essence.cmp(&b.mime_essence),
NodeSorter::ByCreated => a.created.cmp(&b.created),
NodeSorter::ByLastModified => a.last_modified.cmp(&b.last_modified),
NodeSorter::BySize => a.size.cmp(&b.size),
NodeSorter::ByCanonicalAbsolutePath => natord::compare(
@ -434,6 +459,18 @@ impl NodeSorterApplicable {
.map(|s| &s.mime_essence)
.cmp(&b.canonical.as_ref().map(|s| &s.mime_essence)),
NodeSorter::ByCanonicalCreated => a
.canonical
.as_ref()
.map(|s| &s.created)
.cmp(&b.canonical.as_ref().map(|s| &s.created)),
NodeSorter::ByCanonicalLastModified => a
.canonical
.as_ref()
.map(|s| &s.last_modified)
.cmp(&b.canonical.as_ref().map(|s| &s.last_modified)),
NodeSorter::ByCanonicalSize => a
.canonical
.as_ref()
@ -492,6 +529,18 @@ impl NodeSorterApplicable {
.map(|s| &s.mime_essence)
.cmp(&b.symlink.as_ref().map(|s| &s.mime_essence)),
NodeSorter::BySymlinkCreated => a
.symlink
.as_ref()
.map(|s| &s.created)
.cmp(&b.symlink.as_ref().map(|s| &s.created)),
NodeSorter::BySymlinkLastModified => a
.symlink
.as_ref()
.map(|s| &s.last_modified)
.cmp(&b.symlink.as_ref().map(|s| &s.last_modified)),
NodeSorter::BySymlinkSize => a
.symlink
.as_ref()

@ -208,6 +208,7 @@ xplr.config.general.table.row.cols = {
-- Type: [Style](https://xplr.dev/en/style)
xplr.config.general.table.row.style = {}
<<<<<<< HEAD
-- Height of the table rows.
--
-- Type: nullable integer
@ -240,11 +241,10 @@ xplr.config.general.table.col_spacing = 1
-- Type: nullable list of [Constraint](https://xplr.dev/en/layouts#constraint)
xplr.config.general.table.col_widths = {
{ Percentage = 10 },
{ Percentage = 30 },
{ Percentage = 40 },
{ Percentage = 20 },
{ Percentage = 10 },
{ Percentage = 10 },
{ Percentage = 15 },
{ Percentage = 15 },
{ Percentage = 20 },
}
-- The content that is placed before the item name for each row by default.
@ -2366,6 +2366,38 @@ xplr.config.modes.builtin.sort = {
"ExplorePwdAsync",
},
},
["c"] = {
help = "by created",
messages = {
{ AddNodeSorter = { sorter = "ByCreated", reverse = false } },
"ExplorePwdAsync",
},
},
["C"] = {
help = "by created reverse",
messages = {
{ AddNodeSorter = { sorter = "ByCreated", reverse = true } },
"ExplorePwdAsync",
},
},
["l"] = {
help = "by last modified",
messages = {
{ AddNodeSorter = { sorter = "ByLastModified", reverse = false } },
"ExplorePwdAsync",
},
},
["L"] = {
help = "by last modified reverse",
messages = {
{ AddNodeSorter = { sorter = "ByLastModified", reverse = true } },
"ExplorePwdAsync",
},
},
},
},
}

@ -6,7 +6,6 @@ use crate::lua;
use crate::permissions::Permissions;
use ansi_to_tui_forked::ansi_to_text;
use anyhow::Result;
use chrono::{DateTime, Local};
use indexmap::IndexSet;
use lazy_static::lazy_static;
use mlua::Lua;
@ -376,8 +375,10 @@ pub struct ResolvedNodeUiMetadata {
pub mime_essence: String,
pub size: u64,
pub human_size: String,
pub last_modified: Option<DateTime<Local>>,
pub human_modified: String,
pub created: Option<i64>,
pub human_created: String,
pub last_modified: Option<i64>,
pub human_last_modified: String,
}
impl From<ResolvedNode> for ResolvedNodeUiMetadata {
@ -391,8 +392,10 @@ impl From<ResolvedNode> for ResolvedNodeUiMetadata {
mime_essence: node.mime_essence.to_owned(),
size: node.size,
human_size: node.human_size,
created: node.created,
human_created: node.human_created,
last_modified: node.last_modified.to_owned(),
human_modified: node.human_modified,
human_last_modified: node.human_last_modified,
}
}
}
@ -415,8 +418,10 @@ pub struct NodeUiMetadata {
pub permissions: Permissions,
pub canonical: Option<ResolvedNodeUiMetadata>,
pub symlink: Option<ResolvedNodeUiMetadata>,
pub last_modified: Option<DateTime<Local>>,
pub human_modified: String,
pub created: Option<i64>,
pub human_created: String,
pub last_modified: Option<i64>,
pub human_last_modified: String,
// Extra
pub index: usize,
@ -463,8 +468,10 @@ impl NodeUiMetadata {
permissions: node.permissions.to_owned(),
canonical: node.canonical.to_owned().map(ResolvedNode::into),
symlink: node.symlink.to_owned().map(ResolvedNode::into),
last_modified: node.last_modified.to_owned(),
human_modified: node.human_modified.to_owned(),
created: node.created,
human_created: node.human_created.to_owned(),
last_modified: node.last_modified,
human_last_modified: node.human_last_modified.to_owned(),
index,
relative_index,
is_before_focus,

Loading…
Cancel
Save