diff options
| author | Christophe Verbinnen <christophe@verbinnen.org> | 2022-01-15 06:38:45 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-15 12:38:45 +0100 |
| commit | 9cc2645db024df0a95efda42aed34bd3def19b28 (patch) | |
| tree | d2345ca39dba0d5a2bf2cd7527799859147d6d69 /zellij-server/src | |
| parent | docs(changelog): fix dependency problem (diff) | |
| download | sl0ppij-9cc2645db024df0a95efda42aed34bd3def19b28.tar.gz sl0ppij-9cc2645db024df0a95efda42aed34bd3def19b28.tar.xz sl0ppij-9cc2645db024df0a95efda42aed34bd3def19b28.zip | |
Add a copy command option (#996)
Usage: zellij options --copy-command "xclip -sel clip"
Co-authored-by: Christophe Verbinnen <christophev@knowbe4.com>
Diffstat (limited to 'zellij-server/src')
| -rw-r--r-- | zellij-server/src/screen.rs | 5 | ||||
| -rw-r--r-- | zellij-server/src/tab/copy_command.rs | 39 | ||||
| -rw-r--r-- | zellij-server/src/tab/mod.rs | 28 | ||||
| -rw-r--r-- | zellij-server/src/tab/unit/tab_tests.rs | 2 | ||||
| -rw-r--r-- | zellij-server/src/unit/screen_tests.rs | 2 |
5 files changed, 71 insertions, 5 deletions
diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index cfc99518..71ae0805 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -193,6 +193,7 @@ pub(crate) struct Screen { colors: Palette, draw_pane_frames: bool, session_is_mirrored: bool, + copy_command: Option<String>, } impl Screen { @@ -204,6 +205,7 @@ impl Screen { mode_info: ModeInfo, draw_pane_frames: bool, session_is_mirrored: bool, + copy_command: Option<String>, ) -> Self { Screen { bus, @@ -219,6 +221,7 @@ impl Screen { default_mode_info: mode_info, draw_pane_frames, session_is_mirrored, + copy_command, } } @@ -491,6 +494,7 @@ impl Screen { self.connected_clients.clone(), self.session_is_mirrored, client_id, + self.copy_command.clone(), ); tab.apply_layout(layout, new_pids, tab_index, client_id); if self.session_is_mirrored { @@ -692,6 +696,7 @@ pub(crate) fn screen_thread_main( ), draw_pane_frames, session_is_mirrored, + config_options.copy_command, ); loop { let (event, mut err_ctx) = screen diff --git a/zellij-server/src/tab/copy_command.rs b/zellij-server/src/tab/copy_command.rs new file mode 100644 index 00000000..61240950 --- /dev/null +++ b/zellij-server/src/tab/copy_command.rs @@ -0,0 +1,39 @@ +use std::io::prelude::*; +use std::process::{Command, Stdio}; + +pub struct CopyCommand { + command: String, + args: Vec<String>, +} + +impl CopyCommand { + pub fn new(command: String) -> Self { + let mut command_with_args = command.split(' ').map(String::from); + + Self { + command: command_with_args.next().expect("missing command"), + args: command_with_args.collect(), + } + } + pub fn set(&self, value: String) -> bool { + let process = match Command::new(self.command.clone()) + .args(self.args.clone()) + .stdin(Stdio::piped()) + .spawn() + { + Err(why) => { + eprintln!("couldn't spawn {}: {}", self.command, why); + return false; + } + Ok(process) => process, + }; + + match process.stdin.unwrap().write_all(value.as_bytes()) { + Err(why) => { + eprintln!("couldn't write to {} stdin: {}", self.command, why); + false + } + Ok(_) => true, + } + } +} diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 2e98915a..cc83d37c 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -1,9 +1,11 @@ //! `Tab`s holds multiple panes. It tracks their coordinates (x/y) and size, //! as well as how they should be resized +mod copy_command; mod pane_grid; mod pane_resizer; +use copy_command::CopyCommand; use zellij_utils::position::{Column, Line}; use zellij_utils::{position::Position, serde, zellij_tile}; @@ -119,6 +121,7 @@ pub(crate) struct Tab { session_is_mirrored: bool, pending_vte_events: HashMap<RawFd, Vec<VteBytes>>, selecting_with_mouse: bool, + copy_command: Option<String>, } #[derive(Clone, Debug, Default, Serialize, Deserialize)] @@ -299,6 +302,7 @@ impl Tab { connected_clients_in_app: Rc<RefCell<HashSet<ClientId>>>, session_is_mirrored: bool, client_id: ClientId, + copy_command: Option<String>, ) -> Self { let panes = BTreeMap::new(); @@ -335,6 +339,7 @@ impl Tab { connected_clients_in_app, connected_clients, selecting_with_mouse: false, + copy_command, } } @@ -1912,11 +1917,20 @@ impl Tab { fn write_selection_to_clipboard(&self, selection: &str) { let mut output = Output::default(); + let mut system_clipboard_failure = false; output.add_clients(&self.connected_clients); - output.push_str_to_multiple_clients( - &format!("\u{1b}]52;c;{}\u{1b}\\", base64::encode(selection)), - self.connected_clients.iter().copied(), - ); + match self.copy_command.clone() { + Some(copy_command) => { + let system_clipboard = CopyCommand::new(copy_command); + system_clipboard_failure = !system_clipboard.set(selection.to_owned()); + } + None => { + output.push_str_to_multiple_clients( + &format!("\u{1b}]52;c;{}\u{1b}\\", base64::encode(selection)), + self.connected_clients.iter().copied(), + ); + } + } // TODO: ideally we should be sending the Render instruction from the screen self.senders @@ -1926,7 +1940,11 @@ impl Tab { .send_to_plugin(PluginInstruction::Update( None, None, - Event::CopyToClipboard, + if system_clipboard_failure { + Event::SystemClipboardFailure + } else { + Event::CopyToClipboard + }, )) .unwrap(); } diff --git a/zellij-server/src/tab/unit/tab_tests.rs b/zellij-server/src/tab/unit/tab_tests.rs index 13b97124..5ac63f6b 100644 --- a/zellij-server/src/tab/unit/tab_tests.rs +++ b/zellij-server/src/tab/unit/tab_tests.rs @@ -96,6 +96,7 @@ fn create_new_tab(size: Size) -> Tab { let mut connected_clients = HashSet::new(); connected_clients.insert(client_id); let connected_clients = Rc::new(RefCell::new(connected_clients)); + let copy_command = None; let mut tab = Tab::new( index, position, @@ -110,6 +111,7 @@ fn create_new_tab(size: Size) -> Tab { connected_clients, session_is_mirrored, client_id, + copy_command, ); tab.apply_layout( LayoutTemplate::default().try_into().unwrap(), diff --git a/zellij-server/src/unit/screen_tests.rs b/zellij-server/src/unit/screen_tests.rs index ca34645d..1fbc85fc 100644 --- a/zellij-server/src/unit/screen_tests.rs +++ b/zellij-server/src/unit/screen_tests.rs @@ -91,6 +91,7 @@ fn create_new_screen(size: Size) -> Screen { let mode_info = ModeInfo::default(); let draw_pane_frames = false; let session_is_mirrored = true; + let copy_command = None; Screen::new( bus, &client_attributes, @@ -98,6 +99,7 @@ fn create_new_screen(size: Size) -> Screen { mode_info, draw_pane_frames, session_is_mirrored, + copy_command, ) } |
