Skip to content

Commit

Permalink
feat: close confirmation prompts and exit automatically when the ongo…
Browse files Browse the repository at this point in the history
…ing task gone (#997)
  • Loading branch information
sxyazi authored May 3, 2024
1 parent 2fdc0dd commit 0e26f5d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
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 }
}

0 comments on commit 0e26f5d

Please sign in to comment.