summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2025-08-25 13:11:36 +0200
committerMarc Vertes <mvertes@free.fr>2025-08-25 13:11:36 +0200
commitd1403578402a0694457da8b5f628d37c4f3836f4 (patch)
treedab09810ce48d74cdeecce2faa2c1ea908607d10 /bin
parent20bad3245424e6a6b2372925c3f9e8da01a2fd75 (diff)
add bin/isdarktty
Diffstat (limited to 'bin')
-rwxr-xr-xbin/isdarktty20
1 files changed, 20 insertions, 0 deletions
diff --git a/bin/isdarktty b/bin/isdarktty
new file mode 100755
index 0000000..8d01b89
--- /dev/null
+++ b/bin/isdarktty
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+# compute luminance of the current terminal
+
+oldstty=$(stty -g)
+stty raw -echo min 0 time 0
+printf "\e]11;?\e\\"
+sleep 0.01
+read -r answer
+stty "$oldstty"
+
+awk -v s="$answer" 'BEGIN {
+ hex="123456789abcdef"
+ split(s, a, "")
+ r = (16 * index(hex, a[10]) + index(hex, a[11])) / 255
+ g = (16 * index(hex, a[15]) + index(hex, a[16])) / 255
+ b = (16 * index(hex, a[20]) + index(hex, a[21])) / 255
+ # print 0.33 * r + 0.5 * g + 0.16 * b
+ exit (0.33 * r + 0.5 * g + 0.16 * b > 0.5)
+}'