termux_api/api/
clipboard.rs1use std::process::Command;
2
3use super::errors::TermuxError;
4
5pub struct TermuxClipboardGet {}
6
7impl TermuxClipboardGet {
8 pub fn new() -> Self {
9 TermuxClipboardGet {}
10 }
11
12 pub fn run(&self) -> Result<String, TermuxError> {
13 let mut command = Command::new("termux-clipboard-get");
14 let output = command.output();
15 match output {
16 Ok(output) => {
17 if output.status.success() {
18 match String::from_utf8(output.stdout) {
19 Ok(value) => return Ok(value),
20 Err(_) => return Ok(String::new()),
21 }
22 }
23 Err(TermuxError::Output(output.to_owned()))
24 }
25 Err(e) => Err(TermuxError::IOError(e)),
26 }
27 }
28}
29
30pub struct TermuxClipboardSet {
31 content: Option<String>,
32}
33
34impl TermuxClipboardSet {
35 pub fn new() -> Self {
36 TermuxClipboardSet { content: None }
37 }
38
39 pub fn content(mut self, content: String) -> Self {
40 self.content = Some(content);
41 self
42 }
43
44 pub fn run(self) -> Result<(), TermuxError> {
45 let mut command = Command::new("termux-clipboard-set");
46 if let Some(content) = self.content {
47 command.arg(content);
48 }
49 let output = command.output();
50 match output {
51 Ok(output) => {
52 if output.status.success() {
53 return Ok(());
54 }
55 Err(TermuxError::Output(output.to_owned()))
56 }
57 Err(e) => Err(TermuxError::IOError(e)),
58 }
59 }
60}
61
62pub struct TermuxClipboard {
63 pub get: TermuxClipboardGet,
64 pub set: TermuxClipboardSet,
65}
66
67impl TermuxClipboard {
68 pub fn new() -> Self {
69 TermuxClipboard {
70 get: TermuxClipboardGet::new(),
71 set: TermuxClipboardSet::new(),
72 }
73 }
74}