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

Use command for clipboard instead of copypasta #36

Merged
merged 3 commits into from
Aug 8, 2021
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
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,5 @@ jobs:
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
- name: Install dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get install libxcb-xkb-dev
sudo apt install libxcb-composite0-dev
- name: Run Tests
run: cargo test --workspace -- --skip=e2e --color always
32 changes: 12 additions & 20 deletions Cargo.lock

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

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ async-trait = "0.1.50"
itertools = "0.10.0"
rust_decimal = "1.15"

[target.'cfg(any(target_os = "macos", windows))'.dependencies]
copypasta = { version = "0.7.0", default-features = false }

[target.'cfg(not(any(target_os = "macos", windows)))'.dependencies]
copypasta = { version = "0.7.0", features = ["x11"], default-features = false }
[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
which = "4.1"
2 changes: 1 addition & 1 deletion database-tree/src/databasetreeitems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DatabaseTreeItems {
item.set_collapsed(false);
item
} else {
let mut item = item.clone();
let mut item = item;
item.show();
item
}
Expand Down
8 changes: 3 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::clipboard::Clipboard;
use crate::clipboard::copy_to_clipboard;
use crate::components::{CommandInfo, Component as _, DrawableComponent as _, EventState};
use crate::database::{MySqlPool, Pool, PostgresPool, RECORDS_LIMIT_PER_PAGE};
use crate::event::Key;
Expand Down Expand Up @@ -31,7 +31,6 @@ pub struct App {
databases: DatabasesComponent,
connections: ConnectionsComponent,
table_status: TableStatusComponent,
clipboard: Clipboard,
pool: Option<Box<dyn Pool>>,
pub config: Config,
pub error: ErrorComponent,
Expand All @@ -50,7 +49,6 @@ impl App {
table_status: TableStatusComponent::default(),
error: ErrorComponent::new(config.key_config),
focus: Focus::ConnectionList,
clipboard: Clipboard::new(),
pool: None,
}
}
Expand Down Expand Up @@ -262,7 +260,7 @@ impl App {

if key == self.config.key_config.copy {
if let Some(text) = self.record_table.table.selected_cells() {
self.clipboard.store(text)
copy_to_clipboard(text.as_str())?
}
}

Expand Down Expand Up @@ -312,7 +310,7 @@ impl App {

if key == self.config.key_config.copy {
if let Some(text) = self.structure_table.selected_cells() {
self.clipboard.store(text)
copy_to_clipboard(text.as_str())?
}
};
}
Expand Down
110 changes: 55 additions & 55 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
use copypasta::ClipboardContext;
use copypasta::ClipboardProvider;
use anyhow::{anyhow, Result};
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
use std::ffi::OsStr;
use std::io::Write;
use std::process::{Command, Stdio};

pub struct Clipboard {
clipboard: Box<dyn ClipboardProvider>,
selection: Option<Box<dyn ClipboardProvider>>,
}
fn execute_copy_command(command: Command, text: &str) -> Result<()> {
let mut command = command;

impl Clipboard {
pub fn new() -> Self {
Self::default()
}
let mut process = command
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

// #[cfg(any(test, not(any(target_os = "macos", windows))))]
// pub fn new_nop() -> Self {
// Self {
// clipboard: Box::new(NopClipboardContext::new().unwrap()),
// selection: None,
// }
// }
}
process
.stdin
.as_mut()
.ok_or_else(|| anyhow!("`{:?}`", command))?
.write_all(text.as_bytes())
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

impl Default for Clipboard {
fn default() -> Self {
return Self {
clipboard: Box::new(ClipboardContext::new().unwrap()),
selection: None,
};
process
.wait()
.map_err(|e| anyhow!("`{:?}`: {}", command, e))?;

// #[cfg(target_os = "linux")]
// return Self {
// clipboard: Box::new(ClipboardContext::new().unwrap()),
// selection: Some(Box::new(
// X11ClipboardContext::<X11SelectionClipboard>::new().unwrap(),
// )),
// };
Ok(())
}

// #[cfg(not(any(target_os = "macos", windows)))]
// return Self::new_nop();
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
fn gen_command(path: impl AsRef<OsStr>, xclip_syntax: bool) -> Command {
let mut c = Command::new(path);
if xclip_syntax {
c.arg("-selection");
c.arg("clipboard");
} else {
c.arg("--clipboard");
}
c
}

impl Clipboard {
pub fn store(&mut self, text: impl Into<String>) {
let clipboard = match &mut self.selection {
Some(provider) => provider,
None => &mut self.clipboard,
};
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
pub fn copy_to_clipboard(string: &str) -> Result<()> {
use std::path::PathBuf;
use which::which;
let (path, xclip_syntax) = which("xclip").ok().map_or_else(
|| {
(
which("xsel").ok().unwrap_or_else(|| PathBuf::from("xsel")),
false,
)
},
|path| (path, true),
);

clipboard.set_contents(text.into()).unwrap_or_else(|err| {
panic!("Unable to store text in clipboard: {}", err);
});
}
let cmd = gen_command(path, xclip_syntax);
execute_copy_command(cmd, string)
}

pub fn _load(&mut self) -> String {
let clipboard = match &mut self.selection {
Some(provider) => provider,
None => &mut self.clipboard,
};
#[cfg(target_os = "macos")]
pub fn copy_to_clipboard(string: &str) -> Result<()> {
execute_copy_command(Command::new("pbcopy"), string)
}

match clipboard.get_contents() {
Err(err) => {
panic!("Unable to load text from clipboard: {}", err);
}
Ok(text) => text,
}
}
#[cfg(windows)]
pub fn copy_to_clipboard(string: &str) -> Result<()> {
execute_copy_command(Command::new("clip"), string)
}
2 changes: 1 addition & 1 deletion src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ fn convert_column_value_to_string(row: &PgRow, column: &PgColumn) -> anyhow::Res
}
if let Ok(value) = row.try_get(column_name) {
let value: Option<Vec<String>> = value;
return Ok(value.map_or("NULL".to_string(), |v| v.join(",").to_string()));
return Ok(value.map_or("NULL".to_string(), |v| v.join(",")));
}
Err(anyhow::anyhow!(
"column type not implemented: `{}` {}",
Expand Down