#!/bin/ksh93 # (C) 2012 by Jens Elkner usage() { BN=${ basename $0; } print -u2 " Usage: $BN [-h] [-b] [-x] [-l] Print out ANSI-Terminal color samples. The behavior is undetermined if the terminal in use does not support the requested mode or number of colors. The first column shows always the value of the foreground color used in its row and uses the terminals default background color. All other columns show the value of their background color. -b .. print 16 color samples, which should be supported by all color terms. -x .. print out 256 color samples. Color values are printed as hex values. -l .. print samples wrt. to LS_COLORS env var in a human readable way. -h .. print this help and exit. NOTE: If you pipe it into a pager, don't forget to specify the option to honor ANSI-Escape-Sequences. E.g. for 'less' this is '-R'. Some shells use ls builtins and some ls in your PATH. So depending on how buggy they are, you may see different results as shown for -p. " } MODE=( [0]="default" [1]="bold" [2]="faint" [3]="italic" [4]="underline" [5]="blink slow" [6]="blink fast" [7]="inverse" # [8]="hide" [9]="strike through" # [10]="primary font" # [11-19]="n-th alternate font" # [21]="double" ) printBasic() { for MOD in ${!MODE[*]} ; do printf "ESC[%s;Foreground;Background - %s\n" $MOD "${MODE[$MOD]}" for FG in {30..37} {90..96}; do printf '\033[%s;%sm %3s ' $MOD $FG $FG for BG in {40..47} {100..106}; do printf '\033[%s;%s;%sm %3s ' $MOD $FG $BG $BG done print '\033[0m' done print '\033[0m' done } print256() { COLS=${ tput cols; } # one col == 4 chars and we want N blocks of 8 cols per line integer BLOCKS=$(((COLS-4)/32)) BLOCKS=$((8*BLOCKS)) print "256 color term: ESC[0;38;5;FG;48;5;BG - with FG and BG = {0...255}" for BASE in {0..255..$BLOCKS}; do STOP=$((BASE+BLOCKS-1)) (( $STOP > 255 )) && STOP=255 for FG in {0..255}; do printf '\033[0;38;5;%sm %02X ' $FG $FG for BG in {${BASE}..${STOP}}; do printf '\033[0;38;5;%s;48;5;%sm %02X ' $FG $BG $BG done print '\033[0m' done done } printLsColors() { LTYPE=( [no]="normal" # unsupported in Solaris /usr/bin/ls [rs]="reset to 'normal'" [fi]="file" [di]="directory" [pi]="pipe aka FIFO" [ln]="symlink" [or]="orphan symlink" [bd]="block device" [cd]="character device" [su]="setuid file" [sg]="setgid file" [tw]="sticky and other-writable" [ow]="other-writable" [st]="sticky" [ex]="executable" [so]="socket" [do]="door" # unsupported in tcsh [po]="port" ) C_ARC="0;31" C_IMG="0;48;5;195" C_AUDIO="0;38;5;21;48;5;195" C_VIDEO="0;38;5;9;48;5;195" C_MS="0;9;36" E_ARC=" 7z Z ace arj bz bz2 cab cpio deb dz gz jar lz lzh lzma rar rpm rz tar taz tbz tbz2 tgz tlz txz tz xz z zip zoo " E_IMG=" bmp btif cgm cmx djv djvu dwg dxf emf fbs fh fh4 fh5 fh7 fhc fpx fst g3 gif ico ief jpe jpeg jpg ktx mdi mmr npx odi oti pbm pct pcx pgm pic png pnm ppm psd ras rgb rlc sub svg svgz tga tif tiff uvg uvi uvvg uvvi wbmp webp xbm xif xpm xcf xwd " E_AUDIO=" aac adp aep aif aifc aiff au dra dts dtshd ecelp4800 ecelp7470 ecelp9600 eol flac kar lvp m2a m3a m3u mid midi mka mp2 mp2a mp3 mp4a mpc mpga oga ogg pya ra ram rip rmi rm rmp saf snd spx uva uvva wav wax weba wma " E_VIDEO=" 3g2 3gp anx asf asx avi axv f4v fli flv fvt gl h261 h263 h264 jpgm jpgv jpm m1v m2v m4u m4v mj2 mjp2 mkv mov movie mp4 mp4v mpe mpeg mpg mpg4 mng mxu nuv ogv ogx pyv qt rmvb uvh uvm uvp uvs uvu uvv uvvh uvvm uvvp uvvs uvvu uvvv viv vob webm wm wmv wmx wvx yuv " E_MS=" doc dot docx dotx docm dotm xls xlt xla xlsx xltx xlsm xltm xlam xlsb ppt pot pps ppa pptx potx ppsx ppam pptm potm ppsm " DEFAULT_COLORS=' rs=0 ex=0;38;5;22 di=0;38;5;19 ln=0;38;5;93 pi=0;38;5;208 so=0;92 do=0;38;5;12 bd=0;38;5;196 cd=0;38;5;38 or=07;38;5;93 su=0;93;41 sg=0;92;41 ow=07;31 tw=0;38;5;231;48;5;162 st=0;93;48;5;162 ' if [[ -z "$LS_COLORS" ]]; then print -u2 "LS_COLORS env var is not set. Using this script's defaults:\n" COLORS="${DEFAULT_COLORS//+([[:space:]])/:}" for E in ${E_ARC}; do COLORS+=":*.$E=${C_ARC}" done for E in ${E_IMG}; do COLORS+=":*.$E=${C_IMG}" done for E in ${E_AUDIO}; do COLORS+=":*.$E=${C_AUDIO}" done for E in ${E_VIDEO}; do COLORS+=":*.$E=${C_VIDEO}" done for E in ${E_MS}; do COLORS+=":*.$E=${C_MS}" done else COLORS=${LS_COLORS//+([[:space:]])/:} fi COLORS="${COLORS//:/ }" for C in $COLORS; do T="${C%%=*}" A=${.sh.match[0]:1} C=${LTYPE["$T"]} if [[ -z "$C" ]]; then C="unsupported" [[ "${T:0:2}" == "*." ]] && C="${T:2} file" fi printf '%12s=%-20s\t\033[%sm# %s\033[0m\n' "$T" "$A" "$A" "$C" done } BASE="" X256="" LLS="" while getopts "h(help)x(256)b(base)l(ls)" option ; do case "$option" in h) usage ; exit 0 ;; x) X256="true" ;; b) BASE="true" ;; l) LLS="true" ;; esac done [[ -z "$BASE" && -z "$X256" && -z "$LLS" ]] && usage && exit 1 [[ -n "$BASE" ]] && printBasic [[ -n "$X256" ]] && print256 [[ -n "$LLS" ]] && printLsColors