forked from fdw/rofimoji
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaltcode
More file actions
executable file
·184 lines (161 loc) · 5.38 KB
/
Copy pathaltcode
File metadata and controls
executable file
·184 lines (161 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ARGS=("$@")
USAGE="$(basename "$0") -o|--output [xdotool|clipboard|stdout|-] -s|--selector [fzf|rofi] -t|--skin-tone [neutral|light|medium-light|moderate|dark-brown|black]
By default, will prompt a user to select an special character to copy to the clipboard using fzf if in a terminal, or rofi in a gui.
where:
Options:
-o|--output (optional) - Instead of outputting to stdout, output to one of: xdotool (auto type), clipboard, or stdout, -
-s|--selector (optional) - Uses fzf for CLI environments or rofi for GUIs. Override by specifying one of: fzf, rofi
-t|--skin-tone (optional) A skin to to apply to select emojis: neutral, light, medium-light, moderate, dark-brown, black
"
while [[ "${#ARGS[@]}" -gt 0 ]]; do
KEY="${ARGS[0]}"
VALUE="${ARGS[1]}"
case "${KEY}" in
-h|--help)
echo "$USAGE">&2
exit 0
;;
-t|--skin-tone)
SKIN_KEY="$VALUE"
case "${SKIN_KEY}" in
"neutral")
SKIN_TONE=""
;;
"light")
SKIN_TONE="🏻"
;;
"medium-light")
SKIN_TONE="🏼"
;;
"moderate")
SKIN_TONE="🏽"
;;
"dark-brown")
SKIN_TONE="🏾"
;;
"black")
SKIN_TONE="🏿"
;;
*)
SKIN_TONE=""
;;
esac
ARGS=("${ARGS[@]:2}")
;;
-s|--selector)
SELECTOR="$VALUE"
ARGS=("${ARGS[@]:2}")
;;
-o|--output)
OUTPUT_STRATEGY="$VALUE"
ARGS=("${ARGS[@]:2}")
;;
--)
ARGS=("${ARGS[@]:1}")
break
;;
*) # Unknown Option or "key=value"
while IFS='=' read -ra SPLIT <<< ${KEY}; do
# Split by the "=" and add back to our arguments array
for i in "${SPLIT[@]}"; do
ARGS+=("${i}")
done
FOUND_SPLIT=1
ARGS=("${ARGS[@]:1}")
break
done
if [[ -z "${FOUND_SPLIT}" ]]; then
echo "Unknown option $1"
echo "${USAGE}"
exit 1
fi
;;
esac
done
CLI_ENV=$([[ -t 0 ]]; echo $?)
# Set SELECTOR Default (smart based on our environment)
if [[ -z "$SELECTOR" ]]; then
if [[ $CLI_ENV == 0 ]]; then
SELECTOR="fzf"
else
SELECTOR="rofi"
fi
fi
# Set OUTPUT_STRATEGY Default (smart based on our environment)
if [[ -z "$OUTPUT_STRATEGY" ]]; then
if [[ $CLI_ENV == 0 ]]; then
OUTPUT_STRATEGY="stdout"
else
OUTPUT_STRATEGY="xdotool"
fi
fi
altcode_files="$DIR/altcode.d/"*.txt
function select_char() {
# Prompt user to select from list of special characters
case "${SELECTOR}" in
fzf)
cat - | fzf
;;
rofi)
cat - | rofi -dmenu -markup-rows -i -multi-select -p '😀' -kb-custom-1 'Alt+c' -kb-custom-2 'Alt+t' -kb-custom-3 'Alt+p'
;;
*)
echo "Unknown selection strategy: ${SELECTOR}" >&2
exit 1
esac
}
function output_strategy_pre() {
# Pre-selection hook for output strategy
if [[ "$OUTPUT_STRATEGY" == "xdotool" ]] || [[ "$OUTPUT_STRATEGY" == "copypaste" ]]; then
X_ACTIVE_WINDOW=$(xdotool getactivewindow)
fi
}
function output_strategy() {
character="$1"
# Where to send our output
case "${OUTPUT_STRATEGY}" in
xdotool)
xdotool windowactivate --sync "$X_ACTIVE_WINDOW" type --delay 100 "$character"
;;
copypaste)
set +e
# If old target was an image or something this will fail. We tried our best to restore our old clipboard
# values
old_clipboard_content_primary="$(xclip -o -selection primary)"
old_clipboard_content_clipboard="$(xclip -o -selection clipboard)"
set -e
echo -n "$character" | xclip -i -selection primary
echo -n "$character" | xclip -i -selection clipboard
xdotool windowactivate --sync $X_ACTIVE_WINDOW key --delay 100 --clearmodifiers Shift+Insert sleep 0.05
# Put clipboard contents back
echo -n "$old_clipboard_content_primary" | xclip -i -selection primary
echo -n "$old_clipboard_content_clipboard" | xclip -i -selection clipboard
;;
clipboard)
cat "$character" | xclip -i -selection clipboard
;;
stdout|-)
cat "$character"
;;
*)
echo "Unknown output strategy: $OUTPUT_STRATEGY" >&2
exit 1
esac
}
output_strategy_pre
selected="$(cat $altcode_files | select_char)"
special_char=$(echo "$selected" | sed -n -E -e 's/^(.+) \| [^\|]+$/\1/p' | tr -d '\n')
special_char_len=$(
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
echo ${#special_char}
LANG=$oLang LC_ALL=$oLcAll
)
if grep -Fxq "$special_char" "$DIR/altcode.d/emoji_modifiers.meta"; then
output_strategy "${special_char}${SKIN_TONE}"
else
output_strategy "${special_char}"
fi