-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxrandr-smart
executable file
·104 lines (76 loc) · 2 KB
/
xrandr-smart
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
#!/usr/bin/env bash
# Wrapper around xrandr which allows shell globs in --output and automatically
# disables all other outputs.
#
# see https://work.lisk.in/2020/10/11/xrandr-ux.html
set -eu -o pipefail
export LC_ALL=C
unset xrandr_query
function o { printf -->&2 "%s:%s\\n" "${0##*/}" "$(printf " %q" "$@")"; "$@"; }
function get-xrandr-query {
[[ "${xrandr_query-}" ]] && return
xrandr_query=$(xrandr --query)
}
function get-xrandr-outputs {
<<<"$xrandr_query" grep -P -o '^([\w-]+)(?= (dis)?connected)'
}
function get-xrandr-connected-outputs {
<<<"$xrandr_query" grep -P -o '^([\w-]+)(?= connected)'
}
function get-xrandr-outputs-except {
get-xrandr-outputs | grep -F -v -x -f <(printf "%s\n" "$@")
}
function find-one {
local tmp one=
while read -r tmp; do
# shellcheck disable=SC2053
if [[ $tmp == $1 ]]; then
[[ $one ]] && return 1
one="$tmp"
fi
done
[[ $one ]] && printf "%s\n" "$one"
}
function find-xrandr-output { get-xrandr-outputs | find-one "$1"; }
function find-xrandr-connected-output { get-xrandr-connected-outputs | find-one "$1"; }
function xrandr-auto-off {
local -a outputs=()
local output
local -a args=()
local arg
while (( $# )); do
arg="$1"; shift
args+=("$arg")
[[ $arg == --output ]] && outputs+=("$1")
done
if ! (( ${outputs[@]+${#outputs[@]}} )); then
printf -->&2 "Aborting, would disable all outputs.\n"
exit 1
fi
for output in $(get-xrandr-outputs-except ${outputs[@]+"${outputs[@]}"}); do
args+=(--output "$output" --off)
done
o xrandr "${args[@]}"
}
function xrandr-auto-find {
local -a args=()
local arg
while (( $# )); do
arg="$1"; shift
args+=("$arg")
if [[ $arg == --@(output|left-of|right-of|above|below|same-as) ]] && (( $# )); then
arg="$1"; shift
arg=$(find-xrandr-connected-output "$arg")
args+=("$arg")
fi
done
o xrandr-auto-off "${args[@]}"
}
get-xrandr-query
# skip main if sourced
return 0 2>/dev/null || :
if (( ! $# )); then
echo "Usage: xmonad-smart --output …"
exit 1
fi
xrandr-auto-find "$@"