feat: confirm on quitting

This commit is contained in:
sxyazi 2023-07-14 11:57:08 +08:00
parent 2279d95fab
commit 43ec2ada0b
No known key found for this signature in database
4 changed files with 44 additions and 21 deletions

View File

@ -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<PathBuf>) { &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 {

View File

@ -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,

View File

@ -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() }
}

View File

@ -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" => {