Skip to content

Commit

Permalink
feat: add new --orphan option to the shell command (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Apr 8, 2024
1 parent 0cfb50b commit 884de41
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 37 deletions.
1 change: 1 addition & 0 deletions yazi-boot/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl Boot {
)?;

writeln!(s, "\nVariables")?;
writeln!(s, " SHELL: {:?}", env::var_os("SHELL"))?;
writeln!(s, " EDITOR: {:?}", env::var_os("EDITOR"))?;
writeln!(s, " ZELLIJ_SESSION_NAME: {:?}", env::var_os("ZELLIJ_SESSION_NAME"))?;
writeln!(s, " YAZI_FILE_ONE: {:?}", env::var_os("YAZI_FILE_ONE"))?;
Expand Down
6 changes: 2 additions & 4 deletions yazi-config/preset/theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,9 @@ rules = [
{ mime = "audio/*", fg = "yellow" },

# Archives
{ mime = "application/zip", fg = "magenta" },
{ mime = "application/gzip", fg = "magenta" },
{ mime = "application/*zip", fg = "magenta" },
{ mime = "application/x-tar", fg = "magenta" },
{ mime = "application/x-bzip", fg = "magenta" },
{ mime = "application/x-bzip2", fg = "magenta" },
{ mime = "application/x-bzip*", fg = "magenta" },
{ mime = "application/x-7z-compressed", fg = "magenta" },
{ mime = "application/x-rar", fg = "magenta" },
{ mime = "application/x-xz", fg = "magenta" },
Expand Down
12 changes: 4 additions & 8 deletions yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ rules = [
{ mime = "application/json", use = [ "edit", "reveal" ] },
{ mime = "*/javascript", use = [ "edit", "reveal" ] },

{ mime = "application/zip", use = [ "extract", "reveal" ] },
{ mime = "application/gzip", use = [ "extract", "reveal" ] },
{ mime = "application/*zip", use = [ "extract", "reveal" ] },
{ mime = "application/x-tar", use = [ "extract", "reveal" ] },
{ mime = "application/x-bzip", use = [ "extract", "reveal" ] },
{ mime = "application/x-bzip2", use = [ "extract", "reveal" ] },
{ mime = "application/x-bzip*", use = [ "extract", "reveal" ] },
{ mime = "application/x-7z-compressed", use = [ "extract", "reveal" ] },
{ mime = "application/x-rar", use = [ "extract", "reveal" ] },
{ mime = "application/x-xz", use = [ "extract", "reveal" ] },
Expand Down Expand Up @@ -111,11 +109,9 @@ previewers = [
# PDF
{ mime = "application/pdf", run = "pdf" },
# Archive
{ mime = "application/zip", run = "archive" },
{ mime = "application/gzip", run = "archive" },
{ mime = "application/*zip", run = "archive" },
{ mime = "application/x-tar", run = "archive" },
{ mime = "application/x-bzip", run = "archive" },
{ mime = "application/x-bzip2", run = "archive" },
{ mime = "application/x-bzip*", run = "archive" },
{ mime = "application/x-7z-compressed", run = "archive" },
{ mime = "application/x-rar", run = "archive" },
{ mime = "application/x-xz", run = "archive" },
Expand Down
30 changes: 19 additions & 11 deletions yazi-core/src/manager/commands/tab_create.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
use yazi_proxy::AppProxy;
use yazi_shared::{event::Cmd, fs::Url, render};

use crate::{manager::Tabs, tab::Tab};

const MAX_TABS: usize = 9;

pub struct Opt {
url: Option<Url>,
url: Url,
current: bool,
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
let mut opt = Self { url: None, current: c.named.contains_key("current") };

if !opt.current {
opt.url = Some(c.take_first().map_or_else(|| Url::from("."), Url::from));
if c.named.contains_key("current") {
Self { url: Default::default(), current: true }
} else {
Self { url: c.take_first().map_or_else(|| Url::from("."), Url::from), current: false }
}
opt
}
}

impl Tabs {
pub fn create(&mut self, opt: impl Into<Opt>) {
if self.items.len() >= MAX_TABS {
AppProxy::notify_warn("Too many tabs", "You can only open up to 9 tabs at the same time.");
return;
}

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::default();
tab.conf = self.active().conf.clone();
tab.apply_files_attrs();
tab.cd(url);

if !opt.current {
tab.cd(opt.url);
} else if let Some(h) = self.active().current.hovered() {
tab.conf = self.active().conf.clone();
tab.apply_files_attrs();
tab.reveal(h.url.to_owned());
} else {
tab.conf = self.active().conf.clone();
tab.apply_files_attrs();
tab.cd(self.active().current.cwd.clone());
}

self.items.insert(self.cursor + 1, tab);
self.set_idx(self.cursor + 1);
Expand Down
4 changes: 3 additions & 1 deletion yazi-core/src/tab/commands/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::tab::Tab;
pub struct Opt {
run: String,
block: bool,
orphan: bool,
confirm: bool,
}

Expand All @@ -17,6 +18,7 @@ impl From<Cmd> for Opt {
Self {
run: c.take_first().unwrap_or_default(),
block: c.named.contains_key("block"),
orphan: c.named.contains_key("orphan"),
confirm: c.named.contains_key("confirm"),
}
}
Expand Down Expand Up @@ -45,7 +47,7 @@ impl Tab {
Cow::Owned(Opener {
run: opt.run,
block: opt.block,
orphan: false,
orphan: opt.orphan,
desc: Default::default(),
for_: None,
spread: true,
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/notify/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> Layout<'a> {
.split(area);

let chunks =
layout::Layout::vertical([Constraint::Max(1), Constraint::Min(1)]).split(chunks[1]);
layout::Layout::vertical([Constraint::Max(1), Constraint::Fill(1)]).split(chunks[1]);

chunks[1]
}
Expand Down
16 changes: 10 additions & 6 deletions yazi-plugin/preset/plugins/fzf.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
local cwd = ya.sync(function() return tostring(cx.active.current.cwd) end)
local state = ya.sync(function() return tostring(cx.active.current.cwd) end)

local function notify(s, ...) ya.notify { title = "Fzf", content = string.format(s, ...), timeout = 5, level = "error" } end
local function fail(s, ...) ya.notify { title = "Fzf", content = string.format(s, ...), timeout = 5, level = "error" } end

local function entry()
local _permit = ya.hide()
local cwd = cwd()
local cwd = state()

local child, err =
Command("fzf"):cwd(cwd):stdin(Command.INHERIT):stdout(Command.PIPED):stderr(Command.INHERIT):spawn()

if not child then
return notify("Spawn `fzf` failed with error code %s. Do you have it installed?", err)
return fail("Spawn `fzf` failed with error code %s. Do you have it installed?", err)
end

local output, err = child:wait_with_output()
if not output then
return notify("`fzf` exited with error code %s", err)
return fail("Cannot read `fzf` output, error code %s", err)
elseif not output.status:success() and output.status:code() ~= 130 then
return fail("`fzf` exited with error code %s", output.status:code())
end

local target = output.stdout:gsub("\n$", "")
ya.manager_emit(target:match("[/\\]$") and "cd" or "reveal", { target })
if target ~= "" then
ya.manager_emit(target:match("[/\\]$") and "cd" or "reveal", { target })
end
end

return { entry = entry }
19 changes: 13 additions & 6 deletions yazi-plugin/preset/plugins/zoxide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end)

local set_state = ya.sync(function(st, empty) st.empty = empty end)

local function notify(s, ...)
local function fail(s, ...)
ya.notify { title = "Zoxide", content = string.format(s, ...), timeout = 5, level = "error" }
end

Expand Down Expand Up @@ -39,6 +39,7 @@ local function setup(_, opts)
"cd",
function()
ya.manager_emit("shell", {
orphan = true,
confirm = true,
"zoxide add " .. ya.quote(tostring(cx.active.current.cwd)),
})
Expand All @@ -50,10 +51,10 @@ end
local function entry()
local st = state()
if st.empty == true then
return notify("No directory history in the database, check out the `zoxide` docs to set it up.")
return fail("No directory history in the database, check out the `zoxide` docs to set it up.")
elseif st.empty == nil and head(st.cwd) < 2 then
set_state(true)
return notify("No directory history in the database, check out the `zoxide` docs to set it up.")
return fail("No directory history in the database, check out the `zoxide` docs to set it up.")
end

local _permit = ya.hide()
Expand All @@ -66,14 +67,20 @@ local function entry()
:spawn()

if not child then
return notify("Spawn `zoxide` failed with error code %s. Do you have it installed?", err)
return fail("Spawn `zoxide` failed with error code %s. Do you have it installed?", err)
end

local output, err = child:wait_with_output()
if not output then
return notify("`zoxide` exited with error code %s", err)
return fail("Cannot read `zoxide` output, error code %s", err)
elseif not output.status:success() and output.status:code() ~= 130 then
return fail("`zoxide` exited with error code %s", output.status:code())
end

local target = output.stdout:gsub("\n$", "")
if target ~= "" then
ya.manager_emit("cd", { output.stdout:gsub("\n$", "") })
end
ya.manager_emit("cd", { output.stdout:gsub("\n$", "") })
end

return { setup = setup, entry = entry }
1 change: 1 addition & 0 deletions yazi-scheduler/src/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Process {
let status = result.unwrap().wait().await?;
if !status.success() {
let content = match status.code() {
Some(130) => return self.succ(id), // Ctrl-C pressed by user
Some(code) => format!("Process exited with status code: {code}"),
None => "Process terminated by signal".to_string(),
};
Expand Down

0 comments on commit 884de41

Please sign in to comment.