diff options
| author | Marc Vertes <mvertes@free.fr> | 2025-08-26 09:15:26 +0200 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2025-08-26 09:15:26 +0200 |
| commit | b108b851105f0cc72e24c65a84d7d77d7fd202c4 (patch) | |
| tree | 63ab9af1169ece546f8931bdc6f0c9cbdc7b51c7 /bin | |
| parent | 5bb833eeea685a660cc9b664420f764ae20a6587 (diff) | |
improve bin/isdarktty
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/isdarktty | 13 |
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 |
