Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct clipboard on copy phase #51

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
## Build stage
FROM rust:1.79-alpine3.20 as builder

RUN rustup target add x86_64-unknown-linux-musl

RUN apk add musl-dev
# Build stage
FROM rust:1.80.0-slim-bookworm as builder

WORKDIR /jnv

COPY . /jnv
RUN cargo build --release

RUN cargo build --target=x86_64-unknown-linux-musl --release

## Final image

FROM scratch
# Final stage
FROM debian:bookworm-slim

COPY --from=builder /jnv/target/x86_64-unknown-linux-musl/release/jnv /bin/jnv
COPY --from=builder /jnv/target/release/jnv /bin/jnv

ENTRYPOINT ["/bin/jnv"]
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,33 @@ See [package entry on search.nixos.org](https://search.nixos.org/packages?channe
nix-shell -p jnv
```

### Cargo
### Docker

Build
(In the near future, the image will be available on something of registries)

```bash
cargo install jnv
docker build -t jnv .
```

## Examples
And Run
(The following commad is just an example. Please modify the path to the file you want to mount)

```bash
cat data.json | jnv
docker run -it --rm -v $(pwd)/debug.json:/jnv/debug.json jnv /jnv/debug.json
```

Or
### Cargo

```bash
cargo install jnv
```

## Examples

```bash
cat data.json | jnv
# or
jnv data.json
```

Expand Down
76 changes: 48 additions & 28 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();

self.update_hint_message(clipboard_hint, style);
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(),
);
}
}
}

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();
fn store_content_to_clipboard(&mut self) {
self.store_to_clipboard(
&self.json.json_str(),
"Copied selected content to clipboard!",
);
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/jnv/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn default(event: &Event, jnv: &mut crate::jnv::Jnv) -> anyhow::Result<Promp
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => {
jnv.content_to_clipboard();
jnv.store_content_to_clipboard();
}

Event::Key(KeyEvent {
Expand All @@ -203,7 +203,7 @@ pub fn default(event: &Event, jnv: &mut crate::jnv::Jnv) -> anyhow::Result<Promp
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => {
jnv.query_to_clipboard();
jnv.store_query_to_clipboard();
}

Event::Key(KeyEvent {
Expand Down
Loading