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

Indicate to the user when text is copied to the clipboard #642

Merged
merged 7 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 24 additions & 5 deletions default-plugins/status-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zellij_tile::prelude::*;
use zellij_tile_utils::style;

use first_line::{ctrl_keys, superkey};
use second_line::keybinds;
use second_line::{keybinds, text_copied_hint};

// for more of these, copy paste from: https://en.wikipedia.org/wiki/Box-drawing_character
static ARROW_SEPARATOR: &str = "";
Expand All @@ -17,6 +17,7 @@ static MORE_MSG: &str = " ... ";
#[derive(Default)]
struct State {
mode_info: ModeInfo,
diplay_text_copied_hint: bool,
}

register_plugin!(State);
Expand Down Expand Up @@ -136,12 +137,26 @@ impl ZellijPlugin for State {
set_selectable(false);
set_invisible_borders(true);
set_fixed_height(2);
subscribe(&[EventType::ModeUpdate]);
subscribe(&[
EventType::ModeUpdate,
EventType::CopyToClipboard,
EventType::InputReceived,
]);
}

fn update(&mut self, event: Event) {
if let Event::ModeUpdate(mode_info) = event {
self.mode_info = mode_info;
dbg!("got event {:?}", &event);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops :)

match event {
Event::ModeUpdate(mode_info) => {
self.mode_info = mode_info;
}
Event::CopyToClipboard => {
self.diplay_text_copied_hint = true;
}
Event::InputReceived => {
self.diplay_text_copied_hint = false;
}
_ => {}
}
}

Expand All @@ -157,7 +172,11 @@ impl ZellijPlugin for State {
let ctrl_keys = ctrl_keys(&self.mode_info, cols - superkey.len, separator);

let first_line = format!("{}{}", superkey, ctrl_keys);
let second_line = keybinds(&self.mode_info, cols);
let second_line = if self.diplay_text_copied_hint {
text_copied_hint()
} else {
keybinds(&self.mode_info, cols)
};

// [48;5;238m is gray background, [0K is so that it fills the rest of the line
// [m is background reset, [0K is so that it clears the rest of the line
Expand Down
8 changes: 8 additions & 0 deletions default-plugins/status-bar/src/second_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,11 @@ pub fn keybinds(help: &ModeInfo, max_width: usize) -> LinePart {
}
best_effort_shortcut_list(help, max_width)
}

pub fn text_copied_hint() -> LinePart {
let hint = " Text copied to clipboard";
LinePart {
part: hint.into(),
len: hint.len(),
}
}
52 changes: 28 additions & 24 deletions zellij-client/src/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,36 @@ impl InputHandler {
let stdin_buffer = self.os_input.read_from_stdin();
for key_result in stdin_buffer.events_and_raw() {
match key_result {
Ok((event, raw_bytes)) => match event {
termion::event::Event::Key(key) => {
let key = cast_termion_key(key);
self.handle_key(&key, raw_bytes);
}
termion::event::Event::Mouse(me) => {
let mouse_event = zellij_utils::input::mouse::MouseEvent::from(me);
self.handle_mouse_event(&mouse_event);
}
termion::event::Event::Unsupported(unsupported_key) => {
// we have to do this because of a bug in termion
// this should be a key event and not an unsupported event
if unsupported_key == alt_left_bracket {
let key = Key::Alt('[');
Ok((event, raw_bytes)) => {
self.os_input
.send_to_server(ClientToServerMsg::InputReceived);
match event {
termion::event::Event::Key(key) => {
let key = cast_termion_key(key);
self.handle_key(&key, raw_bytes);
} else if unsupported_key == bracketed_paste_start {
self.pasting = true;
} else if unsupported_key == bracketed_paste_end {
self.pasting = false;
} else {
// this is a hack because termion doesn't recognize certain keys
// in this case we just forward it to the terminal
self.handle_unknown_key(raw_bytes);
}
}
},
termion::event::Event::Mouse(me) => {
let mouse_event = zellij_utils::input::mouse::MouseEvent::from(me);
self.handle_mouse_event(&mouse_event);
}
termion::event::Event::Unsupported(unsupported_key) => {
// we have to do this because of a bug in termion
// this should be a key event and not an unsupported event
if unsupported_key == alt_left_bracket {
let key = Key::Alt('[');
self.handle_key(&key, raw_bytes);
} else if unsupported_key == bracketed_paste_start {
self.pasting = true;
} else if unsupported_key == bracketed_paste_end {
self.pasting = false;
} else {
// this is a hack because termion doesn't recognize certain keys
// in this case we just forward it to the terminal
self.handle_unknown_key(raw_bytes);
}
}
};
}
Err(err) => panic!("Encountered read error: {:?}", err),
}
}
Expand Down
8 changes: 8 additions & 0 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ pub(crate) fn route_thread_main(
}
}
ClientToServerMsg::ClientExited => break,
ClientToServerMsg::InputReceived => {
if let Some(rlocked_sessions) = rlocked_sessions.as_ref() {
rlocked_sessions
.senders
.send_to_plugin(PluginInstruction::Update(None, Event::InputReceived))
.unwrap();
}
}
}
}
}
6 changes: 6 additions & 0 deletions zellij-server/src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,9 @@ impl Tab {
let selected_text = self.get_active_pane().and_then(|p| p.get_selected_text());
if let Some(selected_text) = selected_text {
self.write_selection_to_clipboard(&selected_text);
self.senders
.send_to_plugin(PluginInstruction::Update(None, Event::CopyToClipboard))
.unwrap();
}
}

Expand All @@ -2389,6 +2392,9 @@ impl Tab {
self.senders
.send_to_server(ServerInstruction::Render(Some(output)))
.unwrap();
self.senders
.send_to_plugin(PluginInstruction::Update(None, Event::CopyToClipboard))
.unwrap();
}
}

Expand Down
2 changes: 2 additions & 0 deletions zellij-tile/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Event {
TabUpdate(Vec<TabInfo>),
KeyPress(Key),
Timer(f64),
CopyToClipboard,
InputReceived,
}

/// Describes the different input modes, which change the way that keystrokes will be interpreted.
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum ClientToServerMsg {
AttachClient(ClientAttributes, bool, Options),
Action(Action),
ClientExited,
InputReceived,
}

// Types of messages sent from the server to the client
Expand Down