mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41: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,
|
offset: usize,
|
||||||
cursor: usize,
|
cursor: usize,
|
||||||
|
|
||||||
sort: FolderSort,
|
sort: FolderSort,
|
||||||
show_hidden: bool,
|
show_hidden: bool,
|
||||||
|
pub in_search: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
@ -43,6 +44,15 @@ impl Folder {
|
||||||
Self { cwd: cwd.to_path_buf(), show_hidden: MANAGER.show_hidden, ..Default::default() }
|
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]
|
#[inline]
|
||||||
pub fn limit() -> usize { tty_size().ws_row.saturating_sub(DIR_PADDING) as usize }
|
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 cwd = self.current().cwd.clone();
|
||||||
let hovered = self.hovered().map(|h| h.path.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)
|
self.current_mut().update(items)
|
||||||
} else if matches!(self.parent(), Some(p) if p.cwd == path) {
|
} else if matches!(self.parent(), Some(p) if p.cwd == path) {
|
||||||
self.active_mut().parent.as_mut().unwrap().update(items)
|
self.active_mut().parent.as_mut().unwrap().update(items)
|
||||||
|
|
|
||||||
|
|
@ -74,34 +74,47 @@ impl Tab {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let current = self.history.remove(&hovered.path).unwrap_or_else(|| Folder::new(&hovered.path));
|
let rep = self.history_new(&hovered.path);
|
||||||
let parent = mem::replace(&mut self.current, current);
|
let rep = mem::replace(&mut self.current, rep);
|
||||||
|
if !rep.in_search {
|
||||||
if self.parent.is_none() {
|
self.history.insert(rep.cwd.clone(), rep);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
emit!(Refresh);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn leave(&mut self) -> bool {
|
pub fn leave(&mut self) -> bool {
|
||||||
let parent = if let Some(p) = &self.parent {
|
let current = self
|
||||||
p.cwd.clone()
|
.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 {
|
} else {
|
||||||
return false;
|
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 rep = self.history_new(¤t);
|
||||||
let parent = mem::replace(&mut self.parent, pparent).unwrap();
|
let rep = mem::replace(&mut self.current, rep);
|
||||||
let current = mem::replace(&mut self.current, parent);
|
if !rep.in_search {
|
||||||
self.history.insert(cwd, current);
|
self.history.insert(rep.cwd.clone(), rep);
|
||||||
|
}
|
||||||
|
|
||||||
emit!(Refresh);
|
emit!(Refresh);
|
||||||
true
|
true
|
||||||
|
|
@ -139,6 +152,11 @@ impl Tab {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn history(&self, path: &Path) -> Option<&Folder> { self.history.get(path) }
|
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]
|
#[inline]
|
||||||
pub fn preview(&self) -> &Preview { &self.preview }
|
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())
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
||||||
.split(area);
|
.split(area);
|
||||||
|
|
||||||
let manager = &self.cx.manager;
|
let current = &self.cx.manager.current();
|
||||||
Paragraph::new(readable_path(&manager.current().cwd))
|
let location = if current.in_search {
|
||||||
.style(Style::default().fg(Color::Cyan))
|
format!("{} (search)", readable_path(¤t.cwd))
|
||||||
.render(chunks[0], buf);
|
} 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);
|
Tabs::new(self.cx).render(chunks[1], buf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue