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: close confirmation prompts and exit automatically when the ongoing task gone #997

Merged
merged 2 commits into from
May 3, 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
31 changes: 28 additions & 3 deletions yazi-core/src/manager/commands/quit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::time::Duration;

use tokio::{select, time};
use yazi_config::popup::InputCfg;
use yazi_proxy::InputProxy;
use yazi_shared::{emit, event::{Cmd, EventQuit}};
Expand All @@ -19,14 +22,36 @@ impl Manager {
pub fn quit(&self, opt: impl Into<Opt>, tasks: &Tasks) {
let opt = EventQuit { no_cwd_file: opt.into().no_cwd_file, ..Default::default() };

let tasks = tasks.len();
if tasks == 0 {
let ongoing = tasks.ongoing().clone();
let left = ongoing.lock().len();

if left == 0 {
emit!(Quit(opt));
return;
}

tokio::spawn(async move {
let mut result = InputProxy::show(InputCfg::quit(tasks));
let mut i = 0;
let mut result = InputProxy::show(InputCfg::quit(left));
loop {
select! {
_ = time::sleep(Duration::from_millis(100)) => {
i += 1;
if i > 30 { break }
else if ongoing.lock().len() == 0 {
emit!(Quit(opt));
return;
}
}
choice = result.recv() => {
if matches!(choice, Some(Ok(s)) if s == "y" || s == "Y") {
emit!(Quit(opt));
}
return;
}
}
}

if let Some(Ok(choice)) = result.recv().await {
if choice == "y" || choice == "Y" {
emit!(Quit(opt));
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tasks/commands/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::tasks::Tasks;

impl Tasks {
pub fn cancel(&mut self, _: Cmd) {
let id = self.scheduler.ongoing.lock().get_id(self.cursor);
let id = self.ongoing().lock().get_id(self.cursor);
if id.map(|id| self.scheduler.cancel(id)) != Some(true) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/tasks/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ use crate::tasks::Tasks;

impl Tasks {
pub fn inspect(&self, _: Cmd) {
let Some(id) = self.scheduler.ongoing.lock().get_id(self.cursor) else {
let ongoing = self.ongoing().clone();
let Some(id) = ongoing.lock().get_id(self.cursor) else {
return;
};

let scheduler = self.scheduler.clone();
tokio::spawn(async move {
let _permit = HIDER.acquire().await.unwrap();
let (tx, mut rx) = mpsc::unbounded_channel();

let mut buffered = {
let mut ongoing = scheduler.ongoing.lock();
let mut ongoing = ongoing.lock();
let Some(task) = ongoing.get_mut(id) else { return };

task.logger = Some(tx);
Expand All @@ -46,7 +46,7 @@ impl Tasks {
stderr.write_all(b"\r\n").ok();
}
_ = time::sleep(time::Duration::from_millis(500)) => {
if scheduler.ongoing.lock().get(id).is_none() {
if ongoing.lock().get(id).is_none() {
stderr().write_all(b"Task finished, press `q` to quit\r\n").ok();
break;
}
Expand All @@ -60,7 +60,7 @@ impl Tasks {
}
}

if let Some(task) = scheduler.ongoing.lock().get_mut(id) {
if let Some(task) = ongoing.lock().get_mut(id) {
task.logger = None;
}
while answer != b'q' {
Expand Down
8 changes: 4 additions & 4 deletions yazi-core/src/tasks/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{sync::Arc, time::Duration};

use parking_lot::Mutex;
use tokio::{task::JoinHandle, time::sleep};
use yazi_scheduler::{Scheduler, TaskSummary};
use yazi_scheduler::{Ongoing, Scheduler, TaskSummary};
use yazi_shared::{emit, event::Cmd, term::Term, Layer};

use super::{TasksProgress, TASKS_BORDER, TASKS_PADDING, TASKS_PERCENT};
Expand Down Expand Up @@ -56,10 +57,9 @@ impl Tasks {
}

pub fn paginate(&self) -> Vec<TaskSummary> {
let ongoing = self.scheduler.ongoing.lock();
ongoing.values().take(Self::limit()).map(Into::into).collect()
self.ongoing().lock().values().take(Self::limit()).map(Into::into).collect()
}

#[inline]
pub fn len(&self) -> usize { self.scheduler.ongoing.lock().len() }
pub fn ongoing(&self) -> &Arc<Mutex<Ongoing>> { &self.scheduler.ongoing }
}