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