summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-05 00:19:48 +0800
committerGitHub <noreply@github.com>2023-03-05 00:19:48 +0800
commita055d4bee697c2dbf600afbd768e840b0f040247 (patch)
treede7960a847c1b0f7687e97b9c1a20caab2cc6f51 /src/utils.rs
parent5179b9a827cd889fdf72d014aa9cee0c2cc6f0f1 (diff)
downloadaichat-a055d4bee697c2dbf600afbd768e840b0f040247.tar.gz
feat: add `.copy` command (#10)
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
new file mode 100644
index 0000000..cb5d1f9
--- /dev/null
+++ b/src/utils.rs
@@ -0,0 +1,19 @@
+use anyhow::{anyhow, Result};
+use copypasta::{ClipboardContext, ClipboardProvider};
+use std::io::{stdout, Write};
+
+pub fn dump<T: ToString>(text: T, newlines: usize) {
+ print!("{}{}", text.to_string(), "\n".repeat(newlines));
+ let _ = stdout().flush();
+}
+
+pub fn copy(src: &str) -> Result<()> {
+ let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
+ ctx.set_contents(src.to_string())
+ .map_err(|err| anyhow!("{err}"))
+}
+
+pub fn paste() -> Result<String> {
+ let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
+ ctx.get_contents().map_err(|err| anyhow!("{err}"))
+}