Skip to content
Open
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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ clap = { version = "3.0", features = ["derive"] }
isatty = "0.1"
libc-stdhandle = "0.1.0"
yaml-rust = "0.4"
clipboard = "0.5"
clipboard = { version = "0.5", optional = true }

[dev-dependencies]
indoc = "1.0"

[features]
default = ["clipboard"]
clipboard = ["dep:clipboard"]

14 changes: 13 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(feature = "clipboard")]
use std::error::Error;
use std::io;
use std::io::Write;

#[cfg(feature = "clipboard")]
use clipboard::{ClipboardContext, ClipboardProvider};
use rustyline::error::ReadlineError;
use rustyline::Editor;
Expand All @@ -13,12 +15,15 @@ use termion::screen::{ToAlternateScreen, ToMainScreen};
use crate::flatjson;
use crate::input::TuiEvent;
use crate::input::TuiEvent::{KeyEvent, MouseEvent, WinChEvent};
#[cfg(feature = "clipboard")]
use crate::lineprinter::JS_IDENTIFIER;
use crate::options::{DataFormat, Opt};
use crate::screenwriter::{MessageSeverity, ScreenWriter};
use crate::search::{JumpDirection, SearchDirection, SearchState};
use crate::types::TTYDimensions;
use crate::viewer::{Action, JsonViewer, Mode};
use crate::viewer::{Action, JsonViewer};
#[cfg(feature = "clipboard")]
use crate::viewer::Mode;

pub struct App {
viewer: JsonViewer,
Expand All @@ -28,6 +33,7 @@ pub struct App {
input_filename: String,
search_state: SearchState,
message: Option<(String, MessageSeverity)>,
#[cfg(feature = "clipboard")]
clipboard_context: Result<ClipboardContext, Box<dyn Error>>,
}

Expand All @@ -43,11 +49,13 @@ pub struct App {
#[derive(PartialEq)]
enum InputState {
Default,
#[cfg(feature = "clipboard")]
PendingYCommand,
PendingZCommand,
}

// Various things that can be copied
#[cfg(feature = "clipboard")]
enum CopyTarget {
PrettyPrintedValue,
OneLineValue,
Expand Down Expand Up @@ -96,6 +104,7 @@ impl App {
input_filename,
search_state: SearchState::empty(),
message: None,
#[cfg(feature = "clipboard")]
clipboard_context: ClipboardProvider::new(),
})
}
Expand Down Expand Up @@ -152,6 +161,7 @@ impl App {
}
// Handle special input states:
// y commands:
#[cfg(feature = "clipboard")]
event if self.input_state == InputState::PendingYCommand => {
let copy_target = match event {
KeyEvent(Key::Char('y')) => Some(CopyTarget::PrettyPrintedValue),
Expand Down Expand Up @@ -207,6 +217,7 @@ impl App {
None
}
}
#[cfg(feature = "clipboard")]
KeyEvent(Key::Char('y')) => {
match &self.clipboard_context {
Ok(_) => {
Expand Down Expand Up @@ -622,6 +633,7 @@ impl App {
let _ = write!(self.screen_writer.stdout, "{}", ToAlternateScreen);
}

#[cfg(feature = "clipboard")]
fn copy_content(&mut self, copy_target: CopyTarget) {
// Checked when the user first hits 'y'.
let clipboard = self.clipboard_context.as_mut().unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/flatjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ impl FlatJson {
// there are some subtle enough differences, and the code isn't that
// complicated, that I don't think it's worth it to try to have them
// share an implementation.
#[cfg(feature = "clipboard")]
pub fn pretty_printed_value(&self, value_index: Index) -> Result<String, std::fmt::Error> {
if self[value_index].is_primitive() {
return Ok(self.1[self[value_index].range.clone()].to_string());
Expand Down