aboutsummaryrefslogtreecommitdiff
path: root/content/notes/show-xterm-colors.md
blob: 88e98567a1069eeff83a4ac9d865b83d6b1a2df7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: Display xterm color palette
url: write-iso-usb.html
date: 2023-05-25T12:00:00+02:00
type: notes
draft: false
tags: [linux]
---

- `bash xterm-palette.sh` - will show you number of max colors available
- `bash xterm-palette.sh -v` - will create a list of all colors with codes

![xterm color palette](/notes/xterm-palette.png)

```sh
#!/usr/bin/env bash
# xterm-palette.sh

trap 'tput sgr0' exit		# Clean up even if user hits ^C

function setfg () {
    printf '\e[38;5;%dm' $1
}

function setbg () {
    printf '\e[48;5;%dm' $1
}

function showcolors() {
    # Given an integer, display that many colors
    for ((i=0; i<$1; i++))
    do
	printf '%4d ' $i
	setbg $i
	tput el
	tput sgr0
	echo
    done
    tput sgr0 el
}

# First, test if terminal supports OSC 4 at all.
printf '\e]4;%d;?\a' 0
read -d $'\a' -s -t 0.1 </dev/tty
if [ -z "$REPLY" ]
then
    # OSC 4 not supported, so we'll fall back to terminfo
    max=$(tput colors)
else
    # OSC 4 is supported, so use it for a binary search
    min=0
    max=256
    while [[ $((min+1)) -lt $max ]]
    do
	i=$(( (min+max)/2 ))
	printf '\e]4;%d;?\a' $i
	read -d $'\a' -s -t 0.1 </dev/tty
	if [ -z "$REPLY" ]
	then
            max=$i
	else
	    min=$i
	fi
    done
fi


# If -v is given, show all the colors
case ${1-none} in
    none)
	echo $max
	;;
    -v)
	showcolors $max
	;;
    *)
	if [[ "$1" -gt 0 ]]; then
	    showcolors $1
	else
	    echo $max
	fi
	;;
esac | less --raw-control-chars --QUIT-AT-EOF --no-init
```