From d55e002d5a82ad7eeb816f78512cdbee576c2d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20misaki=20masa?= Date: Thu, 5 Jun 2025 20:11:37 +0800 Subject: [PATCH] fix: `cd` on the initial creation of a tab should not be added to the backstack (#2842) --- yazi-core/src/mgr/commands/tab_create.rs | 8 +++---- yazi-core/src/mgr/tabs.rs | 6 ++--- yazi-core/src/tab/commands/back.rs | 6 ++--- yazi-core/src/tab/commands/cd.rs | 30 ++++++++++++++---------- yazi-core/src/tab/commands/enter.rs | 3 ++- yazi-core/src/tab/commands/forward.rs | 6 ++--- yazi-core/src/tab/commands/leave.rs | 3 ++- yazi-core/src/tab/commands/reveal.rs | 13 +++++++--- 8 files changed, 43 insertions(+), 32 deletions(-) diff --git a/yazi-core/src/mgr/commands/tab_create.rs b/yazi-core/src/mgr/commands/tab_create.rs index 7047f934..f05c54fe 100644 --- a/yazi-core/src/mgr/commands/tab_create.rs +++ b/yazi-core/src/mgr/commands/tab_create.rs @@ -3,7 +3,7 @@ use yazi_macro::render; use yazi_proxy::AppProxy; use yazi_shared::{event::CmdCow, url::Url}; -use crate::{mgr::Tabs, tab::Tab}; +use crate::{mgr::Tabs, tab::{Tab, commands::CdSource}}; const MAX_TABS: usize = 9; @@ -35,15 +35,15 @@ impl Tabs { let mut tab = Tab::default(); if !opt.current { - tab.cd(opt.url); + tab.cd((opt.url, CdSource::Tab)); } else if let Some(h) = self.active().hovered() { tab.pref = self.active().pref.clone(); tab.apply_files_attrs(); - tab.reveal(h.url.to_regular()); + tab.reveal((h.url.to_regular(), CdSource::Tab)); } else { tab.pref = self.active().pref.clone(); tab.apply_files_attrs(); - tab.cd(self.active().cwd().to_regular()); + tab.cd((self.active().cwd().to_regular(), CdSource::Tab)); } self.items.insert(self.cursor + 1, tab); diff --git a/yazi-core/src/mgr/tabs.rs b/yazi-core/src/mgr/tabs.rs index 556a4f1d..45e81a21 100644 --- a/yazi-core/src/mgr/tabs.rs +++ b/yazi-core/src/mgr/tabs.rs @@ -6,7 +6,7 @@ use yazi_fs::File; use yazi_proxy::MgrProxy; use yazi_shared::{Id, url::Url}; -use crate::tab::{Folder, Tab}; +use crate::tab::{Folder, Tab, commands::CdSource}; pub struct Tabs { pub cursor: usize, @@ -21,9 +21,9 @@ impl Tabs { for (i, tab) in tabs.iter_mut().enumerate() { let file = &BOOT.files[i]; if file.is_empty() { - tab.cd(Url::from(&BOOT.cwds[i])); + tab.cd((Url::from(&BOOT.cwds[i]), CdSource::Tab)); } else { - tab.reveal(Url::from(BOOT.cwds[i].join(file))); + tab.reveal((Url::from(BOOT.cwds[i].join(file)), CdSource::Tab)); } } tabs diff --git a/yazi-core/src/tab/commands/back.rs b/yazi-core/src/tab/commands/back.rs index f7f7ddc3..8318ae86 100644 --- a/yazi-core/src/tab/commands/back.rs +++ b/yazi-core/src/tab/commands/back.rs @@ -1,14 +1,12 @@ use yazi_shared::event::CmdCow; +use super::cd::CdSource; use crate::tab::Tab; impl Tab { pub fn back(&mut self, _: CmdCow) { - if self.current.url.is_regular() { - self.backstack.push(&self.current.url); - } if let Some(u) = self.backstack.shift_backward().cloned() { - self.cd((u, super::cd::OptSource::Back)); + self.cd((u, CdSource::Back)); } } } diff --git a/yazi-core/src/tab/commands/cd.rs b/yazi-core/src/tab/commands/cd.rs index 887a6b76..c18437dd 100644 --- a/yazi-core/src/tab/commands/cd.rs +++ b/yazi-core/src/tab/commands/cd.rs @@ -13,14 +13,14 @@ use crate::tab::Tab; struct Opt { target: Url, - source: OptSource, + source: CdSource, interactive: bool, } impl From for Opt { fn from(mut c: CmdCow) -> Self { Self { - source: OptSource::Cd, + source: CdSource::Cd, interactive: c.bool("interactive"), ..Self::from(c.take_first_url().unwrap_or_default()) } @@ -28,11 +28,11 @@ impl From for Opt { } impl From for Opt { - fn from(target: Url) -> Self { Self::from((target, OptSource::Cd)) } + fn from(target: Url) -> Self { Self::from((target, CdSource::Cd)) } } -impl From<(Url, OptSource)> for Opt { - fn from((mut target, source): (Url, OptSource)) -> Self { +impl From<(Url, CdSource)> for Opt { + fn from((mut target, source): (Url, CdSource)) -> Self { if target.is_regular() { target = Url::from(expand_path(&target)); } @@ -60,6 +60,16 @@ impl Tab { self.history.insert(rep.url.to_owned(), rep); } + // Backstack + if opt.source.big_jump() { + if self.current.url.is_regular() { + self.backstack.push(&self.current.url); + } + if opt.target.is_regular() { + self.backstack.push(&opt.target); + } + } + // Current let rep = self.history.remove_or(&opt.target); let rep = mem::replace(&mut self.current, rep); @@ -72,11 +82,6 @@ impl Tab { self.parent = Some(self.history.remove_or(&parent)); } - // Backstack - if opt.source.big_jump() && opt.target.is_regular() { - self.backstack.push(&opt.target); - } - Pubsub::pub_from_cd(self.id, self.cwd()); self.hover(None); @@ -118,7 +123,8 @@ impl Tab { // --- OptSource #[derive(Clone, Copy, PartialEq, Eq)] -pub(super) enum OptSource { +pub enum CdSource { + Tab, Cd, Reveal, Enter, @@ -127,7 +133,7 @@ pub(super) enum OptSource { Back, } -impl OptSource { +impl CdSource { #[inline] fn big_jump(self) -> bool { self == Self::Cd || self == Self::Reveal } } diff --git a/yazi-core/src/tab/commands/enter.rs b/yazi-core/src/tab/commands/enter.rs index aeefe4b9..ec373e5d 100644 --- a/yazi-core/src/tab/commands/enter.rs +++ b/yazi-core/src/tab/commands/enter.rs @@ -1,5 +1,6 @@ use yazi_shared::event::CmdCow; +use super::cd::CdSource; use crate::tab::Tab; impl Tab { @@ -8,6 +9,6 @@ impl Tab { .hovered() .filter(|h| h.is_dir()) .map(|h| h.url.to_regular()) - .map(|u| self.cd((u, super::cd::OptSource::Enter))); + .map(|u| self.cd((u, CdSource::Enter))); } } diff --git a/yazi-core/src/tab/commands/forward.rs b/yazi-core/src/tab/commands/forward.rs index aaefd99f..00feb0b8 100644 --- a/yazi-core/src/tab/commands/forward.rs +++ b/yazi-core/src/tab/commands/forward.rs @@ -1,14 +1,12 @@ use yazi_shared::event::CmdCow; +use super::cd::CdSource; use crate::tab::Tab; impl Tab { pub fn forward(&mut self, _: CmdCow) { - if self.current.url.is_regular() { - self.backstack.push(&self.current.url); - } if let Some(u) = self.backstack.shift_forward().cloned() { - self.cd((u, super::cd::OptSource::Forward)); + self.cd((u, CdSource::Forward)); } } } diff --git a/yazi-core/src/tab/commands/leave.rs b/yazi-core/src/tab/commands/leave.rs index db4a3c40..6f6cf307 100644 --- a/yazi-core/src/tab/commands/leave.rs +++ b/yazi-core/src/tab/commands/leave.rs @@ -1,5 +1,6 @@ use yazi_shared::event::CmdCow; +use super::cd::CdSource; use crate::tab::Tab; struct Opt; @@ -19,6 +20,6 @@ impl Tab { .and_then(|h| h.url.parent_url()) .filter(|u| u != self.cwd()) .or_else(|| self.cwd().parent_url()) - .map(|u| self.cd((u.into_regular(), super::cd::OptSource::Leave))); + .map(|u| self.cd((u.into_regular(), CdSource::Leave))); } } diff --git a/yazi-core/src/tab/commands/reveal.rs b/yazi-core/src/tab/commands/reveal.rs index 0e55faaa..4e1eae24 100644 --- a/yazi-core/src/tab/commands/reveal.rs +++ b/yazi-core/src/tab/commands/reveal.rs @@ -2,10 +2,12 @@ use yazi_fs::{File, FilesOp, expand_path}; use yazi_proxy::MgrProxy; use yazi_shared::{event::CmdCow, url::Url}; +use super::cd::CdSource; use crate::tab::Tab; struct Opt { target: Url, + source: CdSource, no_dummy: bool, } @@ -16,11 +18,16 @@ impl From for Opt { target = Url::from(expand_path(&target)); } - Self { target, no_dummy: c.bool("no-dummy") } + Self { target, source: CdSource::Reveal, no_dummy: c.bool("no-dummy") } } } + impl From for Opt { - fn from(target: Url) -> Self { Self { target, no_dummy: false } } + fn from(target: Url) -> Self { Self { target, source: CdSource::Reveal, no_dummy: false } } +} + +impl From<(Url, CdSource)> for Opt { + fn from((target, source): (Url, CdSource)) -> Self { Self { target, source, no_dummy: false } } } impl Tab { @@ -30,7 +37,7 @@ impl Tab { return; }; - self.cd((parent.clone(), super::cd::OptSource::Reveal)); + self.cd((parent.clone(), opt.source)); self.current.hover(child.as_urn()); if !opt.no_dummy && self.hovered().is_none_or(|f| &child != f.urn()) {