Skip to content

Commit 44727c1

Browse files
committed
Refactor clipboard code
1 parent f87a74e commit 44727c1

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

src/clipboard.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use base64::{engine::general_purpose, Engine as _};
2+
use copypasta_ext::prelude::*;
3+
use copypasta_ext::wayland_bin::WaylandBinClipboardContext;
4+
use copypasta_ext::x11_bin::ClipboardContext as BinClipboardContext;
5+
use copypasta_ext::x11_fork::ClipboardContext as ForkClipboardContext;
6+
use crossterm::style::Print;
7+
use std::{env, io};
8+
9+
pub enum CopyType {
10+
Native,
11+
OSC52,
12+
}
13+
14+
pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> {
15+
if ssh_clipboard(content.as_str()) {
16+
Ok(CopyType::OSC52)
17+
} else if wayland_clipboard(content.as_str()) || other_platform_clipboard(content.as_str()) {
18+
Ok(CopyType::Native)
19+
} else {
20+
Err(())
21+
}
22+
}
23+
24+
fn ssh_clipboard(content: &str) -> bool {
25+
env_var_set("SSH_CONNECTION")
26+
// We do not use copypasta_ext::osc52 module because we have enabled terminal raw mode, so we print with crossterm utilities
27+
// Check https://github.com/timvisee/rust-clipboard-ext/blob/371df19d2f961882a21c957f396d1e24548d1f28/src/osc52.rs#L92
28+
&& crossterm::execute!(
29+
io::stdout(),
30+
Print(format!(
31+
"\x1B]52;c;{}\x07",
32+
general_purpose::STANDARD.encode(content)
33+
))
34+
)
35+
.is_ok()
36+
}
37+
38+
fn wayland_clipboard(content: &str) -> bool {
39+
env_var_set("WAYLAND_DISPLAY")
40+
&& WaylandBinClipboardContext::new()
41+
.and_then(|mut ctx| ctx.set_contents(content.to_owned()))
42+
.is_ok()
43+
}
44+
45+
fn other_platform_clipboard(content: &str) -> bool {
46+
BinClipboardContext::new()
47+
.and_then(|mut ctx| ctx.set_contents(content.to_owned()))
48+
.is_ok()
49+
|| ForkClipboardContext::new()
50+
.and_then(|mut ctx| ctx.set_contents(content.to_owned()))
51+
.is_ok()
52+
}
53+
54+
fn env_var_set(env_var: &str) -> bool {
55+
env::var(env_var)
56+
.map(|v| !v.trim().is_empty())
57+
.unwrap_or(false)
58+
}

src/interface/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::clipboard::{copy_string_to_clipboard, CopyType};
12
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
23

34
use crate::interface::app::{App, AppResult};
45
use crate::interface::enums::Page::*;
56
use crate::otp::otp_type::OTPType;
6-
use crate::utils::{copy_string_to_clipboard, CopyType};
77

88
use super::app::Popup;
99
use super::enums::Page;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use zeroize::Zeroize;
1414

1515
mod args;
1616
mod argument_functions;
17+
mod clipboard;
1718
mod crypto;
1819
mod exporters;
1920
mod importers;

src/utils.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
use base64::{engine::general_purpose, Engine as _};
2-
use copypasta_ext::prelude::*;
3-
use copypasta_ext::wayland_bin::WaylandBinClipboardContext;
4-
use copypasta_ext::x11_bin::ClipboardContext as BinClipboardContext;
5-
use copypasta_ext::x11_fork::ClipboardContext as ForkClipboardContext;
6-
use crossterm::style::Print;
71
use dirs::home_dir;
82
use std::path::PathBuf;
93
use std::time::{SystemTime, UNIX_EPOCH};
104
use std::{env, io};
115

12-
pub enum CopyType {
13-
Native,
14-
OSC52,
15-
}
16-
176
pub fn get_db_path() -> PathBuf {
187
match env::var("COTP_DB_PATH") {
198
Ok(value) => PathBuf::from(value),
@@ -90,42 +79,3 @@ pub fn verified_password(message: &str, minimum_length: usize) -> String {
9079
return password;
9180
}
9281
}
93-
94-
pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> {
95-
if env_var_set("SSH_CONNECTION")
96-
&& crossterm::execute!(
97-
io::stdout(),
98-
Print(format!(
99-
"\x1B]52;c;{}\x07",
100-
general_purpose::STANDARD.encode(&content)
101-
))
102-
)
103-
.is_ok()
104-
{
105-
// We do not use copypasta_ext::osc52 module because we have enabled terminal raw mode, so we print with crossterm utilities
106-
// Check https://github.com/timvisee/rust-clipboard-ext/blob/371df19d2f961882a21c957f396d1e24548d1f28/src/osc52.rs#L92
107-
Ok(CopyType::OSC52)
108-
} else if env_var_set("WAYLAND_DISPLAY")
109-
&& WaylandBinClipboardContext::new()
110-
.and_then(|mut ctx| ctx.set_contents(content.clone()))
111-
.is_ok()
112-
{
113-
Ok(CopyType::Native)
114-
} else if BinClipboardContext::new()
115-
.and_then(|mut ctx| ctx.set_contents(content.clone()))
116-
.is_ok()
117-
|| ForkClipboardContext::new()
118-
.and_then(|mut ctx| ctx.set_contents(content))
119-
.is_ok()
120-
{
121-
Ok(CopyType::Native)
122-
} else {
123-
Err(())
124-
}
125-
}
126-
127-
fn env_var_set(env_var: &str) -> bool {
128-
env::var(env_var)
129-
.map(|v| !v.trim().is_empty())
130-
.unwrap_or(false)
131-
}

0 commit comments

Comments
 (0)