|
1 | | -use base64::{engine::general_purpose, Engine as _}; |
2 | | -use copypasta_ext::prelude::*; |
3 | | -use copypasta_ext::x11_fork::ClipboardContext; |
4 | | -use crossterm::style::Print; |
5 | | -#[cfg(not(debug_assertions))] |
6 | 1 | use dirs::home_dir; |
7 | 2 | use std::path::PathBuf; |
8 | 3 | use std::time::{SystemTime, UNIX_EPOCH}; |
9 | 4 | use std::{env, io}; |
10 | 5 |
|
11 | | -pub enum CopyType { |
12 | | - Native, |
13 | | - OSC52, |
14 | | -} |
15 | | - |
16 | 6 | pub fn get_db_path() -> PathBuf { |
17 | 7 | match env::var("COTP_DB_PATH") { |
18 | 8 | Ok(value) => PathBuf::from(value), |
19 | 9 | Err(_e) => get_default_db_path(), |
20 | 10 | } |
21 | 11 | } |
22 | 12 |
|
| 13 | +pub fn is_portable_mode() -> bool { |
| 14 | + PathBuf::from("db.cotp").exists() |
| 15 | +} |
| 16 | + |
23 | 17 | // Pushing an absolute path to a PathBuf replaces the entire PathBuf: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push |
24 | 18 | pub fn get_default_db_path() -> PathBuf { |
25 | | - let result: Option<PathBuf> = { |
26 | | - #[cfg(not(debug_assertions))] |
27 | | - { |
28 | | - home_dir() |
29 | | - } |
30 | | - #[cfg(debug_assertions)] |
31 | | - Some(PathBuf::from(".")) |
32 | | - }; |
33 | | - match result { |
34 | | - Some(home) => home, |
35 | | - None => { |
36 | | - let current_dir = PathBuf::from("."); |
37 | | - if let Some(str_dir) = current_dir.to_str() { |
38 | | - eprintln!("Cannot get home folder, using: {str_dir}"); |
39 | | - } else { |
40 | | - eprintln!("Cannot get home folder, using"); |
41 | | - } |
42 | | - current_dir |
43 | | - } |
| 19 | + let db_from_current_dir = PathBuf::from("./db.cotp"); |
| 20 | + |
| 21 | + // If db.cotp is present in the current directory or we are using a debug artifact, do not use the one in home dir |
| 22 | + // First condition is optimized away in release mode |
| 23 | + if cfg!(debug_assertions) || is_portable_mode() { |
| 24 | + return db_from_current_dir; |
44 | 25 | } |
45 | | - .join(".cotp/db.cotp") |
| 26 | + |
| 27 | + // Take from homedir, otherwise fallback to portable mode |
| 28 | + home_dir() |
| 29 | + .map(|path| path.join(".cotp/db.cotp")) |
| 30 | + .unwrap_or(db_from_current_dir) |
46 | 31 | } |
47 | 32 |
|
48 | 33 | pub fn init_app() -> Result<bool, ()> { |
@@ -94,33 +79,3 @@ pub fn verified_password(message: &str, minimum_length: usize) -> String { |
94 | 79 | return password; |
95 | 80 | } |
96 | 81 | } |
97 | | - |
98 | | -fn in_ssh_shell() -> bool { |
99 | | - return env::var("SSH_CONNECTION") |
100 | | - .map(|v| !v.trim().is_empty()) |
101 | | - .unwrap_or(false); |
102 | | -} |
103 | | - |
104 | | -pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> { |
105 | | - if in_ssh_shell() { |
106 | | - // We do not use copypasta_ext::osc52 module because we have enabled terminal raw mode, so we print with crossterm utilities |
107 | | - // Check https://github.com/timvisee/rust-clipboard-ext/blob/371df19d2f961882a21c957f396d1e24548d1f28/src/osc52.rs#L92 |
108 | | - return match crossterm::execute!( |
109 | | - io::stdout(), |
110 | | - Print(format!( |
111 | | - "\x1B]52;c;{}\x07", |
112 | | - general_purpose::STANDARD.encode(content) |
113 | | - )) |
114 | | - ) { |
115 | | - Ok(_) => Ok(CopyType::OSC52), |
116 | | - Err(_) => Err(()), |
117 | | - }; |
118 | | - } else if let Ok(mut ctx) = ClipboardContext::new() { |
119 | | - return if ctx.set_contents(content).is_ok() { |
120 | | - Ok(CopyType::Native) |
121 | | - } else { |
122 | | - Err(()) |
123 | | - }; |
124 | | - } |
125 | | - Err(()) |
126 | | -} |
0 commit comments