aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-05-25 14:40:15 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-05-25 14:40:15 +0200
commitfe2e0ab47508f37f3a93bfe64c366ff5b4e29255 (patch)
tree87d43bbd08d936a053733cb96552d4112db490ea
parentda9815e767a6ad2840dd8e3e7b2b1df5d494088d (diff)
downloadmitjafelicijan.com-fe2e0ab47508f37f3a93bfe64c366ff5b4e29255.tar.gz
Note: xterm color palette
-rw-r--r--content/notes/show-xterm-colors.md84
-rw-r--r--static/notes/xterm-palette.pngbin0 -> 40140 bytes
2 files changed, 84 insertions, 0 deletions
diff --git a/content/notes/show-xterm-colors.md b/content/notes/show-xterm-colors.md
new file mode 100644
index 0000000..fc7280b
--- /dev/null
+++ b/content/notes/show-xterm-colors.md
@@ -0,0 +1,84 @@
1---
2title: Display xterm color palette
3url: write-iso-usb.html
4date: 2023-05-25
5type: notes
6draft: false
7tags: [linux, xterm, palette]
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```
diff --git a/static/notes/xterm-palette.png b/static/notes/xterm-palette.png
new file mode 100644
index 0000000..c9cd443
--- /dev/null
+++ b/static/notes/xterm-palette.png
Binary files differ