Skip to content

Commit

Permalink
Add a system_clipboard option
Browse files Browse the repository at this point in the history
catch error

fix
  • Loading branch information
ChristopheVerbinnen committed Jan 11, 2022
1 parent e06300f commit ffee5b1
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 10 deletions.
310 changes: 306 additions & 4 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion default-plugins/status-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use zellij_tile_utils::style;

use first_line::{ctrl_keys, superkey};
use second_line::{
fullscreen_panes_to_hide, keybinds, locked_fullscreen_panes_to_hide, text_copied_hint,
fullscreen_panes_to_hide, keybinds, locked_fullscreen_panes_to_hide, system_clipboard_error,
text_copied_hint,
};
use tip::utils::get_cached_tip_name;

Expand All @@ -24,6 +25,7 @@ struct State {
tip_name: String,
mode_info: ModeInfo,
diplay_text_copied_hint: bool,
display_system_clipboard_failure: bool,
}

register_plugin!(State);
Expand Down Expand Up @@ -142,6 +144,7 @@ impl ZellijPlugin for State {
EventType::TabUpdate,
EventType::CopyToClipboard,
EventType::InputReceived,
EventType::SystemClipboardFailure,
]);
}

Expand All @@ -156,8 +159,12 @@ impl ZellijPlugin for State {
Event::CopyToClipboard => {
self.diplay_text_copied_hint = true;
}
Event::SystemClipboardFailure => {
self.display_system_clipboard_failure = true;
}
Event::InputReceived => {
self.diplay_text_copied_hint = false;
self.display_system_clipboard_failure = false;
}
_ => {}
}
Expand Down Expand Up @@ -188,12 +195,16 @@ impl ZellijPlugin for State {
if t.is_fullscreen_active {
second_line = if self.diplay_text_copied_hint {
text_copied_hint(&self.mode_info.palette)
} else if self.display_system_clipboard_failure {
system_clipboard_error(&self.mode_info.palette)
} else {
fullscreen_panes_to_hide(&self.mode_info.palette, t.panes_to_hide)
}
} else {
second_line = if self.diplay_text_copied_hint {
text_copied_hint(&self.mode_info.palette)
} else if self.display_system_clipboard_failure {
system_clipboard_error(&self.mode_info.palette)
} else {
keybinds(&self.mode_info, &self.tip_name, cols)
}
Expand All @@ -203,6 +214,8 @@ impl ZellijPlugin for State {
if t.is_fullscreen_active {
second_line = if self.diplay_text_copied_hint {
text_copied_hint(&self.mode_info.palette)
} else if self.display_system_clipboard_failure {
system_clipboard_error(&self.mode_info.palette)
} else {
locked_fullscreen_panes_to_hide(
&self.mode_info.palette,
Expand All @@ -212,6 +225,8 @@ impl ZellijPlugin for State {
} else {
second_line = if self.diplay_text_copied_hint {
text_copied_hint(&self.mode_info.palette)
} else if self.display_system_clipboard_failure {
system_clipboard_error(&self.mode_info.palette)
} else {
keybinds(&self.mode_info, &self.tip_name, cols)
}
Expand All @@ -220,6 +235,8 @@ impl ZellijPlugin for State {
_ => {
second_line = if self.diplay_text_copied_hint {
text_copied_hint(&self.mode_info.palette)
} else if self.display_system_clipboard_failure {
system_clipboard_error(&self.mode_info.palette)
} else {
keybinds(&self.mode_info, &self.tip_name, cols)
}
Expand Down
12 changes: 12 additions & 0 deletions default-plugins/status-bar/src/second_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ pub fn text_copied_hint(palette: &Palette) -> LinePart {
}
}

pub fn system_clipboard_error(palette: &Palette) -> LinePart {
let hint = " Error using the system clipboard.";
let red_color = match palette.red {
PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
PaletteColor::EightBit(color) => Fixed(color),
};
LinePart {
part: Style::new().fg(red_color).bold().paint(hint).to_string(),
len: hint.len(),
}
}

pub fn fullscreen_panes_to_hide(palette: &Palette, panes_to_hide: usize) -> LinePart {
let white_color = match palette.white {
PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
Expand Down
1 change: 1 addition & 0 deletions zellij-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.4.14"
typetag = "0.1.7"
chrono = "0.4.19"
close_fds = "0.3.2"
copypasta-ext = "0.3.7"

[target.'cfg(target_os = "macos")'.dependencies]
darwin-libproc = "0.2.0"
Expand Down
5 changes: 5 additions & 0 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub(crate) struct Screen {
colors: Palette,
draw_pane_frames: bool,
session_is_mirrored: bool,
use_system_clipboard: bool,
}

impl Screen {
Expand All @@ -204,6 +205,7 @@ impl Screen {
mode_info: ModeInfo,
draw_pane_frames: bool,
session_is_mirrored: bool,
use_system_clipboard: bool,
) -> Self {
Screen {
bus,
Expand All @@ -219,6 +221,7 @@ impl Screen {
default_mode_info: mode_info,
draw_pane_frames,
session_is_mirrored,
use_system_clipboard,
}
}

Expand Down Expand Up @@ -491,6 +494,7 @@ impl Screen {
self.connected_clients.clone(),
self.session_is_mirrored,
client_id,
self.use_system_clipboard,
);
tab.apply_layout(layout, new_pids, tab_index, client_id);
if self.session_is_mirrored {
Expand Down Expand Up @@ -692,6 +696,7 @@ pub(crate) fn screen_thread_main(
),
draw_pane_frames,
session_is_mirrored,
config_options.use_system_clipboard.unwrap_or(false),
);
loop {
let (event, mut err_ctx) = screen
Expand Down
31 changes: 26 additions & 5 deletions zellij-server/src/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use zellij_utils::{
pane_size::{Offset, PaneGeom, Size, Viewport},
};

use copypasta_ext::display::DisplayServer;

// FIXME: This should be replaced by `RESIZE_PERCENT` at some point
const MIN_TERMINAL_HEIGHT: usize = 5;
const MIN_TERMINAL_WIDTH: usize = 5;
Expand Down Expand Up @@ -119,6 +121,7 @@ pub(crate) struct Tab {
session_is_mirrored: bool,
pending_vte_events: HashMap<RawFd, Vec<VteBytes>>,
selecting_with_mouse: bool,
use_system_clipboard: bool,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -299,6 +302,7 @@ impl Tab {
connected_clients_in_app: Rc<RefCell<HashSet<ClientId>>>,
session_is_mirrored: bool,
client_id: ClientId,
use_system_clipboard: bool,
) -> Self {
let panes = BTreeMap::new();

Expand Down Expand Up @@ -335,6 +339,7 @@ impl Tab {
connected_clients_in_app,
connected_clients,
selecting_with_mouse: false,
use_system_clipboard,
}
}

Expand Down Expand Up @@ -1913,11 +1918,23 @@ 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(),
);
if self.use_system_clipboard {
let res = DisplayServer::select()
.try_context()
.ok_or_else(|| "could not get clipboard provider".into())
.and_then(|mut ctx| ctx.set_contents(selection.to_owned()));

if res.is_err() {
system_clipboard_failure = true
}
} else {
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
Expand All @@ -1927,7 +1944,11 @@ impl Tab {
.send_to_plugin(PluginInstruction::Update(
None,
None,
Event::CopyToClipboard,
if system_clipboard_failure {
Event::SystemClipboardFailure
} else {
Event::CopyToClipboard
},
))
.unwrap();
}
Expand Down
2 changes: 2 additions & 0 deletions zellij-server/src/tab/unit/tab_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 use_system_clipboard = false;
let mut tab = Tab::new(
index,
position,
Expand All @@ -110,6 +111,7 @@ fn create_new_tab(size: Size) -> Tab {
connected_clients,
session_is_mirrored,
client_id,
use_system_clipboard,
);
tab.apply_layout(
LayoutTemplate::default().try_into().unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ fn create_new_screen(size: Size) -> Screen {
let mode_info = ModeInfo::default();
let draw_pane_frames = false;
let session_is_mirrored = true;
let use_system_clipboard = false;
Screen::new(
bus,
&client_attributes,
max_panes,
mode_info,
draw_pane_frames,
session_is_mirrored,
use_system_clipboard,
)
}

Expand Down
1 change: 1 addition & 0 deletions zellij-tile/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub enum Event {
Mouse(Mouse),
Timer(f64),
CopyToClipboard,
SystemClipboardFailure,
InputReceived,
Visible(bool),
}
Expand Down
11 changes: 11 additions & 0 deletions zellij-utils/src/input/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub struct Options {
pub on_force_close: Option<OnForceClose>,
#[structopt(long)]
pub scroll_buffer_size: Option<usize>,

/// Switch to using xclip for clipboard instead of OSC52
#[structopt(long)]
#[serde(default)]
pub use_system_clipboard: Option<bool>,
}

impl Options {
Expand All @@ -99,6 +104,7 @@ impl Options {
let theme = other.theme.or_else(|| self.theme.clone());
let on_force_close = other.on_force_close.or(self.on_force_close);
let scroll_buffer_size = other.scroll_buffer_size.or(self.scroll_buffer_size);
let use_system_clipboard = other.use_system_clipboard.or(self.use_system_clipboard);

Options {
simplified_ui,
Expand All @@ -111,6 +117,7 @@ impl Options {
mirror_session,
on_force_close,
scroll_buffer_size,
use_system_clipboard,
}
}

Expand All @@ -133,6 +140,8 @@ impl Options {
let mouse_mode = merge_bool(other.mouse_mode, self.mouse_mode);
let pane_frames = merge_bool(other.pane_frames, self.pane_frames);
let mirror_session = merge_bool(other.mirror_session, self.mirror_session);
let use_system_clipboard =
merge_bool(other.use_system_clipboard, self.use_system_clipboard);

let default_mode = other.default_mode.or(self.default_mode);
let default_shell = other.default_shell.or_else(|| self.default_shell.clone());
Expand All @@ -152,6 +161,7 @@ impl Options {
mirror_session,
on_force_close,
scroll_buffer_size,
use_system_clipboard,
}
}

Expand Down Expand Up @@ -200,6 +210,7 @@ impl From<CliOptions> for Options {
mirror_session: opts.mirror_session,
on_force_close: opts.on_force_close,
scroll_buffer_size: opts.scroll_buffer_size,
use_system_clipboard: opts.use_system_clipboard,
}
}
}

0 comments on commit ffee5b1

Please sign in to comment.