feat: allow quitting the app with escape when there's no selection

This commit is contained in:
Jan Buchar 2024-05-15 17:40:50 +02:00
parent 28dfe728ab
commit a11e971d85

View file

@ -1,16 +1,21 @@
use bitflags::bitflags; use bitflags::bitflags;
use yazi_proxy::{AppProxy, ManagerProxy}; use yazi_proxy::{AppProxy, ManagerProxy};
use yazi_shared::{event::Cmd, render, render_and}; use yazi_shared::{
event::{Cmd, Event, EventQuit},
render, render_and,
};
use crate::tab::Tab; use crate::tab::Tab;
bitflags! { bitflags! {
pub struct Opt: u8 { pub struct Opt: u8 {
const FIND = 0b00001; const FIND = 0b0000001;
const VISUAL = 0b00010; const VISUAL = 0b0000010;
const SELECT = 0b00100; const SELECT = 0b0000100;
const FILTER = 0b01000; const FILTER = 0b0001000;
const SEARCH = 0b10000; const SEARCH = 0b0010000;
const QUIT = 0b0100000;
const CASCADE = 0b1000000;
} }
} }
@ -18,12 +23,14 @@ impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { fn from(c: Cmd) -> Self {
c.args.iter().fold(Opt::empty(), |acc, (k, v)| { c.args.iter().fold(Opt::empty(), |acc, (k, v)| {
match (k.as_str(), v.as_bool().unwrap_or(false)) { match (k.as_str(), v.as_bool().unwrap_or(false)) {
("all", true) => Self::all(), ("all", true) => acc | (Self::all() ^ Self::QUIT ^ Self::CASCADE),
("find", true) => acc | Self::FIND, ("find", true) => acc | Self::FIND,
("visual", true) => acc | Self::VISUAL, ("visual", true) => acc | Self::VISUAL,
("select", true) => acc | Self::SELECT, ("select", true) => acc | Self::SELECT,
("filter", true) => acc | Self::FILTER, ("filter", true) => acc | Self::FILTER,
("search", true) => acc | Self::SEARCH, ("search", true) => acc | Self::SEARCH,
("quit", true) => acc | Self::QUIT,
("cascade", true) => acc | Self::CASCADE,
_ => acc, _ => acc,
} }
}) })
@ -33,29 +40,36 @@ impl From<Cmd> for Opt {
impl Tab { impl Tab {
pub fn escape(&mut self, opt: impl Into<Opt>) { pub fn escape(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
if opt.is_empty() { if opt.is_empty() || opt.contains(Opt::CASCADE) {
_ = self.escape_find() _ = self.escape_find()
|| self.escape_visual() || self.escape_visual()
|| self.escape_select() || self.escape_select()
|| self.escape_filter() || self.escape_filter()
|| self.escape_search(); || self.escape_search()
|| (opt.contains(Opt::QUIT) && self.quit());
return; return;
} }
let mut matched = false;
if opt.contains(Opt::FIND) { if opt.contains(Opt::FIND) {
self.escape_find(); matched |= self.escape_find();
} }
if opt.contains(Opt::VISUAL) { if opt.contains(Opt::VISUAL) {
self.escape_visual(); matched |= self.escape_visual();
} }
if opt.contains(Opt::SELECT) { if opt.contains(Opt::SELECT) {
self.escape_select(); matched |= self.escape_select();
} }
if opt.contains(Opt::FILTER) { if opt.contains(Opt::FILTER) {
self.escape_filter(); matched |= self.escape_filter();
} }
if opt.contains(Opt::SEARCH) { if opt.contains(Opt::SEARCH) {
self.escape_search(); matched |= self.escape_search();
}
if !matched && opt.contains(Opt::QUIT) {
self.quit();
} }
} }
@ -123,4 +137,9 @@ impl Tab {
true true
} }
pub fn quit(&mut self) -> bool {
Event::Quit(EventQuit { no_cwd_file: false, selected: None }).emit();
true
}
} }