-
Notifications
You must be signed in to change notification settings - Fork 2
/
zc-all
100 lines (83 loc) · 3.05 KB
/
zc-all
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
setopt localoptions extendedglob clobber
integer idx is_locked counter=0
local name busyfile busywith
__zconvey_usage_zc-all() {
__zconvey_pinfo2 "Sends command to all unbusy sessions (also busy when -f passed)"
__zconvey_pinfo "Usage: zc-all [-h|--help] [-f|--force] [-a|--ask] [-q|--quiet] [-v|--verbose] COMMAND ARGUMENT ..."
print -- "-h/--help - this message"
print -- "-q/--quiet - don't output status messages"
print -- "-v/--verbose - output more status messages"
print -- "-a/--ask - ask for command (if not provided)"
print -- "-f/--force - send also to busy sessions; command can time out"
}
local -A opthash
zparseopts -D -A opthash h -help q -quiet v -verbose f -force a -ask || { __zconvey_usage_zc-all; return 1; }
integer verbose=0 quiet=0 force=0 ask=0
# Help
(( ${+opthash[-h]} + ${+opthash[--help]} )) && { __zconvey_usage_zc-all; return 0; }
# ASK (requests command and/or ID)
(( ask = ${+opthash[-a]} + ${+opthash[--ask]} ))
if [ "$ask" = "0" ]; then
local ask_setting
zstyle -b ":plugin:zconvey" ask ask_setting || ask_setting="no"
[ "$ask_setting" = "yes" ] && ask=1 || ask=0
fi
# VERBOSE, QUIET, FORCE
(( verbose = ${+opthash[-v]} + ${+opthash[--verbose]} ))
(( quiet = ${+opthash[-q]} + ${+opthash[--quiet]} ))
(( force = ${+opthash[-f]} + ${+opthash[--force]} ))
local -a args
args=( "${(q)@}" )
local cmd="${args[*]}"
cmd="${cmd//\\;/;}"
cmd="${cmd//\\|/|}"
cmd="${cmd//\\&/&}"
cmd="${cmd##[[:space:]]#}"
cmd="${cmd%%[[:space:]]#}"
# Resupply missing input if requested
if (( $ask )); then
# Supply command?
if [[ -z "$cmd" ]]; then
vared -cp "Enter command to send: " cmd
cmd="${cmd##[[:space:]]#}"
cmd="${cmd%%[[:space:]]#}"
if [ -z "$cmd" ]; then
__zconvey_pinfo "No command enterred, exiting"
return 0
fi
fi
fi
if [ -z "$cmd" ]; then
__zconvey_pinfo "No command provided, aborting"
return 1
fi
for (( idx = 1; idx <= 100; idx ++ )); do
name=""
busywith=""
is_locked=0
__zconvey_is_session_active "$idx" && is_locked=1 || is_locked=0
if (( is_locked )); then
busyfile="$ZCONVEY_OTHER_DIR/${idx}.busy"
if [[ -e "$busyfile" && "$idx" != "$ZCONVEY_ID" ]]; then
busywith="\033[1;33m$(<$busyfile)\033[0m"
__zconvey_get_name_of_id "$idx"
if (( force )); then
counter+=1
(( quiet )) || print "\033[1;35mSending to busy\033[0m session $idx (name: $REPLY, busy with: $busywith)"
zc -qi "$idx" -r "${cmd[@]}"
else
(( quiet )) || print "Session $idx (name: $REPLY) busy ($busywith)"
fi
elif [[ "$idx" != "$ZCONVEY_ID" ]]; then
counter+=1
if (( verbose )); then
zc -i "$idx" -r "${cmd[@]}"
else
zc -qi "$idx" -r "${cmd[@]}"
fi
fi
fi
done
(( quiet )) || __zconvey_pinfo "Sent command to $counter other sessions"
return 0
# vim:ft=zsh