finish paste function

This commit is contained in:
Cai BingJun 2023-02-03 17:22:19 +08:00
parent 82be7cf5c2
commit 4b03dcd280
3 changed files with 56 additions and 113 deletions

86
Cargo.lock generated
View file

@ -55,12 +55,6 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]]
name = "cc"
version = "1.0.78"
@ -73,28 +67,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clipboard"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a904646c0340239dcf7c51677b33928bf24fdf424b79a57909c0109075b2e7"
dependencies = [
"clipboard-win",
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard",
]
[[package]]
name = "clipboard-win"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
dependencies = [
"winapi",
]
[[package]]
name = "crossterm"
version = "0.25.0"
@ -146,7 +118,6 @@ name = "erg_common"
version = "0.6.3"
dependencies = [
"backtrace-on-stack-overflow",
"clipboard",
"crossterm",
"hermit-abi",
"libc",
@ -247,15 +218,6 @@ dependencies = [
"url",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [
"libc",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -305,35 +267,6 @@ dependencies = [
"memoffset",
]
[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]]
name = "object"
version = "0.30.2"
@ -644,22 +577,3 @@ name = "windows_x86_64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "x11-clipboard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89bd49c06c9eb5d98e6ba6536cf64ac9f7ee3a009b2f53996d405b3944f6bcea"
dependencies = [
"xcb",
]
[[package]]
name = "xcb"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
dependencies = [
"libc",
"log",
]

View file

@ -20,7 +20,7 @@ large_thread = []
els = []
py_compatible = []
no_std = []
full-repl = ["crossterm","clipboard"]
full-repl = ["crossterm"]
[target.'cfg(unix)'.dependencies]
libc = { version = "0.2", default-features = false }
@ -35,9 +35,5 @@ features = ["consoleapi"]
optional = true
version = "0.25.0"
[dependencies.clipboard]
optional = true
version = "0.5"
[lib]
path = "lib.rs"

View file

@ -5,10 +5,9 @@ use std::thread::LocalKey;
use std::io::{stdin, BufRead, BufReader};
#[cfg(feature = "full-repl")]
use clipboard::{ClipboardContext, ClipboardProvider};
use std::process::Command;
#[cfg(feature = "full-repl")]
use std::error::Error;
use std::process::Output;
#[cfg(feature = "full-repl")]
use crossterm::{
cursor::{CursorShape, MoveToColumn, SetCursorShape},
@ -41,25 +40,52 @@ pub struct StdinReader {
}
impl StdinReader {
#[cfg(feature = "full-repl")]
fn access_clipboard() -> String {
let mut context = ClipboardProvider::new().unwrap();
if let Some(contents) = Self::get_clip_contents(&mut context).unwrap_or_default() {
return contents;
}
"".to_string()
}
#[cfg(feature = "full-repl")]
fn get_clip_contents(context: &mut ClipboardContext) -> Result<Option<String>, Box<dyn Error>> {
match context.get_contents() {
Ok(contents) => Ok(Some(contents)),
Err(err)
if (*err).to_string() == "The operation completed successfully. (os error 0)" =>
{
Ok(None)
#[cfg(all(feature = "full-repl", target_os = "linux"))]
fn access_clipboard() -> Option<Output> {
if let Ok(str) = std::fs::read("/proc/sys/kernel/osrelease") {
if let Ok(str) = std::str::from_utf8(&str) {
if str.to_ascii_lowercase().contains("microsoft") {
return Some(
Command::new("powershell")
.args(["get-clipboard"])
.output()
.expect("failed to get clipboard"),
);
}
}
Err(err) => Err(err),
}
match Command::new("xsel")
.args(["--output", "--clipboard"])
.output()
{
Ok(output) => Some(output),
Err(_) => {
execute!(
std::io::stdout(),
Print("You need to install `xsel` to use the paste feature on Linux desktop"),
)
.unwrap();
None
}
}
}
#[cfg(all(feature = "full-repl", target_os = "macos"))]
fn access_clipboard() -> Option<Output> {
Some(
Command::new("pbpast")
.output()
.expect("failed to get clipboard"),
)
}
#[cfg(all(feature = "full-repl", target_os = "windows"))]
fn access_clipboard() -> Option<Output> {
Some(
Command::new("powershell")
.args(["get-clipboard"])
.output()
.expect("failed to get clipboard"),
)
}
#[cfg(not(feature = "full-repl"))]
@ -105,9 +131,16 @@ impl StdinReader {
return Ok(());
}
(KeyCode::Char('v'), KeyModifiers::CONTROL) => {
let op = Self::access_clipboard();
let output = match op {
None => {
continue;
}
Some(output) => output,
};
let clipboard = {
Self::access_clipboard()
.trim_matches(|c: char| c.is_whitespace())
let this = String::from_utf8_lossy(&output.stdout).to_string();
this.trim_matches(|c: char| c.is_whitespace())
.to_string()
.replace(['\n', '\r'], "")
};