feat: add shell action `copy` (#926)

pull/927/head
sigoden 2 days ago committed by GitHub
parent 9935ecc12f
commit 98cc9879c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -245,7 +245,7 @@ async fn shell_execute(config: &GlobalConfig, shell: &Shell, mut input: Input) -
return Ok(()); return Ok(());
} }
if *IS_STDOUT_TERMINAL { if *IS_STDOUT_TERMINAL {
let options = ["execute", "revise", "describe", "cancel"]; let options = ["execute", "revise", "describe", "copy", "quit"];
let command = color_text(eval_str.trim(), nu_ansi_term::Color::Rgb(255, 165, 0)); let command = color_text(eval_str.trim(), nu_ansi_term::Color::Rgb(255, 165, 0));
let first_letter_color = nu_ansi_term::Color::Cyan; let first_letter_color = nu_ansi_term::Color::Cyan;
let prompt_text = options let prompt_text = options
@ -257,12 +257,14 @@ async fn shell_execute(config: &GlobalConfig, shell: &Shell, mut input: Input) -
println!("{command}"); println!("{command}");
let answer = Text::new(&format!("{prompt_text}:")) let answer = Text::new(&format!("{prompt_text}:"))
.with_default("e") .with_default("e")
.with_validator(|input: &str| match matches!(input, "e" | "r" | "d" | "c") { .with_validator(
true => Ok(Validation::Valid), |input: &str| match matches!(input, "e" | "r" | "d" | "c" | "q") {
false => Ok(Validation::Invalid( true => Ok(Validation::Valid),
"Invalid option, choice one of e, r, d or c".into(), false => Ok(Validation::Invalid(
)), "Invalid option, choice one of e, r, d, c or q".into(),
}) )),
},
)
.prompt()?; .prompt()?;
match answer.as_str() { match answer.as_str() {
@ -292,6 +294,10 @@ async fn shell_execute(config: &GlobalConfig, shell: &Shell, mut input: Input) -
println!(); println!();
continue; continue;
} }
"c" => {
set_text(&eval_str)?;
println!("{}", dimmed_text("✓ Copied the command"));
}
_ => {} _ => {}
} }
break; break;

@ -588,7 +588,7 @@ Type ".help" for additional help.
fn copy(&self, text: &str) -> Result<()> { fn copy(&self, text: &str) -> Result<()> {
if text.is_empty() { if text.is_empty() {
bail!("Empty text") bail!("No text to copy")
} }
set_text(text)?; set_text(text)?;
Ok(()) Ok(())

@ -9,12 +9,12 @@ pub fn set_text(text: &str) -> anyhow::Result<()> {
let mut clipboard = CLIPBOARD.lock().unwrap(); let mut clipboard = CLIPBOARD.lock().unwrap();
match clipboard.as_mut() { match clipboard.as_mut() {
Some(clipboard) => clipboard.set_text(text)?, Some(clipboard) => clipboard.set_text(text)?,
None => anyhow::bail!("No available clipboard"), None => anyhow::bail!("Failed to copy the text; no available clipboard"),
} }
Ok(()) Ok(())
} }
#[cfg(any(target_os = "android", target_os = "emscripten"))] #[cfg(any(target_os = "android", target_os = "emscripten"))]
pub fn set_text(_text: &str) -> anyhow::Result<()> { pub fn set_text(_text: &str) -> anyhow::Result<()> {
anyhow::bail!("No available clipboard") anyhow::bail!("Failed to copy the text; no available clipboard")
} }

Loading…
Cancel
Save