-
Notifications
You must be signed in to change notification settings - Fork 4
/
xcmenuctrl
executable file
·95 lines (83 loc) · 2.4 KB
/
xcmenuctrl
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
#!/bin/bash
# dmenu access to xcmenu daemon
#
# binaries and their parameters
if [[ -f "$HOME/.dmenurc" ]]; then
. "$HOME/.dmenurc"
fi
DMENU="${XCMENUCTRL_DMENU:-dmenu -i -l 15}"
XCMENU="xcmenu"
# clipboard to copy to
# primary = XA_PRIMARY
# secondary = XA_SECONDARY
# clipboard = XA_CLIPBOARD
CLIPBOARD="clipboard"
err() { echo -e "$@" 1>&2; exit 1; }
# usage
usage() {
cat << EOF
usage: $(basename $@) [-us]
-u : url mode
-s : sync selection to another selection
-spc : sync primary to clipboard
-scp : sync clipboard to primary
-ssc : sync secondary to clipboard
-scs : sync clipboard to secondary
-sps : sync primary to secondary
-ssp : sync secondary to primary
EOF
exit 0
}
# urls
urlmode() {
local protos="https?|ftps?|mailto|ssh|git|file"
local url="$("$XCMENU" -l | grep -aoP "((($protos):\/\/|www\..*\.)[-/.\?=&%()#@~:\w]+)" | $DMENU -p "loliurl")"
[[ -n "$url" ]] || exit 1
printf %s "#LC:skip_history:$url" | "$XCMENU" -i --${CLIPBOARD}
echo "$url"
exit 0
}
# syncmode
syncmode() {
local synf="$(echo -e "primary\nsecondary\nclipboard" | $DMENU -p "sync_from")"
[[ -n "$synf" ]] || exit 1
local synt="$(echo -e "primary\nsecondary\nclipboard" | $DMENU -p "sync_to")"
[[ -n "$synt" ]] || exit 1
local data="$("$XCMENU" --${synf})"
printf %s "$data" | "$XCMENU" -i --${synt}
echo "$data"
exit 0
}
# clipmode
clipmode() {
id="$("$XCMENU" -m | $DMENU -p "xcmenu" | sed 's/^[^0-9]*\([^:]*\):.*/\1/g')"
[[ -n "$id" ]] || exit 1
local data="$(echo "$id" | "$XCMENU" -g)"
printf %s "$data" | "$XCMENU" -i --${CLIPBOARD}
echo "$data"
exit 0
}
# sync
sync() {
local data="$("$XCMENU" --$1)"
printf %s "$data" | "$XCMENU" -i --$2
echo "$data"
exit 0;
}
# main
main() {
[[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] && usage "$0"
"$XCMENU" -q &> /dev/null ||
err "xcmenu returned non zero.\ncheck that it's installed and daemon is running."
[[ "$1" == "-u" ]] && urlmode
[[ "$1" == "-spc" ]] && sync "primary" "clipboard"
[[ "$1" == "-scp" ]] && sync "clipboard" "primary"
[[ "$1" == "-ssc" ]] && sync "secondary" "clipboard"
[[ "$1" == "-scs" ]] && sync "clipboard" "secondary"
[[ "$1" == "-sps" ]] && sync "primary" "secondary"
[[ "$1" == "-ssp" ]] && sync "secondary" "primary"
[[ "$1" == "-s" ]] && syncmode
clipmode
}
main "$@"
# vim: set ts=8 sw=3 tw=0 :