Skip to content

Commit

Permalink
Merge pull request #46 from anton-suprun/adding-writing-to-clipboard
Browse files Browse the repository at this point in the history
Added writing query and content to clipboard
  • Loading branch information
ynqa authored Aug 6, 2024
2 parents ca0301f + af03825 commit b490813
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 6 deletions.
90 changes: 88 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ readme = "README.md"
[dependencies]
anyhow = "1.0.82"
clap = { version = "4.5.4", features = ["derive"] }
clipboard = "0.5.0"
jaq-core = "1.2.1"
jaq-interpret = "1.2.1"
jaq-parse = "1.0.2"
jaq-std = "1.2.1"
promkit = "0.4.3"
promkit = "0.4.5"
radix_trie = "0.2.1"

# The profile that 'cargo dist' will build with
Expand Down
30 changes: 30 additions & 0 deletions src/jnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::RefCell;

use anyhow::Result;

use clipboard::{ClipboardContext, ClipboardProvider};
use jaq_interpret::{Ctx, FilterT, ParseCtx, RcIter, Val};

use promkit::{
Expand Down Expand Up @@ -107,6 +108,7 @@ pub struct Jnv {

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

impl Jnv {
Expand Down Expand Up @@ -181,6 +183,7 @@ impl Jnv {
json_expand_depth,
no_hint,
input_stream,
clipboard: ClipboardProvider::new().unwrap()
},
})
}
Expand All @@ -192,6 +195,33 @@ impl Jnv {
.replace(text::State { text, style })
}
}

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

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

self.update_hint_message(clipboard_hint, style);
}

fn query_to_clipboard(&mut self) {
let query = self.filter_editor.after().texteditor.text_without_cursor().to_string();
let _ = self.clipboard.set_contents(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);
}

}

impl promkit::Finalizer for Jnv {
Expand Down
22 changes: 19 additions & 3 deletions src/jnv/keymap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use promkit::{
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
listbox::Listbox,
text_editor, PromptSignal,
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}, listbox::Listbox, text_editor, PromptSignal
};

pub type Keymap = fn(&Event, &mut crate::jnv::Jnv) -> anyhow::Result<PromptSignal>;
Expand Down Expand Up @@ -188,6 +186,24 @@ pub fn default(event: &Event, jnv: &mut crate::jnv::Jnv) -> anyhow::Result<Promp
jnv.json.stream.expand_all();
}

Event::Key(KeyEvent {
code: KeyCode::Char('o'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => {
jnv.content_to_clipboard();
}

Event::Key(KeyEvent {
code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => {
jnv.query_to_clipboard();
}

Event::Key(KeyEvent {
code: KeyCode::Char('n'),
modifiers: KeyModifiers::CONTROL,
Expand Down

0 comments on commit b490813

Please sign in to comment.