mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
refactor: enter, leave
This commit is contained in:
parent
a72acf6fd3
commit
66ac5f9d9c
4 changed files with 55 additions and 23 deletions
|
|
@ -14,8 +14,9 @@ pub struct Folder {
|
|||
offset: usize,
|
||||
cursor: usize,
|
||||
|
||||
sort: FolderSort,
|
||||
show_hidden: bool,
|
||||
sort: FolderSort,
|
||||
show_hidden: bool,
|
||||
pub in_search: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -43,6 +44,15 @@ impl Folder {
|
|||
Self { cwd: cwd.to_path_buf(), show_hidden: MANAGER.show_hidden, ..Default::default() }
|
||||
}
|
||||
|
||||
pub fn new_search(cwd: &Path) -> Self {
|
||||
Self {
|
||||
cwd: cwd.to_path_buf(),
|
||||
show_hidden: MANAGER.show_hidden,
|
||||
in_search: true,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn limit() -> usize { tty_size().ws_row.saturating_sub(DIR_PADDING) as usize }
|
||||
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ impl Manager {
|
|||
let cwd = self.current().cwd.clone();
|
||||
let hovered = self.hovered().map(|h| h.path.clone());
|
||||
|
||||
let mut b = if self.current().cwd == path {
|
||||
let mut b = if cwd == path && !self.current().in_search {
|
||||
self.current_mut().update(items)
|
||||
} else if matches!(self.parent(), Some(p) if p.cwd == path) {
|
||||
self.active_mut().parent.as_mut().unwrap().update(items)
|
||||
|
|
|
|||
|
|
@ -74,34 +74,47 @@ impl Tab {
|
|||
return false;
|
||||
}
|
||||
|
||||
let current = self.history.remove(&hovered.path).unwrap_or_else(|| Folder::new(&hovered.path));
|
||||
let parent = mem::replace(&mut self.current, current);
|
||||
|
||||
if self.parent.is_none() {
|
||||
self.parent = Some(parent);
|
||||
} else {
|
||||
let cwd = self.parent.as_ref().unwrap().cwd.clone();
|
||||
let pparent = mem::replace(self.parent.as_mut().unwrap(), parent);
|
||||
self.history.insert(cwd, pparent);
|
||||
let rep = self.history_new(&hovered.path);
|
||||
let rep = mem::replace(&mut self.current, rep);
|
||||
if !rep.in_search {
|
||||
self.history.insert(rep.cwd.clone(), rep);
|
||||
}
|
||||
|
||||
if let Some(rep) = self.parent.take() {
|
||||
self.history.insert(rep.cwd.clone(), rep);
|
||||
}
|
||||
self.parent = Some(self.history_new(hovered.path.parent().unwrap()));
|
||||
|
||||
emit!(Refresh);
|
||||
true
|
||||
}
|
||||
|
||||
pub fn leave(&mut self) -> bool {
|
||||
let parent = if let Some(p) = &self.parent {
|
||||
p.cwd.clone()
|
||||
let current = self
|
||||
.current
|
||||
.hovered()
|
||||
.and_then(|h| h.path.parent())
|
||||
.and_then(|p| if p == self.current.cwd { None } else { Some(p) })
|
||||
.or_else(|| self.current.cwd.parent());
|
||||
|
||||
let current = if let Some(c) = current {
|
||||
c.to_owned()
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let pparent = parent.parent().map(|p| self.history.remove(p).unwrap_or_else(|| Folder::new(p)));
|
||||
if let Some(rep) = self.parent.take() {
|
||||
self.history.insert(rep.cwd.clone(), rep);
|
||||
}
|
||||
if let Some(parent) = current.parent() {
|
||||
self.parent = Some(self.history_new(parent));
|
||||
}
|
||||
|
||||
let cwd = self.current.cwd.clone();
|
||||
let parent = mem::replace(&mut self.parent, pparent).unwrap();
|
||||
let current = mem::replace(&mut self.current, parent);
|
||||
self.history.insert(cwd, current);
|
||||
let rep = self.history_new(¤t);
|
||||
let rep = mem::replace(&mut self.current, rep);
|
||||
if !rep.in_search {
|
||||
self.history.insert(rep.cwd.clone(), rep);
|
||||
}
|
||||
|
||||
emit!(Refresh);
|
||||
true
|
||||
|
|
@ -139,6 +152,11 @@ impl Tab {
|
|||
#[inline]
|
||||
pub fn history(&self, path: &Path) -> Option<&Folder> { self.history.get(path) }
|
||||
|
||||
#[inline]
|
||||
pub fn history_new(&mut self, path: &Path) -> Folder {
|
||||
self.history.remove(path).unwrap_or_else(|| Folder::new(path))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn preview(&self) -> &Preview { &self.preview }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,10 +18,14 @@ impl<'a> Widget for Layout<'a> {
|
|||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
||||
.split(area);
|
||||
|
||||
let manager = &self.cx.manager;
|
||||
Paragraph::new(readable_path(&manager.current().cwd))
|
||||
.style(Style::default().fg(Color::Cyan))
|
||||
.render(chunks[0], buf);
|
||||
let current = &self.cx.manager.current();
|
||||
let location = if current.in_search {
|
||||
format!("{} (search)", readable_path(¤t.cwd))
|
||||
} else {
|
||||
format!("{}", readable_path(¤t.cwd))
|
||||
};
|
||||
|
||||
Paragraph::new(location).style(Style::default().fg(Color::Cyan)).render(chunks[0], buf);
|
||||
|
||||
Tabs::new(self.cx).render(chunks[1], buf);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue