Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Nov 27, 2023
1 parent dee464d commit 4dd014b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 39 deletions.
6 changes: 3 additions & 3 deletions yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ impl Control {
.exec
.iter()
.map(|e| Exec {
cmd: e.cmd.clone(),
args: e.args.clone(),
cmd: e.cmd.clone(),
args: e.args.clone(),
named: e.named.clone(),
data: None,
..Default::default()
})
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-config/src/popup/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct InputOpt {
}

#[derive(Default)]
pub struct SelectOpt {
pub struct SelectCfg {
pub title: String,
pub items: Vec<String>,
pub position: Position,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl InputOpt {
}
}

impl SelectOpt {
impl SelectCfg {
#[inline]
fn max_height(len: usize) -> u16 {
SELECT.open_offset.height.min(SELECT.border().saturating_add(len as u16))
Expand Down
11 changes: 3 additions & 8 deletions yazi-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{collections::BTreeMap, ffi::OsString};
use anyhow::Result;
use crossterm::event::KeyEvent;
use tokio::sync::{mpsc::{self, UnboundedSender}, oneshot};
use yazi_config::{open::Opener, popup::{InputOpt, SelectOpt}};
use yazi_shared::{fs::Url, Exec, InputError, Layer, RoCell};
use yazi_config::{open::Opener, popup::InputOpt};
use yazi_shared::{fs::Url, term::Term, Exec, InputError, Layer, RoCell};

use super::files::FilesOp;
use crate::{preview::PreviewLock, tasks::TasksProgress};
Expand All @@ -27,7 +27,6 @@ pub enum Event {
Preview(PreviewLock),

// Input
Select(SelectOpt, oneshot::Sender<Result<usize>>),
Input(InputOpt, mpsc::UnboundedSender<Result<String, InputError>>),

// Tasks
Expand All @@ -44,7 +43,7 @@ impl Event {

pub async fn wait<T>(self, rx: oneshot::Receiver<T>) -> T {
TX.send(self).ok();
rx.await.unwrap_or_else(|_| std::process::exit(0))
rx.await.unwrap_or_else(|_| Term::goodbye(|| false))
}
}

Expand Down Expand Up @@ -83,10 +82,6 @@ macro_rules! emit {
$crate::Event::Preview($lock).emit();
};

(Select($opt:expr)) => {{
let (tx, rx) = tokio::sync::oneshot::channel();
$crate::Event::Select($opt, tx).wait(rx)
}};
(Input($opt:expr)) => {{
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
$crate::Event::Input($opt, tx).emit();
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/manager/commands/open.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::ffi::OsString;

use yazi_config::{popup::SelectOpt, OPEN};
use yazi_config::{popup::SelectCfg, OPEN};
use yazi_shared::{Exec, MIME_DIR};

use crate::{emit, external, manager::Manager};
use crate::{emit, external, manager::Manager, select::Select};

pub struct Opt {
interactive: bool,
Expand All @@ -20,7 +20,7 @@ impl Manager {
return;
}

let result = emit!(Select(SelectOpt::open(openers.iter().map(|o| o.desc.clone()).collect())));
let result = Select::_show(SelectCfg::open(openers.iter().map(|o| o.desc.clone()).collect()));
if let Ok(choice) = result.await {
emit!(Open(files, Some(openers[choice].clone())));
}
Expand Down
1 change: 1 addition & 0 deletions yazi-core/src/select/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod arrow;
mod close;
mod show;
48 changes: 48 additions & 0 deletions yazi-core/src/select/commands/show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anyhow::{bail, Result};
use tokio::sync::oneshot;
use yazi_config::popup::SelectCfg;
use yazi_shared::{term::Term, Exec, Layer};

use crate::{emit, select::Select};

pub struct Opt {
cfg: SelectCfg,
tx: oneshot::Sender<Result<usize>>,
}

impl TryFrom<&Exec> for Opt {
type Error = anyhow::Error;

fn try_from(e: &Exec) -> Result<Self, Self::Error> {
let Some(data) = e.data.borrow_mut().take() else {
bail!("missing data");
};
let Ok(opt) = data.downcast::<Opt>() else {
bail!("invalid data");
};
Ok(*opt)
}
}

impl Select {
pub async fn _show(cfg: SelectCfg) -> Result<usize> {
let (tx, rx) = oneshot::channel();
emit!(Call(Exec::call("show", vec![]).with_data(Opt { cfg, tx }).vec(), Layer::Select));
rx.await.unwrap_or_else(|_| Term::goodbye(|| false))
}

pub fn show(&mut self, opt: impl TryInto<Opt>) -> bool {
let Ok(opt) = opt.try_into() else {
return false;
};

self.close(false);
self.title = opt.cfg.title;
self.items = opt.cfg.items;
self.position = opt.cfg.position;

self.callback = Some(opt.tx);
self.visible = true;
true
}
}
15 changes: 2 additions & 13 deletions yazi-core/src/select/select.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use tokio::sync::oneshot::Sender;
use yazi_config::{popup::{Position, SelectOpt}, SELECT};
use yazi_config::{popup::Position, SELECT};

#[derive(Default)]
pub struct Select {
title: String,
pub(super) title: String,
pub(super) items: Vec<String>,
pub position: Position,

Expand All @@ -16,17 +16,6 @@ pub struct Select {
}

impl Select {
pub fn show(&mut self, opt: SelectOpt, tx: Sender<Result<usize>>) {
self.close(false);

self.title = opt.title;
self.items = opt.items;
self.position = opt.position;

self.callback = Some(tx);
self.visible = true;
}

#[inline]
pub fn window(&self) -> &[String] {
let end = (self.offset + self.limit()).min(self.items.len());
Expand Down
6 changes: 1 addition & 5 deletions yazi-fm/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl App {
let cwd = self.cx.manager.cwd().as_os_str();
std::fs::write(p, cwd.as_encoded_bytes()).ok();
}
Term::goodbye(|| false).unwrap();
Term::goodbye(|| false);
}

fn dispatch_key(&mut self, key: KeyEvent) {
Expand Down Expand Up @@ -177,10 +177,6 @@ impl App {
}
}

Event::Select(opt, tx) => {
self.cx.select.show(opt, tx);
emit!(Render);
}
Event::Input(opt, tx) => {
self.cx.input.show(opt, tx);
emit!(Render);
Expand Down
1 change: 1 addition & 0 deletions yazi-fm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl<'a> Executor<'a> {
};
}

on!(show);
on!(close);
on!(arrow);

Expand Down
10 changes: 8 additions & 2 deletions yazi-shared/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{any::Any, collections::BTreeMap, fmt::{self, Display}};
use std::{any::Any, cell::RefCell, collections::BTreeMap, fmt::{self, Display}};

#[derive(Debug, Default)]
pub struct Exec {
pub cmd: String,
pub args: Vec<String>,
pub named: BTreeMap<String, String>,
pub data: Option<Box<dyn Any + Send>>,
pub data: RefCell<Option<Box<dyn Any + Send>>>,
}

impl Exec {
Expand Down Expand Up @@ -35,6 +35,12 @@ impl Exec {
}
self
}

#[inline]
pub fn with_data(mut self, data: impl Any + Send) -> Self {
self.data = RefCell::new(Some(Box::new(data)));
self
}
}

impl Display for Exec {
Expand Down
7 changes: 4 additions & 3 deletions yazi-shared/src/term/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Term {
Ok(disable_raw_mode()?)
}

pub fn goodbye(f: impl FnOnce() -> bool) -> Result<()> {
pub fn goodbye(f: impl FnOnce() -> bool) -> ! {
execute!(
stdout(),
PopKeyboardEnhancementFlags,
Expand All @@ -53,8 +53,9 @@ impl Term {
LeaveAlternateScreen,
crossterm::cursor::SetCursorStyle::DefaultUserShape,
crossterm::cursor::Show,
)?;
disable_raw_mode()?;
)
.ok();
disable_raw_mode().ok();
std::process::exit(f() as i32);
}

Expand Down

0 comments on commit 4dd014b

Please sign in to comment.