forked from cdown/clipmenu
/
keratin
executable file
·243 lines (211 loc) · 5.1 KB
/
keratin
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
LC_COLLATE=C
LOGDIR="/tmp"
LOGFILE="$LOGDIR/keratin.log"
CACHE_DIR="$HOME/.local/share/keratin"
P="keratin"
first_selection() {
declare -A selections
ordered_selections=()
files=("$CACHE_DIR/"*)
for (( i=${#files[@]}-1; i>=0; i-- )); do
file=${files[$i]}
first_line=$(sed -n '/./{p;q}' "$file")
lines=$(wc -l "$file")
lines=${lines%% *}
if (( lines > 1 )); then
first_line+=" ($lines lines)"
fi
if
[ -z "$first_line" ]
then
rm $file
else
ordered_selections+=("$first_line")
selections[$first_line]=$file
fi
done
echo "${selections[${ordered_selections[0]}]}"
}
dmenu_options() {
declare -A selections
ordered_selections=()
files=("$CACHE_DIR/"*)
for (( i=${#files[@]}-1; i>=0; i-- )); do
file=${files[$i]}
first_line=$(sed -n '/./{p;q}' "$file")
lines=$(wc -l "$file")
lines=${lines%% *}
if (( lines > 1 )); then
first_line+=" ($lines lines)"
fi
if
[ -z "$first_line" ]
then
rm $file
else
ordered_selections+=("$first_line")
selections[$first_line]=$file
fi
done
chosen_line=$(
printf '%s\n' "${ordered_selections[@]}" | dmenu -l 15 -p $P $FN $NB $NF $SB $SF "$@"
)
echo ${selections[$chosen_line]}
}
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Usage: keratin [OPTION]"
echo "Dmenu powered clipboard manager for Linux systems."
echo "Run without any flags to try to access the daemon and copying history."
echo ""
echo " -c, --current Returns the most recently copied/cut item in the clipboard"
echo " -C, --copy Copies argument to clipboard"
echo " -d, --daemon Start the keratin daemon"
echo " -D, --delete Delete clipboard entry"
echo " -fn [font] Defines the font or font set used for dmenu."
echo " eg. \"fixed\" or \"Monospace-12:normal\" (an xft font)"
echo " -h, --help Print this help guide and exit"
echo " -K, --kill-daemon Stop the keratin daemon"
echo " -nb [color] Defines the normal background color for dmenu."
echo " #RGB, #RRGGBB, and X color names are supported"
echo " -nf [color] Defines the normal foreground color for dmenu"
echo " -s, --daemon-status Stop the keratin daemon"
echo " -sb [color] Defines the selected background color for dmenu"
echo " -sf [color] Defines the selected foreground color for dmenu"
exit 0
;;
-c|--current)
if type -p xsel >/dev/null 2>&1; then
xsel --clipboard
else
xclip -o -sel clipboard
fi
exit 0
;;
-C|--copy)
shift
if type -p xsel >/dev/null 2>&1; then
echo -n "$1" | xsel -i --clipboard
else
echo -n "$1" | xclip -sel clipboard
fi
exit 0
;;
-d|--daemon)
if
[[ `ps -A | grep keratin-d` == *[^[:blank:]]* ]]
then
echo "Daemon already running."
exit 0
else
keratin-d >> $LOGFILE 2>&1 &
if
[[ `ps -A | grep keratin-d` == *[^[:blank:]]* ]]
then
echo "Daemon started."
exit 0
else
echo "Unknown error occurred; daemon did not start."
exit 1
fi
fi
;;
-D|--delete)
fs=$(first_selection)
P="DELETION"
chosen_file=$(dmenu_options)
rm "$chosen_file"
if
[ "$chosen_file" == "$fs" ]
then
fs=$(first_selection)
if type -p xsel >/dev/null 2>&1; then
xsel -i --clipboard < "$fs"
else
xclip -sel clipboard < "$fs"
fi
rm "$fs"
fi
exit 0
;;
-K|--kill-daemon)
if
[[ `ps -A | grep keratin-d` == *[^[:blank:]]* ]]
then
kill `ps -A | grep keratin-d | awk '{print $1;}'`
echo "Daemon stopped."
exit 0
else
echo "No daemon running."
exit 0
fi
;;
-s|--daemon-status)
if
[[ `ps -A | grep keratin-d` == *[^[:blank:]]* ]]
then
echo "Daemon running."
exit 0
else
echo "No daemon running."
exit 0
fi
;;
-fn)
shift
FN="-fn $1"
shift
;;
-nb)
shift
NB="-nb $1"
shift
;;
-nf)
shift
NF="-nf $1"
shift
;;
-sb)
shift
SB="-sb $1"
shift
;;
-sf)
shift
SF="-sf $1"
shift
;;
*)
echo "keratin should take no arguments other than accepted flags and accepted
arguements to said flags. Either invalid flags or a disproportionate number of
arguments-to-flags were used."
exit 1
;;
esac
done
if
[[ ! `ps -A | grep keratin-d` == *[^[:blank:]]* ]]
then
echo "Daemon not running; run \"keratin -d\" or \"keratin --daemon\" to correct this."
exit 1
fi
chosen_file=$(dmenu_options)
[[ $chosen_file ]] || exit 1
if type -p xsel >/dev/null 2>&1; then
data=$(xsel --clipboard; printf x)
else
data=$(xclip -o -sel clipboard; printf x)
fi
if
[ "${data%x}" != "`cat $chosen_file`" ]
then
if type -p xsel >/dev/null 2>&1; then
xsel -i --clipboard < "$chosen_file"
else
xclip -sel clipboard < "$chosen_file"
fi
rm "$chosen_file"
fi