feat: add cursor option to the shell command (#1422)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
HE7086 2024-08-09 12:12:28 +02:00 committed by GitHub
parent 5f923e8706
commit 3bb867c5bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 13 deletions

View file

@ -1,8 +1,9 @@
use std::borrow::Cow;
use std::{borrow::Cow, fmt::Display};
use anyhow::bail;
use yazi_config::{open::Opener, popup::InputCfg};
use yazi_proxy::{AppProxy, InputProxy, TasksProxy};
use yazi_shared::event::Cmd;
use yazi_shared::event::{Cmd, Data};
use crate::tab::Tab;
@ -12,27 +13,40 @@ pub struct Opt {
orphan: bool,
confirm: bool,
interactive: bool,
cursor: Option<usize>,
}
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
impl TryFrom<Cmd> for Opt {
type Error = anyhow::Error;
fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
let me = Self {
run: c.take_first_str().unwrap_or_default(),
block: c.bool("block"),
orphan: c.bool("orphan"),
confirm: c.bool("confirm"),
interactive: c.bool("interactive"),
cursor: c.get("cursor").and_then(Data::as_usize),
};
if me.cursor.is_some_and(|c| c > me.run.chars().count()) {
bail!("The cursor position is out of bounds.");
}
Ok(me)
}
}
impl Tab {
pub fn shell(&mut self, opt: impl Into<Opt>) {
pub fn shell(&mut self, opt: impl TryInto<Opt, Error = impl Display>) {
if !self.try_escape_visual() {
return;
}
let mut opt = opt.into() as Opt;
let mut opt = match opt.try_into() {
Ok(o) => o as Opt,
Err(e) => return AppProxy::notify_warn("`shell` command", e),
};
// TODO: Remove in v0.3.2
if !opt.interactive && !opt.confirm {
@ -52,10 +66,10 @@ Please replace e.g. `shell` with `shell --interactive`, `shell "my-template"` wi
}
let selected = self.hovered_and_selected(true).cloned().collect();
tokio::spawn(async move {
if !opt.confirm || opt.run.is_empty() {
let mut result = InputProxy::show(InputCfg::shell(opt.block).with_value(opt.run));
let mut result =
InputProxy::show(InputCfg::shell(opt.block).with_value(opt.run).with_cursor(opt.cursor));
match result.recv().await {
Some(Ok(e)) => opt.run = e,
_ => return,

View file

@ -26,11 +26,11 @@ impl AppProxy {
}
#[inline]
pub fn notify_warn(title: &str, content: &str) {
pub fn notify_warn(title: &str, content: impl ToString) {
emit!(Call(
Cmd::new("notify").with_any("option", NotifyOpt {
title: title.to_owned(),
content: content.to_owned(),
content: content.to_string(),
level: NotifyLevel::Warn,
timeout: Duration::from_secs(5),
}),
@ -39,11 +39,11 @@ impl AppProxy {
}
#[inline]
pub fn notify_error(title: &str, content: &str) {
pub fn notify_error(title: &str, content: impl ToString) {
emit!(Call(
Cmd::new("notify").with_any("option", NotifyOpt {
title: title.to_owned(),
content: content.to_owned(),
content: content.to_string(),
level: NotifyLevel::Error,
timeout: Duration::from_secs(10),
}),