From 43ec2ada0b5381d35263063c775650ea2b4afb67 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 14 Jul 2023 11:57:08 +0800 Subject: [PATCH] feat: confirm on quitting --- src/core/manager/manager.rs | 42 ++++++++++++++++++++++++++++--------- src/core/manager/tab.rs | 3 +-- src/core/tasks/tasks.rs | 11 +++++++--- src/ui/dispatcher.rs | 9 +++----- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/core/manager/manager.rs b/src/core/manager/manager.rs index cec661d0..1604f545 100644 --- a/src/core/manager/manager.rs +++ b/src/core/manager/manager.rs @@ -3,7 +3,7 @@ use std::{collections::{BTreeSet, HashMap, HashSet}, mem, path::PathBuf}; use tokio::fs; use super::{PreviewData, Tab, Tabs, Watcher}; -use crate::{core::{files::{File, FilesOp}, input::{Input, InputOpt, InputPos}, manager::Folder, tasks::Precache}, emit}; +use crate::{core::{files::{File, FilesOp}, input::{InputOpt, InputPos}, manager::Folder, tasks::{Precache, Tasks}}, emit}; pub struct Manager { tabs: Tabs, @@ -72,15 +72,6 @@ impl Manager { false } - pub fn close(&mut self) -> bool { - if self.tabs.len() > 1 { - return self.tabs.close(self.tabs.idx()); - } - - emit!(Quit); - return false; - } - pub fn yank(&mut self, cut: bool) -> bool { self.yanked.0 = cut; self.yanked.1.clear(); @@ -91,6 +82,37 @@ impl Manager { #[inline] pub fn yanked(&self) -> &(bool, HashSet) { &self.yanked } + pub fn quit(&self, tasks: &Tasks) -> bool { + let tasks = tasks.len(); + if tasks == 0 { + emit!(Quit); + return false; + } + + tokio::spawn(async move { + let result = emit!(Input(InputOpt { + title: format!("There are {} tasks running, sure to quit? (y/N)", tasks), + value: "".to_string(), + position: InputPos::Top, + })) + .await; + + if let Ok(choice) = result { + if choice.to_lowercase() == "y" { + emit!(Quit); + } + } + }); + false + } + + pub fn close(&mut self, tasks: &Tasks) -> bool { + if self.tabs.len() > 1 { + return self.tabs.close(self.tabs.idx()); + } + self.quit(tasks) + } + pub fn create(&self) -> bool { let cwd = self.current().cwd.clone(); tokio::spawn(async move { diff --git a/src/core/manager/tab.rs b/src/core/manager/tab.rs index d8645045..b5484802 100644 --- a/src/core/manager/tab.rs +++ b/src/core/manager/tab.rs @@ -2,10 +2,9 @@ use std::{collections::BTreeMap, mem, path::{Path, PathBuf}}; use anyhow::{Error, Result}; use tokio::task::JoinHandle; -use tracing::info; use super::{Folder, Mode, Preview}; -use crate::{core::{external::{self, FzfOpt, ZoxideOpt}, files::{File, Files, FilesOp}, input::{Input, InputOpt, InputPos}, Event, BLOCKER}, emit, misc::Defer}; +use crate::{core::{external::{self, FzfOpt, ZoxideOpt}, files::{File, Files, FilesOp}, input::{InputOpt, InputPos}, Event, BLOCKER}, emit, misc::Defer}; pub struct Tab { pub(super) current: Folder, diff --git a/src/core/tasks/tasks.rs b/src/core/tasks/tasks.rs index cf4e9155..502dac62 100644 --- a/src/core/tasks/tasks.rs +++ b/src/core/tasks/tasks.rs @@ -3,7 +3,7 @@ use std::{collections::{BTreeMap, HashSet}, path::PathBuf, sync::Arc}; use tracing::trace; use super::{Scheduler, TASKS_PADDING, TASKS_PERCENT}; -use crate::{config::OPEN, core::input::{Input, InputOpt, InputPos}, emit, misc::tty_size}; +use crate::{config::OPEN, core::input::{InputOpt, InputPos}, emit, misc::tty_size}; #[derive(Clone, Debug)] pub struct Task { @@ -71,7 +71,7 @@ impl Tasks { } pub fn next(&mut self) -> bool { - let limit = Self::limit().min(self.scheduler.running.read().len()); + let limit = Self::limit().min(self.len()); let old = self.cursor; self.cursor = limit.saturating_sub(1).min(self.cursor + 1); @@ -151,7 +151,7 @@ impl Tasks { let scheduler = self.scheduler.clone(); tokio::spawn(async move { let result = emit!(Input(InputOpt { - title: "Are you sure delete these files? (Y/n)".to_string(), + title: "Are you sure delete these files? (y/N)".to_string(), value: "".to_string(), position: InputPos::Hovered, })) @@ -182,3 +182,8 @@ impl Tasks { true } } + +impl Tasks { + #[inline] + pub fn len(&self) -> usize { self.scheduler.running.read().len() } +} diff --git a/src/ui/dispatcher.rs b/src/ui/dispatcher.rs index de922ee0..9c0f753d 100644 --- a/src/ui/dispatcher.rs +++ b/src/ui/dispatcher.rs @@ -1,7 +1,7 @@ use crossterm::event::KeyCode; use super::Ctx; -use crate::{config::{keymap::{Exec, Key, Single}, KEYMAP}, core::input::InputMode, emit, misc::optinal_bool}; +use crate::{config::{keymap::{Exec, Key, Single}, KEYMAP}, core::input::InputMode, misc::optinal_bool}; pub struct Executor; @@ -45,11 +45,8 @@ impl Executor { fn manager(cx: &mut Ctx, exec: &Exec) -> bool { match exec.cmd.as_str() { "escape" => cx.manager.active_mut().escape(), - "quit" => { - emit!(Quit); - false - } - "close" => cx.manager.close(), + "quit" => cx.manager.quit(&cx.tasks), + "close" => cx.manager.close(&cx.tasks), // Navigation "arrow" => {