Switch to a maintained clipboard library
This commit is contained in:
parent
605b5f5388
commit
57f7985955
File diff suppressed because it is too large
Load Diff
|
@ -5,12 +5,12 @@ edition = "2021"
|
|||
authors = ["Agatha V. Lovelace <agatha@technogothic.net>"]
|
||||
description = "Strip unneeded parameters from URLs copied to clipboard"
|
||||
license = "NVPLv7+"
|
||||
rust-version = "1.65"
|
||||
rust-version = "1.66.1"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cli-clipboard = "0.4.0"
|
||||
arboard = { version = "3.3.0", features = ["wayland-data-control"] }
|
||||
knuffel = "3.0.0"
|
||||
memoize = { version = "0.4.0", features = ["full"] }
|
||||
miette = { version = "5.7.0", features = ["fancy"] }
|
||||
|
|
44
flake.lock
44
flake.lock
|
@ -5,11 +5,11 @@
|
|||
"nixpkgs": "nixpkgs"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1679567394,
|
||||
"narHash": "sha256-ZvLuzPeARDLiQUt6zSZFGOs+HZmE+3g4QURc8mkBsfM=",
|
||||
"lastModified": 1698420672,
|
||||
"narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=",
|
||||
"owner": "nix-community",
|
||||
"repo": "naersk",
|
||||
"rev": "88cd22380154a2c36799fe8098888f0f59861a15",
|
||||
"rev": "aeb58d5e8faead8980a807c840232697982d47b9",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -21,11 +21,11 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1680273054,
|
||||
"narHash": "sha256-Bs6/5LpvYp379qVqGt9mXxxx9GSE789k3oFc+OAL07M=",
|
||||
"lastModified": 1704161960,
|
||||
"narHash": "sha256-QGua89Pmq+FBAro8NriTuoO/wNaUtugt29/qqA8zeeM=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3364b5b117f65fe1ce65a3cdd5612a078a3b31e3",
|
||||
"rev": "63143ac2c9186be6d9da6035fa22620018c85932",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -35,11 +35,11 @@
|
|||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1680273054,
|
||||
"narHash": "sha256-Bs6/5LpvYp379qVqGt9mXxxx9GSE789k3oFc+OAL07M=",
|
||||
"lastModified": 1704161960,
|
||||
"narHash": "sha256-QGua89Pmq+FBAro8NriTuoO/wNaUtugt29/qqA8zeeM=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3364b5b117f65fe1ce65a3cdd5612a078a3b31e3",
|
||||
"rev": "63143ac2c9186be6d9da6035fa22620018c85932",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -56,13 +56,31 @@
|
|||
"utils": "utils"
|
||||
}
|
||||
},
|
||||
"utils": {
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1678901627,
|
||||
"narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=",
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1701680307,
|
||||
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6",
|
||||
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
63
src/main.rs
63
src/main.rs
|
@ -1,4 +1,4 @@
|
|||
use cli_clipboard::{ClipboardContext, ClipboardProvider};
|
||||
use arboard::Clipboard;
|
||||
use memoize::memoize;
|
||||
use miette::{miette, IntoDiagnostic, Result};
|
||||
use std::{env, fs};
|
||||
|
@ -32,41 +32,40 @@ fn main() -> Result<()> {
|
|||
.collect::<Vec<Category>>();
|
||||
|
||||
println!("Loaded with categories:");
|
||||
filters.iter().for_each(|v| println!("\t• {}", v.name));
|
||||
for filter in &filters {
|
||||
println!("\t• {}", filter.name);
|
||||
}
|
||||
|
||||
// Flatten filters into patterns
|
||||
let patterns: Vec<String> = filters.iter().map(|v| v.params.clone()).flatten().collect();
|
||||
let patterns: Vec<String> = filters.iter().flat_map(|v| v.params.clone()).collect();
|
||||
|
||||
// Initialize clipboard context
|
||||
let mut clipboard = ClipboardContext::new()
|
||||
let mut clipboard = Clipboard::new()
|
||||
.map_err(|e| miette!(format!("Could not initialize clipboard context: {e}")))?;
|
||||
let mut last_contents = clipboard.get_contents().unwrap_or_else(|_| String::new());
|
||||
let mut last_contents = clipboard.get_text().unwrap_or_else(|_| String::new());
|
||||
loop {
|
||||
match clipboard.get_contents() {
|
||||
Ok(contents) => {
|
||||
// Empty clipboard (Linux)
|
||||
if contents.is_empty() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||
continue;
|
||||
};
|
||||
|
||||
// Clipboard changed
|
||||
if contents != last_contents {
|
||||
last_contents = contents.clone();
|
||||
if let Ok(url) = clean_url(contents, patterns.clone()) {
|
||||
// Update clipboard
|
||||
clipboard.set_contents(url.clone()).map_err(|e| {
|
||||
miette!(format!("Couldn't set clipboard contents: {e}"))
|
||||
})?;
|
||||
last_contents = url;
|
||||
};
|
||||
};
|
||||
}
|
||||
// Empty clipboard (Mac, Windows)
|
||||
Err(_) => {
|
||||
if let Ok(contents) = clipboard.get_text() {
|
||||
// Empty clipboard (Linux)
|
||||
if contents.is_empty() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Clipboard changed
|
||||
if contents != last_contents {
|
||||
last_contents = contents.clone();
|
||||
if let Ok(url) = clean_url(contents, patterns.clone()) {
|
||||
// Update clipboard
|
||||
clipboard
|
||||
.set_text(url.clone())
|
||||
.map_err(|e| miette!(format!("Couldn't set clipboard contents: {e}")))?;
|
||||
last_contents = url;
|
||||
};
|
||||
};
|
||||
} else {
|
||||
// Empty clipboard (Mac, Windows)
|
||||
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||
continue;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +76,9 @@ fn clean_url(text: String, patterns: Vec<String>) -> Result<String, String> {
|
|||
let url_inner = url.clone();
|
||||
|
||||
// Skip URLs without a host
|
||||
let Some(host) = url_inner.host_str() else { return Err(format!("URL {} does not have a host", url_inner)) };
|
||||
let Some(host) = url_inner.host_str() else {
|
||||
return Err(format!("URL {url_inner} does not have a host"));
|
||||
};
|
||||
|
||||
for pattern in &patterns {
|
||||
let url_inner = url.clone();
|
||||
|
@ -104,10 +105,10 @@ fn clean_url(text: String, patterns: Vec<String>) -> Result<String, String> {
|
|||
}
|
||||
|
||||
// Handle dangling ?s when no query pairs are appended
|
||||
let url = url.as_str().trim_end_matches("?").to_owned();
|
||||
let url = url.as_str().trim_end_matches('?').to_owned();
|
||||
|
||||
Ok(url)
|
||||
} else {
|
||||
Err(format!("Contents are not a valid URL"))
|
||||
Err(String::from("Contents are not a valid URL"))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue