aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2016-09-05 02:44:11 -0400
committerTeddy Wing2016-09-05 02:44:11 -0400
commit07558bf1a6e681d5492f2e0112d94fd584aa8d0f (patch)
treeae4bf24f351db0dcc98e8b29b9854549d8f7a7f3
parent426e7a89b6a00cdeab0dbbbb8b55eab37bc5c834 (diff)
downloadPassextract-07558bf1a6e681d5492f2e0112d94fd584aa8d0f.tar.bz2
ClipboardStore proof of concept
The new type `ClipboardStore` wraps `ClipboardContext`, allowing us to save the contents of the clipboard before resetting it. We will then be able to reset the clipboard to this original value. This commit includes some old code from when I was originally thinking about this problem before I decided to make a separate module for it. That code should be deleted. One caveat to think about in the future is how to reset the clipboard to the original value after a period of time. In other words, how to pass the original contents value to a spawned child process. Also, we need to make sure that we're only setting `last` once. Subsequent calls should not change the `last` value. And now that I think about that, "last" is probably the wrong name for this. It should be something like "original".
-rw-r--r--src/clipboard_store.rs36
-rw-r--r--src/main.rs20
2 files changed, 56 insertions, 0 deletions
diff --git a/src/clipboard_store.rs b/src/clipboard_store.rs
new file mode 100644
index 0000000..61f53b3
--- /dev/null
+++ b/src/clipboard_store.rs
@@ -0,0 +1,36 @@
+extern crate clipboard;
+
+use std::error::Error;
+
+use clipboard::ClipboardContext;
+
+pub struct ClipboardStore {
+ context: ClipboardContext,
+ last: String,
+}
+
+impl ClipboardStore {
+ pub fn new() -> Result<ClipboardStore, Box<Error>> {
+ let context = try!(ClipboardContext::new());
+
+ return Ok(
+ ClipboardStore {
+ context: context,
+ last: String::new(),
+ }
+ )
+ }
+
+ pub fn set_contents(&mut self, data: String) -> Result<(), Box<Error>> {
+ // Save last value
+ self.last = try!(self.context.get_contents());
+ println!("last: {}", self.last);
+
+ // Set new clipboard contents
+ // self.context.set_contents(data)
+ Ok(())
+ }
+
+ pub fn reset() {
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8e497c7..f499809 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,10 @@ use std::io::{self, BufRead};
use std::process::{self, Command};
use std::time::Duration;
+mod clipboard_store;
+
+use clipboard_store::ClipboardStore;
+
struct Point {
x: usize,
y: usize,
@@ -78,6 +82,17 @@ fn parse_options(filename: &str) -> Vec<String> {
options
}
+fn spawn_reset_clipboard() {
+ Command::new("passextract")
+ .arg("--reset-clipboard")
+ .spawn()
+ .expect("Failed to reset clipboard");
+}
+
+fn reset_clipboard() {
+
+}
+
fn main() {
let args: Vec<String> = env::args().collect();
@@ -103,6 +118,7 @@ fn main() {
let mut selection = Point { x: 0, y: 2 };
let mut clipboard_ctx = ClipboardContext::new().unwrap();
+ let mut clipboard_store = ClipboardStore::new().expect("Failed to initialize clipboard storage");
loop {
term.printline_with_cell(0, 0, "Passextract (Press q or Ctrl-C to quit)", knockout_cell);
@@ -117,6 +133,9 @@ fn main() {
if let Some(Event::Key(ch)) = evt {
match ch {
'q' | '\x03' => {
+ // Reset clipboard
+ reset_clipboard();
+
break;
}
'j' => {
@@ -138,6 +157,7 @@ fn main() {
move_selection(&mut term, &mut selection, knockout_cell, options.len() + 1)
}
'\x0D' => {
+ clipboard_store.set_contents(strip_key(&options[selection.y - 2]).to_owned());
match clipboard_ctx.set_contents(strip_key(&options[selection.y - 2]).to_owned()) {
Ok(_) => {
term.printline_with_cell(selection.x, selection.y, "->", green_cell);