This commit is contained in:
sxyazi 2023-11-27 22:47:08 +08:00
parent 4dd014b416
commit bab7f09007
No known key found for this signature in database
15 changed files with 95 additions and 68 deletions

View file

@ -2,7 +2,7 @@ use super::{Offset, Position};
use crate::{INPUT, SELECT}; use crate::{INPUT, SELECT};
#[derive(Default)] #[derive(Default)]
pub struct InputOpt { pub struct InputCfg {
pub title: String, pub title: String,
pub value: String, pub value: String,
pub position: Position, pub position: Position,
@ -18,7 +18,7 @@ pub struct SelectCfg {
pub position: Position, pub position: Position,
} }
impl InputOpt { impl InputCfg {
#[inline] #[inline]
pub fn cd() -> Self { pub fn cd() -> Self {
Self { Self {

View file

@ -1,10 +1,9 @@
use std::{collections::BTreeMap, ffi::OsString}; use std::{collections::BTreeMap, ffi::OsString};
use anyhow::Result;
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;
use tokio::sync::{mpsc::{self, UnboundedSender}, oneshot}; use tokio::sync::{mpsc::UnboundedSender, oneshot};
use yazi_config::{open::Opener, popup::InputOpt}; use yazi_config::open::Opener;
use yazi_shared::{fs::Url, term::Term, Exec, InputError, Layer, RoCell}; use yazi_shared::{fs::Url, term::Term, Exec, Layer, RoCell};
use super::files::FilesOp; use super::files::FilesOp;
use crate::{preview::PreviewLock, tasks::TasksProgress}; use crate::{preview::PreviewLock, tasks::TasksProgress};
@ -26,8 +25,7 @@ pub enum Event {
Mimetype(BTreeMap<Url, String>), Mimetype(BTreeMap<Url, String>),
Preview(PreviewLock), Preview(PreviewLock),
// Input // Input(InputOpt, mpsc::UnboundedSender<Result<String, InputError>>),
Input(InputOpt, mpsc::UnboundedSender<Result<String, InputError>>),
// Tasks // Tasks
Open(Vec<(OsString, String)>, Option<Opener>), Open(Vec<(OsString, String)>, Option<Opener>),
@ -82,12 +80,6 @@ macro_rules! emit {
$crate::Event::Preview($lock).emit(); $crate::Event::Preview($lock).emit();
}; };
(Input($opt:expr)) => {{
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
$crate::Event::Input($opt, tx).emit();
rx
}};
(Open($targets:expr, $opener:expr)) => { (Open($targets:expr, $opener:expr)) => {
$crate::Event::Open($targets, $opener).emit(); $crate::Event::Open($targets, $opener).emit();
}; };

View file

@ -10,6 +10,7 @@ mod kill;
mod move_; mod move_;
mod paste; mod paste;
mod redo; mod redo;
mod show;
mod type_; mod type_;
mod undo; mod undo;
mod visual; mod visual;

View file

@ -0,0 +1,56 @@
use anyhow::bail;
use tokio::sync::mpsc;
use yazi_config::popup::InputCfg;
use yazi_shared::{Exec, InputError, Layer};
use crate::{emit, input::Input};
pub struct Opt {
cfg: InputCfg,
tx: mpsc::UnboundedSender<Result<String, InputError>>,
}
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 Input {
pub fn _show(cfg: InputCfg) -> mpsc::UnboundedReceiver<Result<String, InputError>> {
let (tx, rx) = mpsc::unbounded_channel();
emit!(Call(Exec::call("show", vec![]).with_data(Opt { cfg, tx }).vec(), Layer::Input));
rx
}
pub fn show(&mut self, opt: impl TryInto<Opt>) -> bool {
let Ok(opt) = opt.try_into() else {
return false;
};
self.close(false);
self.visible = true;
self.title = opt.cfg.title;
self.position = opt.cfg.position;
// Typing
self.callback = Some(opt.tx);
self.realtime = opt.cfg.realtime;
self.completion = opt.cfg.completion;
// Shell
self.highlight = opt.cfg.highlight;
// Reset snaps
self.snaps.reset(opt.cfg.value, self.limit());
true
}
}

View file

@ -2,7 +2,7 @@ use std::ops::Range;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use yazi_config::{popup::{InputOpt, Position}, INPUT}; use yazi_config::{popup::Position, INPUT};
use yazi_shared::InputError; use yazi_shared::InputError;
use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps}; use super::{mode::InputMode, op::InputOp, InputSnap, InputSnaps};
@ -19,7 +19,7 @@ pub struct Input {
// Typing // Typing
pub(super) callback: Option<UnboundedSender<Result<String, InputError>>>, pub(super) callback: Option<UnboundedSender<Result<String, InputError>>>,
realtime: bool, pub(super) realtime: bool,
pub(super) completion: bool, pub(super) completion: bool,
// Shell // Shell
@ -27,24 +27,6 @@ pub struct Input {
} }
impl Input { impl Input {
pub fn show(&mut self, opt: InputOpt, tx: UnboundedSender<Result<String, InputError>>) {
self.close(false);
self.visible = true;
self.title = opt.title;
self.position = opt.position;
// Typing
self.callback = Some(tx);
self.realtime = opt.realtime;
self.completion = opt.completion;
// Shell
self.highlight = opt.highlight;
// Reset snaps
self.snaps.reset(opt.value, self.limit());
}
#[inline] #[inline]
pub(super) fn limit(&self) -> usize { pub(super) fn limit(&self) -> usize {
self.position.offset.width.saturating_sub(INPUT.border()) as usize self.position.offset.width.saturating_sub(INPUT.border()) as usize

View file

@ -1,10 +1,10 @@
use std::path::{PathBuf, MAIN_SEPARATOR}; use std::path::{PathBuf, MAIN_SEPARATOR};
use tokio::fs; use tokio::fs;
use yazi_config::popup::InputOpt; use yazi_config::popup::InputCfg;
use yazi_shared::{fs::Url, Exec}; use yazi_shared::{fs::Url, Exec};
use crate::{emit, files::{File, FilesOp}, manager::Manager}; use crate::{emit, files::{File, FilesOp}, input::Input, manager::Manager};
pub struct Opt { pub struct Opt {
force: bool, force: bool,
@ -19,14 +19,14 @@ impl Manager {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
let cwd = self.cwd().to_owned(); let cwd = self.cwd().to_owned();
tokio::spawn(async move { tokio::spawn(async move {
let mut result = emit!(Input(InputOpt::create())); let mut result = Input::_show(InputCfg::create());
let Some(Ok(name)) = result.recv().await else { let Some(Ok(name)) = result.recv().await else {
return Ok(()); return Ok(());
}; };
let path = cwd.join(&name); let path = cwd.join(&name);
if !opt.force && fs::symlink_metadata(&path).await.is_ok() { if !opt.force && fs::symlink_metadata(&path).await.is_ok() {
match emit!(Input(InputOpt::overwrite())).recv().await { match Input::_show(InputCfg::overwrite()).recv().await {
Some(Ok(c)) if c == "y" || c == "Y" => (), Some(Ok(c)) if c == "y" || c == "Y" => (),
_ => return Ok(()), _ => return Ok(()),
} }

View file

@ -1,7 +1,7 @@
use yazi_config::popup::InputOpt; use yazi_config::popup::InputCfg;
use yazi_shared::Exec; use yazi_shared::Exec;
use crate::{emit, manager::Manager, tasks::Tasks}; use crate::{emit, input::Input, manager::Manager, tasks::Tasks};
#[derive(Default)] #[derive(Default)]
pub struct Opt { pub struct Opt {
@ -25,7 +25,7 @@ impl Manager {
} }
tokio::spawn(async move { tokio::spawn(async move {
let mut result = emit!(Input(InputOpt::quit(tasks))); let mut result = Input::_show(InputCfg::quit(tasks));
if let Some(Ok(choice)) = result.recv().await { if let Some(Ok(choice)) = result.recv().await {
if choice == "y" || choice == "Y" { if choice == "y" || choice == "Y" {
emit!(Quit(opt.no_cwd_file)); emit!(Quit(opt.no_cwd_file));

View file

@ -2,10 +2,10 @@ use std::{collections::BTreeMap, ffi::OsStr, io::{stdout, BufWriter, Write}, pat
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}}; use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}};
use yazi_config::{popup::InputOpt, OPEN, PREVIEW}; use yazi_config::{popup::InputCfg, OPEN, PREVIEW};
use yazi_shared::{fs::{max_common_root, Url}, term::Term, Defer, Exec}; use yazi_shared::{fs::{max_common_root, Url}, term::Term, Defer, Exec};
use crate::{emit, external::{self, ShellOpt}, files::{File, FilesOp}, manager::Manager, Event, BLOCKER}; use crate::{emit, external::{self, ShellOpt}, files::{File, FilesOp}, input::Input, manager::Manager, Event, BLOCKER};
pub struct Opt { pub struct Opt {
force: bool, force: bool,
@ -39,7 +39,7 @@ impl Manager {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
tokio::spawn(async move { tokio::spawn(async move {
let mut result = let mut result =
emit!(Input(InputOpt::rename().with_value(hovered.file_name().unwrap().to_string_lossy()))); Input::_show(InputCfg::rename().with_value(hovered.file_name().unwrap().to_string_lossy()));
let Some(Ok(name)) = result.recv().await else { let Some(Ok(name)) = result.recv().await else {
return; return;
@ -51,7 +51,7 @@ impl Manager {
return; return;
} }
let mut result = emit!(Input(InputOpt::overwrite())); let mut result = Input::_show(InputCfg::overwrite());
if let Some(Ok(choice)) = result.recv().await { if let Some(Ok(choice)) = result.recv().await {
if choice == "y" || choice == "Y" { if choice == "y" || choice == "Y" {
Self::rename_and_hover(hovered, Url::from(new)).await.ok(); Self::rename_and_hover(hovered, Url::from(new)).await.ok();

View file

@ -2,10 +2,10 @@ use std::{mem, time::Duration};
use tokio::{fs, pin}; use tokio::{fs, pin};
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
use yazi_config::popup::InputOpt; use yazi_config::popup::InputCfg;
use yazi_shared::{fs::{expand_path, Url}, Debounce, Exec, InputError, Layer}; use yazi_shared::{fs::{expand_path, Url}, Debounce, Exec, InputError, Layer};
use crate::{completion::Completion, emit, manager::Manager, tab::Tab}; use crate::{completion::Completion, emit, input::Input, manager::Manager, tab::Tab};
pub struct Opt { pub struct Opt {
target: Url, target: Url,
@ -72,7 +72,7 @@ impl Tab {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
tokio::spawn(async move { tokio::spawn(async move {
let rx = emit!(Input(InputOpt::cd().with_value(opt.target.to_string_lossy()))); let rx = Input::_show(InputCfg::cd().with_value(opt.target.to_string_lossy()));
let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50)); let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50));
pin!(rx); pin!(rx);

View file

@ -2,10 +2,10 @@ use std::time::Duration;
use tokio::pin; use tokio::pin;
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
use yazi_config::popup::InputOpt; use yazi_config::popup::InputCfg;
use yazi_shared::{Debounce, Exec, InputError, Layer}; use yazi_shared::{Debounce, Exec, InputError, Layer};
use crate::{emit, tab::{Finder, FinderCase, Tab}}; use crate::{emit, input::Input, tab::{Finder, FinderCase, Tab}};
pub struct Opt<'a> { pub struct Opt<'a> {
query: Option<&'a str>, query: Option<&'a str>,
@ -39,7 +39,7 @@ impl Tab {
pub fn find<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool { pub fn find<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
tokio::spawn(async move { tokio::spawn(async move {
let rx = emit!(Input(InputOpt::find(opt.prev))); let rx = Input::_show(InputCfg::find(opt.prev));
let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50)); let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50));
pin!(rx); pin!(rx);

View file

@ -3,10 +3,10 @@ use std::{mem, time::Duration};
use anyhow::bail; use anyhow::bail;
use tokio::pin; use tokio::pin;
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
use yazi_config::popup::InputOpt; use yazi_config::popup::InputCfg;
use yazi_shared::Exec; use yazi_shared::Exec;
use crate::{emit, external, files::FilesOp, manager::Manager, tab::Tab}; use crate::{emit, external, files::FilesOp, input::Input, manager::Manager, tab::Tab};
pub struct Opt { pub struct Opt {
pub type_: OptType, pub type_: OptType,
@ -46,7 +46,7 @@ impl Tab {
let hidden = self.conf.show_hidden; let hidden = self.conf.show_hidden;
self.search = Some(tokio::spawn(async move { self.search = Some(tokio::spawn(async move {
let Some(Ok(subject)) = emit!(Input(InputOpt::search())).recv().await else { bail!("") }; let Some(Ok(subject)) = Input::_show(InputCfg::search()).recv().await else { bail!("") };
cwd = cwd.into_search(subject.clone()); cwd = cwd.into_search(subject.clone());
let rx = if opt.type_ == OptType::Rg { let rx = if opt.type_ == OptType::Rg {

View file

@ -1,7 +1,7 @@
use yazi_config::{open::Opener, popup::InputOpt}; use yazi_config::{open::Opener, popup::InputCfg};
use yazi_shared::Exec; use yazi_shared::Exec;
use crate::{emit, tab::Tab}; use crate::{emit, input::Input, tab::Tab};
pub struct Opt { pub struct Opt {
cmd: String, cmd: String,
@ -30,7 +30,7 @@ impl Tab {
let mut opt = opt.into() as Opt; let mut opt = opt.into() as Opt;
tokio::spawn(async move { tokio::spawn(async move {
if !opt.confirm || opt.cmd.is_empty() { if !opt.confirm || opt.cmd.is_empty() {
let mut result = emit!(Input(InputOpt::shell(opt.block).with_value(opt.cmd))); let mut result = Input::_show(InputCfg::shell(opt.block).with_value(opt.cmd));
match result.recv().await { match result.recv().await {
Some(Ok(e)) => opt.cmd = e, Some(Ok(e)) => opt.cmd = e,
_ => return, _ => return,

View file

@ -2,11 +2,11 @@ use std::{collections::{BTreeMap, HashMap, HashSet}, ffi::OsStr, path::Path, syn
use serde::Serialize; use serde::Serialize;
use tracing::debug; use tracing::debug;
use yazi_config::{manager::SortBy, open::Opener, popup::InputOpt, OPEN}; use yazi_config::{manager::SortBy, open::Opener, popup::InputCfg, OPEN};
use yazi_shared::{fs::Url, term::Term, MimeKind}; use yazi_shared::{fs::Url, term::Term, MimeKind};
use super::{running::Running, task::TaskSummary, Scheduler, TASKS_PADDING, TASKS_PERCENT}; use super::{running::Running, task::TaskSummary, Scheduler, TASKS_PADDING, TASKS_PERCENT};
use crate::{emit, files::{File, Files}}; use crate::{files::{File, Files}, input::Input};
pub struct Tasks { pub struct Tasks {
pub(super) scheduler: Arc<Scheduler>, pub(super) scheduler: Arc<Scheduler>,
@ -110,11 +110,11 @@ impl Tasks {
let scheduler = self.scheduler.clone(); let scheduler = self.scheduler.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut result = emit!(Input(if permanently { let mut result = Input::_show(if permanently {
InputOpt::delete(targets.len()) InputCfg::delete(targets.len())
} else { } else {
InputOpt::trash(targets.len()) InputCfg::trash(targets.len())
})); });
if let Some(Ok(choice)) = result.recv().await { if let Some(Ok(choice)) = result.recv().await {
if choice != "y" && choice != "Y" { if choice != "y" && choice != "Y" {

View file

@ -177,11 +177,6 @@ impl App {
} }
} }
Event::Input(opt, tx) => {
self.cx.input.show(opt, tx);
emit!(Render);
}
Event::Open(targets, opener) => { Event::Open(targets, opener) => {
if let Some(p) = &BOOT.chooser_file { if let Some(p) = &BOOT.chooser_file {
let paths = targets.into_iter().fold(OsString::new(), |mut s, (p, _)| { let paths = targets.into_iter().fold(OsString::new(), |mut s, (p, _)| {

View file

@ -207,6 +207,7 @@ impl<'a> Executor<'a> {
}; };
} }
on!(show);
on!(close); on!(close);
on!(escape); on!(escape);
on!(move_, "move"); on!(move_, "move");