Skip to content

Commit f0b0dc9

Browse files
author
Bruno Sutic
committed
Add error handling
1 parent 2aa6c06 commit f0b0dc9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

scripts/tmux_yank_error_message.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
error_message="tmux_yank error! Clipboard command not found."
4+
5+
# Ensures a message is displayed for 5 seconds in tmux prompt.
6+
# Does not override the 'display-time' tmux option.
7+
display_message() {
8+
local message="$1"
9+
10+
# display_duration defaults to 5 seconds, if not passed as an argument
11+
if [ "$#" -eq 2 ]; then
12+
local display_duration="$2"
13+
else
14+
local display_duration="5000"
15+
fi
16+
17+
# saves user-set 'display-time' option
18+
local saved_display_time=$(get_tmux_option "display-time" "750")
19+
20+
# sets message display time to 5 seconds
21+
tmux set-option -gq display-time "$display_duration"
22+
23+
# displays message
24+
tmux display-message "$message"
25+
26+
# restores original 'display-time' value
27+
tmux set-option -gq display-time "$saved_display_time"
28+
}
29+
30+
main() {
31+
display_message "$error_message"
32+
}
33+
main

yank.tmux

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
35
yank_default="y"
46
yank_option="@yank"
57

@@ -45,6 +47,23 @@ clipboard_copy_command() {
4547
fi
4648
}
4749

50+
set_error_bindings() {
51+
local key_bindings="$(yank_key) $(put_key) $(yank_put_key)"
52+
local key
53+
for key in $key_bindings; do
54+
tmux bind-key -t vi-copy "$key" copy-pipe "$CURRENT_DIR/scripts/tmux_yank_error_message.sh"
55+
tmux bind-key -t emacs-copy "$key" copy-pipe "$CURRENT_DIR/scripts/tmux_yank_error_message.sh"
56+
done
57+
}
58+
59+
error_handling_if_command_not_present() {
60+
local copy_command="$1"
61+
if [ -z "$copy_command" ]; then
62+
set_error_bindings
63+
exit 0
64+
fi
65+
}
66+
4867
set_bindings() {
4968
local copy_command="$1"
5069
tmux bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command"
@@ -58,6 +77,7 @@ set_bindings() {
5877

5978
main() {
6079
local copy_command="$(clipboard_copy_command)"
80+
error_handling_if_command_not_present "$copy_command"
6181
set_bindings "$copy_command"
6282
}
6383
main

0 commit comments

Comments
 (0)