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