diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-08 23:25:41 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-08 23:25:41 +0200 |
| commit | cd6644ea4ddc78597934ab0ef5ba50e3c3daa927 (patch) | |
| tree | 03de331a8db6386dfd6fa75155bfbcea6b4feaf3 /content/show-xterm-colors.md | |
| parent | 84ed124529ffeee1590295b8de3a8faf51848680 (diff) | |
| download | mitjafelicijan.com-cd6644ea4ddc78597934ab0ef5ba50e3c3daa927.tar.gz | |
Moved to a simpler SSG
Diffstat (limited to 'content/show-xterm-colors.md')
| -rw-r--r-- | content/show-xterm-colors.md | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/content/show-xterm-colors.md b/content/show-xterm-colors.md new file mode 100644 index 0000000..cc5eca4 --- /dev/null +++ b/content/show-xterm-colors.md | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | --- | ||
| 2 | title: Display xterm color palette | ||
| 3 | url: write-iso-usb.html | ||
| 4 | date: 2023-05-25T12:00:00+02:00 | ||
| 5 | type: note | ||
| 6 | draft: false | ||
| 7 | tags: [linux] | ||
| 8 | --- | ||
| 9 | |||
| 10 | - `bash xterm-palette.sh` - will show you number of max colors available | ||
| 11 | - `bash xterm-palette.sh -v` - will create a list of all colors with codes | ||
| 12 | |||
| 13 |  | ||
| 14 | |||
| 15 | ```sh | ||
| 16 | #!/usr/bin/env bash | ||
| 17 | # xterm-palette.sh | ||
| 18 | |||
| 19 | trap 'tput sgr0' exit # Clean up even if user hits ^C | ||
| 20 | |||
| 21 | function setfg () { | ||
| 22 | printf '\e[38;5;%dm' $1 | ||
| 23 | } | ||
| 24 | |||
| 25 | function setbg () { | ||
| 26 | printf '\e[48;5;%dm' $1 | ||
| 27 | } | ||
| 28 | |||
| 29 | function showcolors() { | ||
| 30 | # Given an integer, display that many colors | ||
| 31 | for ((i=0; i<$1; i++)) | ||
| 32 | do | ||
| 33 | printf '%4d ' $i | ||
| 34 | setbg $i | ||
| 35 | tput el | ||
| 36 | tput sgr0 | ||
| 37 | echo | ||
| 38 | done | ||
| 39 | tput sgr0 el | ||
| 40 | } | ||
| 41 | |||
| 42 | # First, test if terminal supports OSC 4 at all. | ||
| 43 | printf '\e]4;%d;?\a' 0 | ||
| 44 | read -d $'\a' -s -t 0.1 </dev/tty | ||
| 45 | if [ -z "$REPLY" ] | ||
| 46 | then | ||
| 47 | # OSC 4 not supported, so we'll fall back to terminfo | ||
| 48 | max=$(tput colors) | ||
| 49 | else | ||
| 50 | # OSC 4 is supported, so use it for a binary search | ||
| 51 | min=0 | ||
| 52 | max=256 | ||
| 53 | while [[ $((min+1)) -lt $max ]] | ||
| 54 | do | ||
| 55 | i=$(( (min+max)/2 )) | ||
| 56 | printf '\e]4;%d;?\a' $i | ||
| 57 | read -d $'\a' -s -t 0.1 </dev/tty | ||
| 58 | if [ -z "$REPLY" ] | ||
| 59 | then | ||
| 60 | max=$i | ||
| 61 | else | ||
| 62 | min=$i | ||
| 63 | fi | ||
| 64 | done | ||
| 65 | fi | ||
| 66 | |||
| 67 | |||
| 68 | # If -v is given, show all the colors | ||
| 69 | case ${1-none} in | ||
| 70 | none) | ||
| 71 | echo $max | ||
| 72 | ;; | ||
| 73 | -v) | ||
| 74 | showcolors $max | ||
| 75 | ;; | ||
| 76 | *) | ||
| 77 | if [[ "$1" -gt 0 ]]; then | ||
| 78 | showcolors $1 | ||
| 79 | else | ||
| 80 | echo $max | ||
| 81 | fi | ||
| 82 | ;; | ||
| 83 | esac | less --raw-control-chars --QUIT-AT-EOF --no-init | ||
| 84 | ``` | ||
| 85 | |||
