-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker
executable file
·80 lines (64 loc) · 1.79 KB
/
locker
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
#!/usr/bin/env bash
# ╻ ┏━┓┏━╸╻┏ ┏━╸┏━┓
# ┃ ┃ ┃┃ ┣┻┓┣╸ ┣┳┛
# ┗━╸┗━┛┗━╸╹ ╹┗━╸╹┗╸
# simple locker with xautolock and zzz
set -o errexit
set -o pipefail
set -o nounset
PROG=$(basename "$0")
DEPS=(xautolock zzz setsid)
SUSPEND_TIME=30
LOCKER="sudo zzz"
CMD="xautolock"
function log_err {
printf "%s: %s\n" "$PROG" "$*"
exit 1
}
function send_notification {
local prog
local icon="system-lock-screen-symbolic"
local mesg="<b>$1</b>"
prog=$(echo "$PROG" | tr '[:lower:]' '[:upper:]')
notify-send -i "$icon" "$prog" "$mesg"
}
function is_running {
if ! pgrep -x "$CMD" >/dev/null; then
return 1
fi
return 0
}
function kill_locker {
pkill -x "$CMD"
}
function usage {
cat <<-END
[0;1;34;94m╻[0m [0;1;34;94m┏━┓┏━╸╻┏[0m [0;1;34;94m┏━╸┏[0;34m━┓[0m
[0;1;34;94m┃[0m [0;1;34;94m┃[0m [0;1;34;94m┃┃[0m [0;34m┣┻┓┣╸[0m [0;34m┣┳┛[0m
[0;34m┗━╸┗━┛┗━╸╹[0m [0;34m╹┗━╸╹[0;37m┗╸[0m
simple locker with xautolock and zzz
Usage: $PROG <timer in minutes> (default: 30m | max: 60m)
END
}
function main {
local timer dep
timer="${1:-}"
for dep in "${DEPS[@]}"; do
if ! command -v "$dep" >/dev/null; then
local err_msg="'$dep' not found"
send_notification "$err_msg"
log_err "$err_msg"
fi
done
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
usage
exit
fi
if is_running; then
kill_locker
fi
[[ -n "$timer" ]] && SUSPEND_TIME=$timer
setsid -f "$CMD" -time "$SUSPEND_TIME" -locker "$LOCKER" -detectsleep
send_notification "<i>Setting locker to</i> ${SUSPEND_TIME}m"
}
main "$@"