Skip to content

Instantly share code, notes, and snippets.

@parke
Last active April 24, 2022 07:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parke/dd5fac890eface4a51f02284ecb48626 to your computer and use it in GitHub Desktop.
Save parke/dd5fac890eface4a51f02284ecb48626 to your computer and use it in GitHub Desktop.
Xterm color chart generator
local reset = '\027(B\027[m'
function cellx ( prefix_core, text ) ----------------------------- cellx
io .write ( reset )
io .write ( ('\27[%sm') : format ( prefix_core ) )
io .write ( text )
io .write ( reset ) end
function color_chart_16 () -------------------------------- color_chart_16
local function color ( n )
if n == 30 then return ('100;%s') : format ( n ) end
if n == 40 then return ('90;%s') : format ( n ) end
if n == 44 then return ('94;%s') : format ( n ) end
return ('30;%s') : format ( n ) end
local function row ( a, b )
io .write ' '
for n=b,b+7 do cellx ( color(n), (' %03d ') : format ( n ) ) end
io .write ( (' %s \n') : format ( a ) ) end
print ''
print ' \\e[Nm N=30-37,40-47,90-97,100-107'
row ( 'tput setaf 00-07', 30 )
row ( 'tput setaf 08-15', 90 )
row ( 'tput setab 00-07', 40 )
row ( 'tput setab 08-15', 100 ) end
function color_chart_256 () ---------------------------- color_chart_256
local function color ( n )
local s = '38;5;%d;48;5;%d'
if n <= 0 then return s:format(238,n) end
if n == 4 then return s:format(12,n) end
if n <= 15 then return s:format(0,n) end
if n <= 21 then return s:format(238,n) end
if n <= 231 then return s:format(0,n) end
if n <= 244 then return s:format(n+8,n) end
if n <= 255 then return s:format(n-8,n) end end
local function row ( a, b, c, d, e, f, g, h, i )
local function row ( j, a, b, c )
io .write ( (' '):rep(j) )
for n = a, a+(b*((c or 1)))-1, (c or 1) do
cellx ( color(n), (' %03d ') : format ( n ) )
end end
row ( a, b, c, d )
if e then row ( e, f, g, h ) end
io .write ( i or '\n' ) end
print ''
print ' \\e[38;5;Nm N=0-255 tput setaf N ( set foreground )'
print ' \\e[48;5;Nm N=0-255 tput setab N ( set background )'
row ( 4, 0, 8 )
row ( 4, 8, 8 )
row ( 4, 232, 13 )
row ( 4, 15, 1, 1, 0, 7, 1, 1, '' )
row ( 0, 255, 9, -1 )
for n=16,33 do row ( 4, n, 6, 36, 5, n+18, 6, 36 ) end end
function ps1 () ---------------------------------------------------- ps1
print ''
print ' in bash set PS1 as follows:'
print ' wrap non-printing characters in \\[ and \\]'
print ' set terminal title with \\e]0;your title here\\007'
print ' see https://gist.github.com/lenormf/5c638b8c58b7460140cfa281d452f17e'
print ' see https://serverfault.com/q/23978'
print ' see https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters'
end
function style_chart () ------------------------------------ style_chart
local names, seqs = {}, {}
local proc = io .popen 'infocmp -1'
for s in proc : lines() do
local name, seq = s : match '^\t*(.-)=(.-),'
if name then
-- print ( name, seq )
names [ name ] = seq
seqs [ seq ] = name end end
assert ( proc : close() )
-- print ()
for n=0,74 do
local seq = ('\\E[%dm') : format ( n )
local name = seqs [ seq ]
-- print ( n, seq, name )
end
local function stylex ( seq, name, comment )
local s = ' %-11s %-7s %s%s%s'
name = name or seqs [ seq ] or '-'
comment = comment or name
local raw = seq : gsub ( '\\E', '\027' )
print ( s : format ( seq, name, raw, comment, reset ) )
end
print ''
print ' style chart'
print ' code tput comment'
-- stylex 'sgr0'
-- stylex ( nil, '\\E[0m', '"reset / normal"' )
stylex '\\E(B\\E[m'
stylex ( '\\E(B\\E[0m', 'srg 0' )
stylex ( '\\E(0', nil, 'altcharset' )
stylex ( '\\E[0m', nil, '"all attributes off"' )
stylex '\\E[1m'
stylex '\\E[2m'
stylex '\\E[3m'
stylex ( '\\E[4m', nil, '"underline"' )
stylex '\\E[5m'
stylex ( '\\E[6m', nil, '"rapid blink"' )
stylex ( '\\E[7m', 'rev', 'reverse' )
stylex ( '\\E[7m', 'smso', 'standout' )
stylex '\\E[8m'
stylex '\\E[9m'
--[[
stylex '\\E[10m'
stylex '\\E[11m'
stylex '\\E[12m'
stylex '\\E[13m'
stylex '\\E[14m'
stylex '\\E[15m'
stylex '\\E[16m'
stylex '\\E[17m'
stylex '\\E[18m'
stylex '\\E[19m'
stylex '\\E[20m'
stylex '\\E[21m'
stylex '\\E[22m'
stylex '\\E[23m'
stylex '\\E[24m'
stylex '\\E[25m'
stylex '\\E[26m'
stylex '\\E[27m'
stylex '\\E[28m'
stylex '\\E[29m'
--]]
stylex ( '\\E[39m', nil, '"default foreground color"' )
stylex ( '\\E[49m', nil, '"default background color"' )
end
function main () -------------------------------------------------- main
ps1()
style_chart()
color_chart_16()
color_chart_256()
end
main()
#! /bin/sh
# This script prints a chart of 256 xterm colors.
# The script also prints tips for writing and editing escape sequences.
# Parts of the script are hardcoded to generate xterm escape sequences.
# Many common terminals may be compatible with these xterm sequences.
# It should be straightfoward to adapt the script to work on any terminal.
# The script runs in dash, bash, and possibly other shells.
#
# Updated 20170119.
# hint: infocmp -1 | less
# hint: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Colors
infocmp_parse () { # ---------------------------------------- infocmp_parse
# 20170118 infocmp_parse is no longer used.
for line in $( infocmp -1 ); do
capability=${line%%=*}
code=${line#*=}
code=${code%,}
case "$capability" in
('sgr0' ) sgr0="$code" ;;
('smso' ) smso="$code" ;;
('smul' ) smul="$code" ;;
('rev' ) rev="$code" ;;
('blink') blink="$code" ;;
('dim' ) dim="$code" ;;
('bold' ) bold="$code" ;;
('invis') invis="$code" ;;
('prot' ) prot="$code" ;;
('smacs') smacs="$code" ;;
esac
done; }
style_line () { name="$1"; capability="$2" # ----------------- style_line
code=$( tput "$capability" "$3" )
code_=$( echo "$code" | sed "s/$esc/\\\\E/g"; )
printf ' %-14s %-12s %s\n' "$name" "$code_" "$code$name$sgr0"; }
style_guide () ( # -------------------------------------------- style_guide
esc=$( printf '\033' )
sgr0=$( tput sgr0 )
printf ' style codes\n'
style_line 'none (sgr0)' sgr0
style_line 'none (sgr 0)' sgr 0
style_line standout smso
style_line underline smul
style_line reverse rev
style_line blink blink
style_line dim dim
style_line bold bold
style_line invisible invis
style_line protect prot
style_line altcharset smacs )
color_guide () ( capname="$1"; description="$2"; # ---------- color_guide
esc=$( printf '\033' )
f () { tput "$capname" "$1" | sed "s/$esc/\\\\E/g"; }
printf ' %s\n' "$description"
printf ' 0-7 0 %-16s 7 %s\n' "$( f 0 )" "$( f 7 )"
printf ' 8-15 8 %-16s 15 %s\n' "$( f 8 )" "$( f 15 )"
printf ' 16-255 16 %-16s 255 %s\n' "$( f 16 )" "$( f 255 )" )
usage_guide () ( # -------------------------------------------- usage_guide
esc=$( printf '\033' )
sgr0=$( tput sgr0 )
f () { description="$1" code="$2"
code_=$( echo "$code" | sed "s/$esc/\\\\E/g" )
printf ' %-25s %s\n' "$code_" "$code$description$sgr0"; }
printf ' example codes created dynamically via the tput command\n'
f 'foreground color 196' "$( tput setaf 196 )"
f 'foreground color 226, bold' "$( tput setaf 226 && tput bold )"
f 'background color 20' "$( tput setab 20 )"
echo
printf ' stitac, manually created example codes that work on xterm\n'
f 'foreground color 226, none' "$( printf '\33[0;38;5;226m' )"
f 'foreground color 226, bold' "$( printf '\33[1;38;5;226m' )"
f 'foreground and background combined' \
"$( printf '\33[0;38;5;226;48;5;20m' )"
f 'foreground and background combined' \
"$( printf '\33[1;38;5;226;48;5;20m' )"
printf ' Note: In xterm, the 16-255 color codes '
printf 'work for all colors 0-255.\n' )
range () { a="$1"; b="$2"; step="$3" # --------------------------- range
while [ "$a" -le "$b" ]; do
echo "$a"; a="$(($a+$step))"; done; }
color_cell () { # ---------------------------------------------- color_cell
if [ "$1" -ge 232 ] && [ "$1" -le 243 ]; then
printf "\33[38;5;$(($1+8))m"
elif [ "$1" -ge 244 ] && [ "$1" -le 255 ]; then
printf "\33[38;5;$(($1-8))m"
else
case "$1" in
(0|4|16|17|18|19|20|21) printf "\33[38;5;238m" ;;
(*) printf "\33[38;5;0m" ;;
esac; fi
printf '\33[48;5;%dm %03d ' "$c" "$c"; }
color_row () { # ------------------------------------------------ color_row
# usage: color_row prefix a b step suffix
printf "$1"
for c in $( range "$2" "$3" "$4" ); do color_cell "$c"; done
printf "$sgr0$5"; }
color_chart () ( # -------------------------------------------- color_chart
printf ' color chart\n'
sgr0=$( tput sgr0 )
color_row ' ' 0 7 1 '\n'
color_row ' ' 8 15 1 '\n'
color_row ' ' 232 243 1 '\n'
color_row ' ' 244 255 1 '\n'
for n in $( range 0 17 1 ); do
color_row ' ' $((16+n)) $((196+n)) 36
color_row ' ' $((34+n)) $((214+n)) 36 '\n'; done )
main () ( # ---------------------------------------------------------- main
echo; style_guide
echo; color_guide setaf 'foreground color codes'
echo; color_guide setab 'background color codes'
echo; usage_guide
echo; color_chart
)
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment