From ec33085f672e52adb934503d78703b8dd70621a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sun, 14 Jul 2024 15:02:07 +0800 Subject: [PATCH] feat: add `ctime` linemode (#1295) --- yazi-config/preset/keymap.toml | 35 +++++++++++----------- yazi-config/src/preview/preview.rs | 6 ++-- yazi-plugin/preset/components/linemode.lua | 21 +++++++++++-- yazi-plugin/preset/plugins/zoxide.lua | 2 +- yazi-shared/src/time.rs | 5 ++-- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index 5139472d6..31d09d5df 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -63,14 +63,14 @@ keymap = [ { on = "O", run = "open --interactive", desc = "Open selected files interactively" }, { on = "", run = "open", desc = "Open selected files" }, { on = "", run = "open --interactive", desc = "Open selected files interactively" }, - { on = "y", run = "yank", desc = "Copy selected files" }, - { on = "x", run = "yank --cut", desc = "Cut the selected files" }, - { on = "Y", run = "unyank", desc = "Cancel the yank status" }, - { on = "X", run = "unyank", desc = "Cancel the yank status" }, + { on = "y", run = "yank", desc = "Yank selected files (copy)" }, + { on = "x", run = "yank --cut", desc = "Yank selected files (cut)" }, { on = "p", run = "paste", desc = "Paste yanked files" }, { on = "P", run = "paste --force", desc = "Paste yanked files (overwrite if the destination exists)" }, { on = "-", run = "link", desc = "Symlink the absolute path of yanked files" }, { on = "_", run = "link --relative", desc = "Symlink the relative path of yanked files" }, + { on = "Y", run = "unyank", desc = "Cancel the yank status" }, + { on = "X", run = "unyank", desc = "Cancel the yank status" }, { on = "d", run = "remove", desc = "Trash selected files" }, { on = "D", run = "remove --permanently", desc = "Permanently delete selected files" }, { on = "a", run = "create", desc = "Create a file (ends with / for directories)" }, @@ -87,6 +87,7 @@ keymap = [ # Linemode { on = [ "m", "s" ], run = "linemode size", desc = "Set linemode to size" }, { on = [ "m", "p" ], run = "linemode permissions", desc = "Set linemode to permissions" }, + { on = [ "m", "c" ], run = "linemode ctime", desc = "Set linemode to ctime" }, { on = [ "m", "m" ], run = "linemode mtime", desc = "Set linemode to mtime" }, { on = [ "m", "o" ], run = "linemode owner", desc = "Set linemode to owner" }, { on = [ "m", "n" ], run = "linemode none", desc = "Set linemode to none" }, @@ -107,19 +108,19 @@ keymap = [ { on = "N", run = "find_arrow --previous", desc = "Go to the previous found" }, # Sorting - { on = [ ",", "m" ], run = "sort modified --reverse=no", desc = "Sort by modified time" }, - { on = [ ",", "M" ], run = "sort modified --reverse", desc = "Sort by modified time (reverse)" }, - { on = [ ",", "c" ], run = "sort created --reverse=no", desc = "Sort by created time" }, - { on = [ ",", "C" ], run = "sort created --reverse", desc = "Sort by created time (reverse)" }, - { on = [ ",", "e" ], run = "sort extension --reverse=no", desc = "Sort by extension" }, - { on = [ ",", "E" ], run = "sort extension --reverse", desc = "Sort by extension (reverse)" }, - { on = [ ",", "a" ], run = "sort alphabetical --reverse=no", desc = "Sort alphabetically" }, - { on = [ ",", "A" ], run = "sort alphabetical --reverse", desc = "Sort alphabetically (reverse)" }, - { on = [ ",", "n" ], run = "sort natural --reverse=no", desc = "Sort naturally" }, - { on = [ ",", "N" ], run = "sort natural --reverse", desc = "Sort naturally (reverse)" }, - { on = [ ",", "s" ], run = "sort size --reverse=no", desc = "Sort by size" }, - { on = [ ",", "S" ], run = "sort size --reverse", desc = "Sort by size (reverse)" }, - { on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" }, + { on = [ ",", "m" ], run = [ "sort modified --reverse=no", "linemode mtime" ], desc = "Sort by modified time" }, + { on = [ ",", "M" ], run = [ "sort modified --reverse", "linemode mtime" ], desc = "Sort by modified time (reverse)" }, + { on = [ ",", "c" ], run = [ "sort created --reverse=no", "linemode ctime" ], desc = "Sort by created time" }, + { on = [ ",", "C" ], run = [ "sort created --reverse", "linemode ctime" ], desc = "Sort by created time (reverse)" }, + { on = [ ",", "e" ], run = "sort extension --reverse=no", desc = "Sort by extension" }, + { on = [ ",", "E" ], run = "sort extension --reverse", desc = "Sort by extension (reverse)" }, + { on = [ ",", "a" ], run = "sort alphabetical --reverse=no", desc = "Sort alphabetically" }, + { on = [ ",", "A" ], run = "sort alphabetical --reverse", desc = "Sort alphabetically (reverse)" }, + { on = [ ",", "n" ], run = "sort natural --reverse=no", desc = "Sort naturally" }, + { on = [ ",", "N" ], run = "sort natural --reverse", desc = "Sort naturally (reverse)" }, + { on = [ ",", "s" ], run = [ "sort size --reverse=no", "linemode size" ], desc = "Sort by size" }, + { on = [ ",", "S" ], run = [ "sort size --reverse", "linemode size" ], desc = "Sort by size (reverse)" }, + { on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" }, # Tabs { on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" }, diff --git a/yazi-config/src/preview/preview.rs b/yazi-config/src/preview/preview.rs index 5f38a109c..866b3f32a 100644 --- a/yazi-config/src/preview/preview.rs +++ b/yazi-config/src/preview/preview.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, str::FromStr, time::{self, SystemTime}}; +use std::{path::PathBuf, str::FromStr, time::{SystemTime, UNIX_EPOCH}}; use anyhow::Context; use serde::{Deserialize, Serialize}; @@ -26,8 +26,8 @@ pub struct Preview { impl Preview { #[inline] pub fn tmpfile(&self, prefix: &str) -> PathBuf { - let nanos = SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap().as_nanos(); - self.cache_dir.join(format!("{prefix}-{}", nanos / 1000)) + let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards"); + self.cache_dir.join(format!("{prefix}-{}", time.as_nanos() / 1000)) } } diff --git a/yazi-plugin/preset/components/linemode.lua b/yazi-plugin/preset/components/linemode.lua index 4c61af373..3e4ccc6a4 100644 --- a/yazi-plugin/preset/components/linemode.lua +++ b/yazi-plugin/preset/components/linemode.lua @@ -24,9 +24,26 @@ function Linemode:size(file) return ui.Line(size and ya.readable_size(size) or "") end +function Linemode:ctime(file) + local time = (file.cha.created or 0) // 1 + if time == 0 then + return ui.Line("") + elseif os.date("%Y", time) == os.date("%Y") then + return ui.Line(os.date("%m/%d %H:%M", time)) + else + return ui.Line(os.date("%m/%d %Y", time)) + end +end + function Linemode:mtime(file) - local time = file.cha.modified - return ui.Line(time and os.date("%y-%m-%d %H:%M", time // 1) or "") + local time = (file.cha.modified or 0) // 1 + if time == 0 then + return ui.Line("") + elseif os.date("%Y", time) == os.date("%Y") then + return ui.Line(os.date("%m/%d %H:%M", time)) + else + return ui.Line(os.date("%m/%d %Y", time)) + end end function Linemode:permissions(file) return ui.Line(file.cha:permissions() or "") end diff --git a/yazi-plugin/preset/plugins/zoxide.lua b/yazi-plugin/preset/plugins/zoxide.lua index a7b89aebd..83ee601ba 100644 --- a/yazi-plugin/preset/plugins/zoxide.lua +++ b/yazi-plugin/preset/plugins/zoxide.lua @@ -81,7 +81,7 @@ local function entry() local target = output.stdout:gsub("\n$", "") if target ~= "" then - ya.manager_emit("cd", { output.stdout:gsub("\n$", "") }) + ya.manager_emit("cd", { target }) end end diff --git a/yazi-shared/src/time.rs b/yazi-shared/src/time.rs index 6a75756a9..8d7b1f38d 100644 --- a/yazi-shared/src/time.rs +++ b/yazi-shared/src/time.rs @@ -1,5 +1,6 @@ -use std::time::{self, SystemTime}; +use std::time::{SystemTime, UNIX_EPOCH}; +#[inline] pub fn timestamp_us() -> u64 { - SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap().as_micros() as u64 + SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_micros() as _ }