diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
| commit | 23a56bd50b04211da3cab45f72c3390711b2416b (patch) | |
| tree | ab9a4a0136b4cce06dba7d853e296f682f807dbb /content/show-xterm-colors.md | |
| parent | cecb4b48a39a3558979b9c4b50e45bf605a3684e (diff) | |
| download | mitjafelicijan.com-23a56bd50b04211da3cab45f72c3390711b2416b.tar.gz | |
Moved notes and posts into subfolders
Diffstat (limited to 'content/show-xterm-colors.md')
| -rw-r--r-- | content/show-xterm-colors.md | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/content/show-xterm-colors.md b/content/show-xterm-colors.md deleted file mode 100644 index cc5eca4..0000000 --- a/content/show-xterm-colors.md +++ /dev/null | |||
| @@ -1,85 +0,0 @@ | |||
| 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 | |||
