summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2024-01-24 19:44:25 +0100
committerqkzk <qu3nt1n@gmail.com>2024-01-24 19:44:25 +0100
commit04817c389599af1717300ac013bbf7b35f799432 (patch)
tree2630f6c654515fea93ef231d5691cf9b104f41da
parent64e186cd24bc298dacf133720aad292642c7fa29 (diff)
sort trash by reversed deletion date
-rw-r--r--development.md2
-rw-r--r--src/modes/edit/trash.rs19
2 files changed, 18 insertions, 3 deletions
diff --git a/development.md b/development.md
index f95ef05..7e1e9f9 100644
--- a/development.md
+++ b/development.md
@@ -916,7 +916,7 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally.
- [x] allow header & footer to be right aligned
- [x] merge both bulkthing modes. If more files, just create them. Like [oil](https://github.com/stevearc/oil.nvim)
- [ ] allow different ports in remote
-- [ ] sort trash by reversed date
+- [x] sort trash by reversed deletion date
- [ ] ???
## TODO
diff --git a/src/modes/edit/trash.rs b/src/modes/edit/trash.rs
index 41d7133..2ef9ec0 100644
--- a/src/modes/edit/trash.rs
+++ b/src/modes/edit/trash.rs
@@ -1,3 +1,4 @@
+use std::cmp::Ordering;
use std::fs::{create_dir, read_dir, remove_dir_all};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
@@ -24,7 +25,7 @@ const TRASHINFO_DATETIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S";
/// - where the file came from,
/// - what name it was given when trashed,
/// - when it was trashed
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Info {
origin: PathBuf,
dest_name: String,
@@ -174,6 +175,19 @@ DeletionDate={date}
}
}
+impl Ord for Info {
+ /// Reversed to ensure most recent trashed files are displayed at top
+ fn cmp(&self, other: &Self) -> Ordering {
+ self.deletion_date.cmp(&other.deletion_date).reverse()
+ }
+}
+
+impl PartialOrd for Info {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
impl std::fmt::Display for Info {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
@@ -254,7 +268,7 @@ impl Trash {
fn parse_updated_content(trash_folder_info: &str) -> Result<Vec<Info>> {
match read_dir(trash_folder_info) {
Ok(read_dir) => {
- let content: Vec<Info> = read_dir
+ let mut content: Vec<Info> = read_dir
.filter_map(std::result::Result::ok)
.filter(|direntry| direntry.path().extension().is_some())
.filter(|direntry| {
@@ -264,6 +278,7 @@ impl Trash {
.filter_map(std::result::Result::ok)
.collect();
+ content.sort_unstable();
Ok(content)
}
Err(error) => {