Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: the cd event in DDS will now also be triggered when the tab is first created #861

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions yazi-core/src/folder/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ pub struct Folder {
pub tracing: bool,
}

impl From<Url> for Folder {
fn from(cwd: Url) -> Self { Self { cwd, ..Default::default() } }
}

impl From<&Url> for Folder {
fn from(cwd: &Url) -> Self { Self::from(cwd.clone()) }
fn from(cwd: &Url) -> Self { Self { cwd: cwd.clone(), ..Default::default() } }
}

impl Folder {
Expand Down
3 changes: 2 additions & 1 deletion yazi-core/src/manager/commands/tab_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ impl Tabs {
let opt = opt.into() as Opt;
let url = if opt.current { self.active().current.cwd.to_owned() } else { opt.url.unwrap() };

let mut tab = Tab::from(url);
let mut tab = Tab::default();
tab.conf = self.active().conf.clone();
tab.apply_files_attrs();
tab.cd(url);

self.items.insert(self.cursor + 1, tab);
self.set_idx(self.cursor + 1);
Expand Down
5 changes: 3 additions & 2 deletions yazi-core/src/manager/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ pub struct Tabs {

impl Tabs {
pub fn make() -> Self {
let mut tabs = Self { cursor: 0, items: vec![Tab::from(Url::from(&BOOT.cwd))] };
let mut tabs = Self { cursor: 0, items: vec![Tab::default()] };
if let Some(file) = &BOOT.file {
tabs.items[0].reveal(Url::from(BOOT.cwd.join(file)));
} else {
tabs.items[0].cd(Url::from(&BOOT.cwd));
}

ManagerProxy::refresh();
tabs
}

Expand Down
49 changes: 25 additions & 24 deletions yazi-core/src/tab/backstack.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#[derive(Default)]
pub struct Backstack<T: Eq> {
cursor: usize,
stack: Vec<T>,
}

impl<T: Eq> Backstack<T> {
pub fn new(item: T) -> Self { Self { cursor: 0, stack: vec![item] } }

pub fn push(&mut self, item: T) {
if self.stack.is_empty() {
self.stack.push(item);
return;
}

if self.stack[self.cursor] == item {
return;
}
Expand All @@ -27,10 +31,6 @@ impl<T: Eq> Backstack<T> {
}
}

#[cfg(test)]
#[inline]
pub fn current(&self) -> &T { &self.stack[self.cursor] }

pub fn shift_backward(&mut self) -> Option<&T> {
if self.cursor > 0 {
self.cursor -= 1;
Expand All @@ -56,27 +56,28 @@ mod tests {

#[test]
fn test_backstack() {
let mut backstack = Backstack::<u32>::new(1);
assert_eq!(backstack.current(), &1);
let mut bs = Backstack::default();
bs.push(1);
assert_eq!(bs.stack[bs.cursor], 1);

backstack.push(2);
backstack.push(3);
assert_eq!(backstack.current(), &3);
bs.push(2);
bs.push(3);
assert_eq!(bs.stack[bs.cursor], 3);

assert_eq!(backstack.shift_backward(), Some(&2));
assert_eq!(backstack.shift_backward(), Some(&1));
assert_eq!(backstack.shift_backward(), None);
assert_eq!(backstack.shift_backward(), None);
assert_eq!(backstack.current(), &1);
assert_eq!(backstack.shift_forward(), Some(&2));
assert_eq!(backstack.shift_forward(), Some(&3));
assert_eq!(backstack.shift_forward(), None);
assert_eq!(bs.shift_backward(), Some(&2));
assert_eq!(bs.shift_backward(), Some(&1));
assert_eq!(bs.shift_backward(), None);
assert_eq!(bs.shift_backward(), None);
assert_eq!(bs.stack[bs.cursor], 1);
assert_eq!(bs.shift_forward(), Some(&2));
assert_eq!(bs.shift_forward(), Some(&3));
assert_eq!(bs.shift_forward(), None);

backstack.shift_backward();
backstack.push(4);
bs.shift_backward();
bs.push(4);

assert_eq!(backstack.current(), &4);
assert_eq!(backstack.shift_forward(), None);
assert_eq!(backstack.shift_backward(), Some(&2));
assert_eq!(bs.stack[bs.cursor], 4);
assert_eq!(bs.shift_forward(), None);
assert_eq!(bs.shift_backward(), Some(&2));
}
}
28 changes: 1 addition & 27 deletions yazi-core/src/tab/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use yazi_shared::{fs::Url, render};
use super::{Backstack, Config, Finder, Mode, Preview};
use crate::{folder::{Folder, FolderStage}, tab::Selected};

#[derive(Default)]
pub struct Tab {
pub idx: usize,
pub mode: Mode,
Expand All @@ -23,33 +24,6 @@ pub struct Tab {
pub(super) search: Option<JoinHandle<Result<()>>>,
}

impl From<Url> for Tab {
fn from(url: Url) -> Self {
let parent = url.parent_url().map(Folder::from);

Self {
idx: 0,
mode: Default::default(),
current: Folder::from(url.clone()),
parent,

backstack: Backstack::new(url),
history: Default::default(),
selected: Default::default(),

preview: Default::default(),
finder: None,
search: None,

conf: Default::default(),
}
}
}

impl From<&Url> for Tab {
fn from(url: &Url) -> Self { Self::from(url.clone()) }
}

impl Tab {
pub fn shutdown(&mut self) {
if let Some(handle) = self.search.take() {
Expand Down
27 changes: 9 additions & 18 deletions yazi-dds/src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use anyhow::Result;
use mlua::{ExternalResult, IntoLua, Lua, Value};
use serde::{Deserialize, Serialize};

use super::{BodyBulk, BodyCd, BodyCustom, BodyHey, BodyHi, BodyHover, BodyRename, BodyTabs, BodyYank};
use super::{BodyBulk, BodyCd, BodyCustom, BodyHey, BodyHi, BodyHover, BodyRename, BodyYank};
use crate::Payload;

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Body<'a> {
Hi(BodyHi<'a>),
Hey(BodyHey),
Tabs(BodyTabs<'a>),
Cd(BodyCd<'a>),
Hover(BodyHover<'a>),
Rename(BodyRename<'a>),
Expand All @@ -24,7 +23,6 @@ impl<'a> Body<'a> {
Ok(match kind {
"hi" => Body::Hi(serde_json::from_str(body)?),
"hey" => Body::Hey(serde_json::from_str(body)?),
"tabs" => Body::Tabs(serde_json::from_str(body)?),
"cd" => Body::Cd(serde_json::from_str(body)?),
"hover" => Body::Hover(serde_json::from_str(body)?),
"rename" => Body::Rename(serde_json::from_str(body)?),
Expand All @@ -36,7 +34,7 @@ impl<'a> Body<'a> {

pub fn from_lua(kind: &str, value: Value) -> Result<Self> {
Ok(match kind {
"hi" | "hey" | "tabs" | "cd" | "hover" | "rename" | "bulk" | "yank" => {
"hi" | "hey" | "cd" | "hover" | "rename" | "bulk" | "yank" => {
Err("Cannot construct system event from Lua").into_lua_err()?
}
_ => BodyCustom::from_lua(kind, value)?,
Expand All @@ -48,7 +46,6 @@ impl<'a> Body<'a> {
match self {
Self::Hi(_) => "hi",
Self::Hey(_) => "hey",
Self::Tabs(_) => "tabs",
Self::Cd(_) => "cd",
Self::Hover(_) => "hover",
Self::Rename(_) => "rename",
Expand All @@ -73,18 +70,13 @@ impl<'a> Body<'a> {
}
}

pub fn upgrade(self) -> Payload<'a> {
let severity = match self {
Body::Hi(_) => 0,
Body::Hey(_) => 0,
Body::Tabs(_) => 10,
Body::Cd(_) => 20,
Body::Hover(_) => 30,
Body::Rename(_) => 0,
Body::Bulk(_) => 0,
Body::Yank(_) => 40,
Body::Custom(_) => 0,
};
#[inline]
pub fn with_receiver(self, receiver: u64) -> Payload<'a> {
Payload::new(self).with_receiver(receiver)
}

#[inline]
pub fn with_severity(self, severity: u8) -> Payload<'a> {
Payload::new(self).with_severity(severity)
}
}
Expand All @@ -94,7 +86,6 @@ impl IntoLua<'_> for Body<'static> {
match self {
Body::Hi(b) => b.into_lua(lua),
Body::Hey(b) => b.into_lua(lua),
Body::Tabs(b) => b.into_lua(lua),
Body::Cd(b) => b.into_lua(lua),
Body::Hover(b) => b.into_lua(lua),
Body::Rename(b) => b.into_lua(lua),
Expand Down
2 changes: 0 additions & 2 deletions yazi-dds/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod hey;
mod hi;
mod hover;
mod rename;
mod tabs;
mod yank;

pub use body::*;
Expand All @@ -19,5 +18,4 @@ pub use hey::*;
pub use hi::*;
pub use hover::*;
pub use rename::*;
pub use tabs::*;
pub use yank::*;
59 changes: 0 additions & 59 deletions yazi-dds/src/body/tabs.rs

This file was deleted.

12 changes: 8 additions & 4 deletions yazi-dds/src/body/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ use super::Body;
pub struct BodyYank<'a> {
pub cut: bool,
pub urls: Cow<'a, HashSet<Url>>,
#[serde(skip)]
dummy: bool,
}

impl<'a> BodyYank<'a> {
#[inline]
pub fn borrowed(cut: bool, urls: &'a HashSet<Url>) -> Body<'a> {
Self { cut, urls: Cow::Borrowed(urls) }.into()
Self { cut, urls: Cow::Borrowed(urls), dummy: false }.into()
}
}

impl BodyYank<'static> {
#[inline]
pub fn dummy(cut: bool) -> Body<'static> { Self { cut, urls: Default::default() }.into() }
pub fn dummy(cut: bool) -> Body<'static> {
Self { cut, urls: Default::default(), dummy: true }.into()
}
}

impl<'a> From<BodyYank<'a>> for Body<'a> {
Expand All @@ -30,7 +34,7 @@ impl<'a> From<BodyYank<'a>> for Body<'a> {

impl IntoLua<'_> for BodyYank<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value<'_>> {
if let Cow::Owned(urls) = self.urls {
if let Some(Cow::Owned(urls)) = Some(self.urls).filter(|_| !self.dummy) {
BodyYankIter { cut: self.cut, urls: urls.into_iter().collect() }.into_lua(lua)
} else {
lua.create_table_from([("cut", self.cut)])?.into_lua(lua)
Expand All @@ -46,7 +50,7 @@ pub struct BodyYankIter {

impl UserData for BodyYankIter {
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("is_cut", |_, me| Ok(me.cut));
fields.add_field_method_get("cut", |_, me| Ok(me.cut));
}

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
Expand Down
6 changes: 4 additions & 2 deletions yazi-dds/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Client {
if line.starts_with("hey,") {
Self::handle_hey(line);
} else {
Payload::from_str(&line).map(|p| p.flush(false).emit()).ok();
Payload::from_str(&line).map(|p| p.emit()).ok();
}
}
}
Expand All @@ -62,7 +62,9 @@ impl Client {
}

#[inline]
pub(super) fn push(payload: Payload) { QUEUE.send(format!("{}\n", payload)).ok(); }
pub(super) fn push<'a>(payload: impl Into<Payload<'a>>) {
QUEUE.send(format!("{}\n", payload.into())).ok();
}

#[inline]
pub(super) fn able(&self, ability: &str) -> bool { self.abilities.contains(ability) }
Expand Down
Loading