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