Skip to content

Commit

Permalink
feat: tab-specific sorting (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Sep 11, 2023
1 parent cad6560 commit 1a1dd9c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
6 changes: 2 additions & 4 deletions core/src/files/files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::{BTreeMap, BTreeSet}, mem, ops::Deref, sync::atomic::Ordering};

use anyhow::Result;
use config::manager::SortBy;
use config::{manager::SortBy, MANAGER};
use shared::Url;
use tokio::{fs, select, sync::mpsc::{self, UnboundedReceiver}};

Expand Down Expand Up @@ -32,7 +32,7 @@ impl Default for Files {
selected: Default::default(),

sorter: Default::default(),
show_hidden: true,
show_hidden: MANAGER.show_hidden,
}
}
}
Expand Down Expand Up @@ -244,7 +244,6 @@ impl Files {
#[inline]
pub fn sorter(&self) -> &FilesSorter { &self.sorter }

#[inline]
pub fn set_sorter(&mut self, sorter: FilesSorter) -> bool {
if self.sorter == sorter {
return false;
Expand All @@ -255,7 +254,6 @@ impl Files {
}

// --- Show hidden
#[inline]
pub fn set_show_hidden(&mut self, state: bool) -> bool {
if state == self.show_hidden {
return false;
Expand Down
2 changes: 1 addition & 1 deletion core/src/files/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use shared::Url;

use super::File;

#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
pub struct FilesSorter {
pub by: SortBy,
pub reverse: bool,
Expand Down
4 changes: 2 additions & 2 deletions core/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Manager {
pub fn refresh(&mut self) {
env::set_current_dir(self.cwd()).ok();

self.active_mut().apply_show_hidden(false);
self.active_mut().apply_files_attrs(false);

if let Some(f) = self.parent() {
self.watcher.trigger_dirs(&[self.cwd(), &f.cwd]);
Expand Down Expand Up @@ -339,7 +339,7 @@ impl Manager {
self.active_mut().parent.as_mut().unwrap().update(op)
} else if matches!(self.hovered(), Some(h) if h.url() == &url) {
self.active_mut().history.entry(url.clone()).or_insert_with(|| Folder::from(&url));
self.active_mut().apply_show_hidden(true);
self.active_mut().apply_files_attrs(true);
self.active_mut().history.get_mut(&url).unwrap().update(op)
} else {
self.active_mut().history.entry(url.clone()).or_insert_with(|| Folder::from(&url)).update(op);
Expand Down
42 changes: 25 additions & 17 deletions core/src/manager/tab.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, collections::{BTreeMap, BTreeSet}, ffi::{OsStr, OsString}, mem, time::Duration};

use anyhow::{bail, Error, Result};
use config::{keymap::{Exec, KeymapLayer}, open::Opener};
use config::{keymap::{Exec, KeymapLayer}, open::Opener, MANAGER};
use shared::{Debounce, Defer, InputError, Url};
use tokio::{pin, task::JoinHandle};
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
Expand All @@ -19,6 +19,7 @@ pub struct Tab {

finder: Option<Finder>,
search: Option<JoinHandle<Result<()>>>,
pub(super) sorter: FilesSorter,
pub(super) show_hidden: bool,
}

Expand All @@ -36,7 +37,8 @@ impl From<Url> for Tab {

finder: None,
search: None,
show_hidden: true,
sorter: Default::default(),
show_hidden: MANAGER.show_hidden,
}
}
}
Expand Down Expand Up @@ -465,12 +467,12 @@ impl Tab {

// --- Sorter
pub fn set_sorter(&mut self, sorter: FilesSorter) -> bool {
if !self.current.files.set_sorter(sorter) {
if sorter == self.sorter {
return false;
}

self.current.hover_repos();
true
self.sorter = sorter;
self.apply_files_attrs(false)
}

// --- Show hidden
Expand All @@ -481,26 +483,32 @@ impl Tab {
}

self.show_hidden = state;
self.apply_show_hidden(false);
true
self.apply_files_attrs(false)
}

pub fn apply_show_hidden(&mut self, only_hovered: bool) -> bool {
let state = self.show_hidden;
let mut b = match self.current.hovered {
Some(ref h) if h.is_dir() => {
self.history.get_mut(h.url()).map(|f| f.files.set_show_hidden(state)) == Some(true)
}
_ => false,
};
pub fn apply_files_attrs(&mut self, only_hovered: bool) -> bool {
let mut b = false;
if let Some(f) = self
.current
.hovered
.as_ref()
.filter(|h| h.is_dir())
.and_then(|h| self.history.get_mut(h.url()))
{
b |= f.files.set_show_hidden(self.show_hidden);
b |= f.files.set_sorter(self.sorter);
}

if only_hovered {
return b;
}

b |= self.current.files.set_show_hidden(state);
b |= self.current.files.set_show_hidden(self.show_hidden);
b |= self.current.files.set_sorter(self.sorter);

if let Some(parent) = self.parent.as_mut() {
b |= parent.files.set_show_hidden(state);
b |= parent.files.set_show_hidden(self.show_hidden);
b |= parent.files.set_sorter(self.sorter);
}

self.current.hover_repos();
Expand Down
8 changes: 3 additions & 5 deletions core/src/manager/tabs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use config::{BOOT, MANAGER};
use config::BOOT;
use shared::Url;

use super::Tab;
Expand All @@ -13,10 +13,7 @@ pub struct Tabs {

impl Tabs {
pub fn make() -> Self {
let mut tab = Tab::from(Url::from(&BOOT.cwd));
tab.set_show_hidden(Some(MANAGER.show_hidden));

let mut tabs = Self { idx: usize::MAX, items: vec![tab] };
let mut tabs = Self { idx: usize::MAX, items: vec![Tab::from(Url::from(&BOOT.cwd))] };
tabs.set_idx(0);
tabs
}
Expand All @@ -28,6 +25,7 @@ impl Tabs {

let mut tab = Tab::from(url);
tab.set_show_hidden(Some(self.active().show_hidden));
tab.set_sorter(self.active().sorter);

self.items.insert(self.idx + 1, tab);
self.set_idx(self.idx + 1);
Expand Down

0 comments on commit 1a1dd9c

Please sign in to comment.