summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/isdarktty13
1 files changed, 7 insertions, 6 deletions
diff --git a/bin/isdarktty b/bin/isdarktty
index 779f6c8..a09579b 100755
--- a/bin/isdarktty
+++ b/bin/isdarktty
@@ -1,13 +1,14 @@
#!/bin/sh
-# Check darkness by computing luminance of the current terminal.
-# see https://en.wikipedia.org/wiki/Luma_(video)
+# Detect terminal darkness. Return 0 if dark, else 1.
-trap "stty $(stty -g)" EXIT
-stty raw -echo min 0 time 0
-printf '\e]11;?\e\\'
-sleep 0.01
+trap "stty $(stty -g)" EXIT # restore terminal settings at exit
+stty raw -echo min 0 time 0 # set terminal in raw mode for query
+printf '\e]11;?\e\\' # xterm escape sequence to get bg color
+sleep 0.01 # 10ms delay to let terminal respond
+# parse terminal response in form of: ...rgb:cf00/ff00/ff00...
+# luma equation adapted from https://en.wikipedia.org/wiki/Luma_(video)
awk -v FS='' -v hex=123456789abcdef '{
r = (16 * index(hex, $10) + index(hex, $11)) / 255
g = (16 * index(hex, $15) + index(hex, $16)) / 255