Skip to content

Commit

Permalink
define common function representing the flow until save contents into…
Browse files Browse the repository at this point in the history
… clipboard
  • Loading branch information
ynqa committed Aug 6, 2024
1 parent 2d577a8 commit 8fd6ddc
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions src/jnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub struct Jnv {

json_expand_depth: Option<usize>,
no_hint: bool,
clipboard: Clipboard,
}

impl Jnv {
Expand Down Expand Up @@ -183,7 +182,6 @@ impl Jnv {
json_expand_depth,
no_hint,
input_stream,
clipboard: Clipboard::new()?,
},
})
}
Expand All @@ -196,35 +194,57 @@ impl Jnv {
}
}

fn content_to_clipboard(&mut self) {
let content = self.json.json_str();
let _ = self.clipboard.set_text(content);

let clipboard_hint = String::from("Copied selected content to clipboard!");
let style = StyleBuilder::new()
.fgc(Color::Grey)
.attrs(Attributes::from(Attribute::Italic))
.build();
fn store_to_clipboard(&mut self, content: &str, hint: &str) {
match Clipboard::new() {
Ok(mut clipboard) => match clipboard.set_text(content) {
Ok(_) => {
self.update_hint_message(
hint.to_string(),
StyleBuilder::new().fgc(Color::Grey).build(),
);
}
Err(e) => {
self.update_hint_message(
format!("Failed to copy to clipboard: {}", e),
StyleBuilder::new()
.fgc(Color::Red)
.attrs(Attributes::from(Attribute::Bold))
.build(),
);
}
},
// arboard fails (in the specific environment like linux?) on Clipboard::new()
// suppress the errors (but still show them) not to break the prompt
// https://github.com/1Password/arboard/issues/153
Err(e) => {
self.update_hint_message(
format!("Failed to setup clipboard: {}", e),
StyleBuilder::new()
.fgc(Color::Red)
.attrs(Attributes::from(Attribute::Bold))
.build(),
);
}
}
}

self.update_hint_message(clipboard_hint, style);
fn content_to_clipboard(&mut self) {
self.store_to_clipboard(
&self.json.json_str(),
"Copied selected content to clipboard!",
);
}

fn query_to_clipboard(&mut self) {
let query = self
.filter_editor
.after()
.texteditor
.text_without_cursor()
.to_string();
let _ = self.clipboard.set_text(query);

let clipboard_hint = String::from("Copied jq query to clipboard!");
let style = StyleBuilder::new()
.fgc(Color::Grey)
.attrs(Attributes::from(Attribute::Italic))
.build();

self.update_hint_message(clipboard_hint, style);
self.store_to_clipboard(
&self
.filter_editor
.after()
.texteditor
.text_without_cursor()
.to_string(),
"Copied jq query to clipboard!",
);
}
}

Expand Down

0 comments on commit 8fd6ddc

Please sign in to comment.