aboutsummaryrefslogtreecommitdiff
path: root/content/notes/show-xterm-colors.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/notes/show-xterm-colors.md')
-rw-r--r--content/notes/show-xterm-colors.md85
1 files changed, 0 insertions, 85 deletions
diff --git a/content/notes/show-xterm-colors.md b/content/notes/show-xterm-colors.md
deleted file mode 100644
index 88e9856..0000000
--- a/content/notes/show-xterm-colors.md
+++ /dev/null
@@ -1,85 +0,0 @@
1---
2title: Display xterm color palette
3url: write-iso-usb.html
4date: 2023-05-25T12:00:00+02:00
5type: notes
6draft: false
7tags: [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![xterm color palette](/notes/xterm-palette.png)
14
15```sh
16#!/usr/bin/env bash
17# xterm-palette.sh
18
19trap 'tput sgr0' exit # Clean up even if user hits ^C
20
21function setfg () {
22 printf '\e[38;5;%dm' $1
23}
24
25function setbg () {
26 printf '\e[48;5;%dm' $1
27}
28
29function 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.
43printf '\e]4;%d;?\a' 0
44read -d $'\a' -s -t 0.1 </dev/tty
45if [ -z "$REPLY" ]
46then
47 # OSC 4 not supported, so we'll fall back to terminfo
48 max=$(tput colors)
49else
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
65fi
66
67
68# If -v is given, show all the colors
69case ${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 ;;
83esac | less --raw-control-chars --QUIT-AT-EOF --no-init
84```
85