Skip to content

Commit

Permalink
Add the ability to sort by file name and path
Browse files Browse the repository at this point in the history
  • Loading branch information
Aszarsha authored and baskerville committed Mar 3, 2020
1 parent 29c2c9d commit 53dcb4a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ pub fn asciify(name: &str) -> String {
.replace('’', "'")
}


pub fn open<P: AsRef<Path>>(path: P) -> Option<Box<dyn Document>> {
file_kind(path.as_ref()).and_then(|k| {
match k.as_ref() {
Expand Down
21 changes: 19 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,18 @@ pub enum SortMethod {
Size,
Kind,
Pages,
FileName,
FilePath,
}

impl SortMethod {
pub fn reverse_order(self) -> bool {
match self {
SortMethod::Author | SortMethod::Title | SortMethod::Kind => false,
SortMethod::Author |
SortMethod::Title |
SortMethod::Kind |
SortMethod::FileName |
SortMethod::FilePath => false,
_ => true,
}
}
Expand All @@ -473,6 +479,8 @@ impl SortMethod {
SortMethod::Size => "File Size",
SortMethod::Kind => "File Type",
SortMethod::Pages => "Pages",
SortMethod::FileName => "File Name",
SortMethod::FilePath => "File Path",
}
}

Expand All @@ -492,6 +500,8 @@ pub fn sort(md: &mut Metadata, sort_method: SortMethod, reverse_order: bool) {
SortMethod::Size => sort_size,
SortMethod::Kind => sort_kind,
SortMethod::Pages => sort_pages,
SortMethod::FileName => sort_filename,
SortMethod::FilePath => sort_filepath,
};
if reverse_order {
md.sort_by(|a, b| sort_fn(a, b).reverse());
Expand All @@ -509,7 +519,6 @@ pub fn sort_opened(i1: &Info, i2: &Info) -> Ordering {
}
}


pub fn sort_pages(i1: &Info, i2: &Info) -> Ordering {
match (&i1.reader, &i2.reader) {
(&None, &None) => Ordering::Equal,
Expand Down Expand Up @@ -560,6 +569,14 @@ pub fn sort_year(i1: &Info, i2: &Info) -> Ordering {
i1.year.cmp(&i2.year)
}

pub fn sort_filename(i1: &Info, i2: &Info) -> Ordering {
i1.file.path.file_name().cmp(&i2.file.path.file_name())
}

pub fn sort_filepath(i1: &Info, i2: &Info) -> Ordering {
i1.file.path.cmp(&i2.file.path)
}

lazy_static! {
pub static ref TITLE_PREFIXES: FnvHashMap<&'static str, Regex> = {
let mut p = FnvHashMap::default();
Expand Down
6 changes: 6 additions & 0 deletions src/view/home/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@ impl Home {
EntryKind::RadioButton("File Type".to_string(),
EntryId::Sort(SortMethod::Kind),
self.sort_method == SortMethod::Kind),
EntryKind::RadioButton("File Name".to_string(),
EntryId::Sort(SortMethod::FileName),
self.sort_method == SortMethod::FileName),
EntryKind::RadioButton("File Path".to_string(),
EntryId::Sort(SortMethod::FilePath),
self.sort_method == SortMethod::FilePath),
EntryKind::Separator,
EntryKind::CheckBox("Reverse Order".to_string(),
EntryId::ReverseOrder, self.reverse_order)];
Expand Down

0 comments on commit 53dcb4a

Please sign in to comment.