refactor sorting
This commit is contained in:
parent
d47002bb0d
commit
197398fe4e
1 changed files with 82 additions and 73 deletions
155
src/tab.rs
155
src/tab.rs
|
|
@ -542,15 +542,15 @@ pub struct Tab {
|
||||||
pub history_i: usize,
|
pub history_i: usize,
|
||||||
pub history: Vec<Location>,
|
pub history: Vec<Location>,
|
||||||
pub config: TabConfig,
|
pub config: TabConfig,
|
||||||
//TODO: this no longer needs to be a map
|
sort_name: HeadingOptions,
|
||||||
sort_map: HashMap<HeadingOptions, bool>,
|
sort_direction: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tab {
|
impl Tab {
|
||||||
pub fn new(location: Location, config: TabConfig) -> Self {
|
pub fn new(location: Location, config: TabConfig) -> Self {
|
||||||
let history = vec![location.clone()];
|
let history = vec![location.clone()];
|
||||||
let mut sort_map = HashMap::new();
|
let sort_name = HeadingOptions::Name;
|
||||||
sort_map.insert(HeadingOptions::Name, true);
|
let sort_direction = true;
|
||||||
Self {
|
Self {
|
||||||
location,
|
location,
|
||||||
context_menu: None,
|
context_menu: None,
|
||||||
|
|
@ -564,7 +564,8 @@ impl Tab {
|
||||||
history_i: 0,
|
history_i: 0,
|
||||||
history,
|
history,
|
||||||
config,
|
config,
|
||||||
sort_map,
|
sort_name,
|
||||||
|
sort_direction,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -840,69 +841,13 @@ impl Tab {
|
||||||
self.view = view;
|
self.view = view;
|
||||||
}
|
}
|
||||||
Message::ToggleSort(heading_option) => {
|
Message::ToggleSort(heading_option) => {
|
||||||
let heading_sort = !self.sort_map.get(&heading_option).unwrap_or(&false);
|
let heading_sort = if self.sort_name == heading_option {
|
||||||
self.sort_map.clear();
|
!self.sort_direction
|
||||||
self.sort_map.insert(heading_option, heading_sort);
|
} else {
|
||||||
let check_reverse = |ord: Ordering, sort: bool| {
|
true
|
||||||
if sort {
|
|
||||||
ord
|
|
||||||
} else {
|
|
||||||
ord.reverse()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
if let Some(item) = &mut self.items_opt {
|
self.sort_direction = heading_sort;
|
||||||
match heading_option {
|
self.sort_name = heading_option;
|
||||||
HeadingOptions::Size => {
|
|
||||||
item.sort_by(|a, b| {
|
|
||||||
// entries take precedence over size
|
|
||||||
let get_size = |x: &Item| match &x.metadata {
|
|
||||||
ItemMetadata::Path { metadata, children } => {
|
|
||||||
if metadata.is_dir() {
|
|
||||||
(true, *children as u64)
|
|
||||||
} else {
|
|
||||||
(false, metadata.len())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ItemMetadata::Trash { metadata, .. } => match metadata.size {
|
|
||||||
trash::TrashItemSize::Entries(entries) => {
|
|
||||||
(true, entries as u64)
|
|
||||||
}
|
|
||||||
trash::TrashItemSize::Bytes(bytes) => (false, bytes),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let (a_is_entry, a_size) = get_size(a);
|
|
||||||
let (b_is_entry, b_size) = get_size(b);
|
|
||||||
|
|
||||||
let ord = match (a_is_entry, b_is_entry) {
|
|
||||||
(true, false) => Ordering::Less,
|
|
||||||
(false, true) => Ordering::Greater,
|
|
||||||
_ => a_size.cmp(&b_size),
|
|
||||||
};
|
|
||||||
check_reverse(ord, heading_sort)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
HeadingOptions::Name => item.sort_by(|a, b| {
|
|
||||||
let ord = match (a.path.is_dir(), b.path.is_dir()) {
|
|
||||||
(true, false) => Ordering::Less,
|
|
||||||
(false, true) => Ordering::Greater,
|
|
||||||
_ => lexical_sort::natural_lexical_cmp(&a.name, &b.name),
|
|
||||||
};
|
|
||||||
check_reverse(ord, heading_sort)
|
|
||||||
}),
|
|
||||||
HeadingOptions::Modified => {
|
|
||||||
item.sort_by(|a, b| {
|
|
||||||
let get_modified = |x: &Item| match &x.metadata {
|
|
||||||
ItemMetadata::Path { metadata, .. } => metadata.modified().ok(),
|
|
||||||
ItemMetadata::Trash { .. } => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let a = get_modified(a);
|
|
||||||
let b = get_modified(b);
|
|
||||||
check_reverse(a.cmp(&b), heading_sort)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(location) = cd {
|
if let Some(location) = cd {
|
||||||
|
|
@ -927,6 +872,68 @@ impl Tab {
|
||||||
commands
|
commands
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn column_sort(&self) -> Option<Vec<Item>> {
|
||||||
|
let check_reverse = |ord: Ordering, sort: bool| {
|
||||||
|
if sort {
|
||||||
|
ord
|
||||||
|
} else {
|
||||||
|
ord.reverse()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut item = self.items_opt.clone()?;
|
||||||
|
let heading_sort = self.sort_direction;
|
||||||
|
match self.sort_name {
|
||||||
|
HeadingOptions::Size => {
|
||||||
|
item.sort_by(|a, b| {
|
||||||
|
// entries take precedence over size
|
||||||
|
let get_size = |x: &Item| match &x.metadata {
|
||||||
|
ItemMetadata::Path { metadata, children } => {
|
||||||
|
if metadata.is_dir() {
|
||||||
|
(true, *children as u64)
|
||||||
|
} else {
|
||||||
|
(false, metadata.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ItemMetadata::Trash { metadata, .. } => match metadata.size {
|
||||||
|
trash::TrashItemSize::Entries(entries) => (true, entries as u64),
|
||||||
|
trash::TrashItemSize::Bytes(bytes) => (false, bytes),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let (a_is_entry, a_size) = get_size(a);
|
||||||
|
let (b_is_entry, b_size) = get_size(b);
|
||||||
|
|
||||||
|
let ord = match (a_is_entry, b_is_entry) {
|
||||||
|
(true, false) => Ordering::Less,
|
||||||
|
(false, true) => Ordering::Greater,
|
||||||
|
_ => a_size.cmp(&b_size),
|
||||||
|
};
|
||||||
|
check_reverse(ord, heading_sort)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
HeadingOptions::Name => item.sort_by(|a, b| {
|
||||||
|
let ord = match (a.path.is_dir(), b.path.is_dir()) {
|
||||||
|
(true, false) => Ordering::Less,
|
||||||
|
(false, true) => Ordering::Greater,
|
||||||
|
_ => lexical_sort::natural_lexical_cmp(&a.name, &b.name),
|
||||||
|
};
|
||||||
|
check_reverse(ord, heading_sort)
|
||||||
|
}),
|
||||||
|
HeadingOptions::Modified => {
|
||||||
|
item.sort_by(|a, b| {
|
||||||
|
let get_modified = |x: &Item| match &x.metadata {
|
||||||
|
ItemMetadata::Path { metadata, .. } => metadata.modified().ok(),
|
||||||
|
ItemMetadata::Trash { .. } => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let a = get_modified(a);
|
||||||
|
let b = get_modified(b);
|
||||||
|
check_reverse(a.cmp(&b), heading_sort)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(item)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn location_view(&self, core: &Core) -> Element<Message> {
|
pub fn location_view(&self, core: &Core) -> Element<Message> {
|
||||||
let cosmic_theme::Spacing {
|
let cosmic_theme::Spacing {
|
||||||
space_xxxs,
|
space_xxxs,
|
||||||
|
|
@ -1168,11 +1175,13 @@ impl Tab {
|
||||||
($name: literal, $width: expr, $msg: expr) => {
|
($name: literal, $width: expr, $msg: expr) => {
|
||||||
widget::row::with_children(vec![
|
widget::row::with_children(vec![
|
||||||
widget::text::heading(fl!($name)).into(),
|
widget::text::heading(fl!($name)).into(),
|
||||||
widget::button(widget::icon::from_name(match self.sort_map.get(&$msg) {
|
widget::button(widget::icon::from_name(
|
||||||
Some(true) => "go-down-symbolic",
|
match (self.sort_name == $msg, self.sort_direction) {
|
||||||
Some(false) => "go-up-symbolic",
|
(true, true) => "go-down-symbolic",
|
||||||
None => "list-remove-symbolic",
|
(true, false) => "go-up-symbolic",
|
||||||
}))
|
_ => "list-remove-symbolic",
|
||||||
|
},
|
||||||
|
))
|
||||||
.on_press(Message::ToggleSort($msg))
|
.on_press(Message::ToggleSort($msg))
|
||||||
.style(cosmic::style::Button::Icon)
|
.style(cosmic::style::Button::Icon)
|
||||||
.into(),
|
.into(),
|
||||||
|
|
@ -1200,7 +1209,7 @@ impl Tab {
|
||||||
|
|
||||||
children.push(horizontal_rule(1).into());
|
children.push(horizontal_rule(1).into());
|
||||||
|
|
||||||
if let Some(ref items) = self.items_opt {
|
if let Some(ref items) = self.column_sort() {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
let mut hidden = 0;
|
let mut hidden = 0;
|
||||||
let TabConfig {
|
let TabConfig {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue