Files
q-tools/reporting/qolop
2017-08-20 10:48:21 +03:00

202 lines
6.0 KiB
Bash
Executable File

#!/bin/bash
_qColVersion() { echo Version 2017.08.20.0; }
_qColHelp() {
_qColVersion
echo 'Quick shell COLOrizer Package
Source me in BASH!
Functions:
- _qCol: Print ANSI color codes by letter codes.
Can export all letter codes as varibles.
- _qCode: Print ANSI color codes by number sequence
- _qPos: Move cursor
- _qClr: Clear screen
- _qReset: Reset terminal
Examples:
source qolop # load functions
- As functions:
_title(){ _qClr; _qCol z G; echo "$@"; _qCol z; }
_text(){ _qCol z; _qCode 1 34; echo "$@"; _qCol z; }
_title Title; _text Colored;
- As variables
TITLE=$( _qCol z G; )
Z=$( _qCol z; )
echo ${TITLE}Title${Z}
- Export to variables (prefixed with _c. Also the default)
_qCol export _c
echo -e ${_cG}Title${_cZ}
Run qolop as script to print ANSI code table
'
}
_qCol() {
# print "easier" mapping of ANSI colors and controls
local K="\033[1;30m"
local R="\033[1;31m"
local G="\033[1;32m"
local B="\033[1;34m"
local Y="\033[1;33m"
local M="\033[1;35m"
local C="\033[1;36m"
local W="\033[1;37m"
local k="\033[0;30m"
local r="\033[0;31m"
local g="\033[0;32m"
local b="\033[0;34m"
local y="\033[0;33m"
local m="\033[0;35m"
local c="\033[0;36m"
local w="\033[0;37m"
local bk="\033[40m"
local br="\033[41m"
local bg="\033[42m"
local by="\033[43m"
local bb="\033[44m"
local bm="\033[45m"
local bc="\033[46m"
local bw="\033[47m"
local S='\033[1m' #strong
local s='\033[2m' #strong off
local U='\033[4m' #underline
local u='\033[24m' #underline off
local z='\033[0m' #zero colors
local Z='\033[0m' #zero colors
local ic='\033[7m' #inverse colors
local io='\033[27m' #inverse off
local st='\033[9m' #strike on
local so='\033[29m' #strike off
local CLR='\033[2J' # Clear screen
local CLREND='\033[K' # Clear to end of line
local CLRBEG='\033[1K' # Clear to beginning of line
local CLRSCR="$CLR"'\033[0;0H' # Clear screen, reset cursor
local color_keys=" K R G B Y M C W k r g b y m c w S s U u z Z ic io st so bk br bg by bb bm bc bw CLR CLREND CLRBEG CLRSCR "
[[ "$1" = "export" ]] && {
local key
local prefix="$2"
[[ -z "$2" ]] && prefix=_c
for key in $color_keys; do
eval export ${prefix}${key}=\'${!key}\'
done
return
}
local arg val
for ((arg=1;arg<=$#;arg++)) {
val=${!arg}
[[ ${color_keys} = *" $val "* ]] || { echo "No such color code '${val}'" >&2; return 1; }
printf ${!val}
}
}
_qCode() {
# Enter numerical ANSI colors directly
local arg val
for ((arg=1;arg<=$#;arg++)) {
val=${!arg}
printf '\033['"${val}m"
}
}
_qPos() {
# Cursor position control
local n="$2"
local x="$3"
[[ -z "$n" ]] && n=1
[[ -z "$x" ]] && x=1
[[ "$1" = "save" ]] && { printf '\033[s'; return; }
[[ "$1" = "restore" ]] && { printf '\033[u'; return; }
[[ "$1" = "up" ]] && { printf '\033['${n}'A'; return; }
[[ "$1" = "down" ]] && { printf '\033['${n}'B'; return; }
[[ "$1" = "right" ]] && { printf '\033['${n}'C'; return; }
[[ "$1" = "left" ]] && { printf '\033['${n}'D'; return; }
[[ "$1" = "lineup" ]] && { printf '\033['${n}'E'; return; }
[[ "$1" = "linedown" ]] && { printf '\033['${n}'F'; return; }
[[ "$1" = "col" ]] && { printf '\033['${n}'G'; return; }
[[ "$1" = "abs" ]] && { printf '\033['${n}';'${x}'H'; return; }
}
_qClr() {
# Clear screen
_qCol "CLR"
_qPos abs 0 0
}
_qReset() {
# Reset terminal
printf '\033c'
}
[[ "$0" = "${BASH_SOURCE[0]}" ]] && {
[[ "$1" = "update" ]] && {
set -e
case $OSTYPE in
darwin*)
MYPATH=$( realpath "$0" )
;;
*)
MYPATH=$( readlink -f "$0" )
;;
esac
MYDIR=$( dirname "$MYPATH" )
QTOOLS=0
if [[ -f "$MYDIR/../rc" ]]; then if grep -q QTOOLS "$MYDIR/../rc"; then
QTOOLS=1
fi;fi;
if [[ "$QTOOLS" -eq 1 ]]; then
echo "Update qolop with _q-tools-update"
exit 1
fi
TMPFILE=$( mktemp )
rm -f "$TMPFILE"
wget -O "$TMPFILE" https://bitbucket.org/MoonQ/tools/raw/tip/reporting/qolop
mv -v "$TMPFILE" "$MYPATH"
chmod +x "$MYPATH"
echo Updated
source "$MYPATH"
_qColVersion
exit 0
} # end update
_qColHelp
[[ "$1" = *"help" ]] && exit
[[ "$1" = "-h" ]] && exit
_qCol export "_"
printf "${_S}ANSI CODES AND QOLOP VARIABLES
==============================${_Z}
${_R}Co${_G}lo${_B}rs${_W} and ${_S}Mo${_U}di${_st}fi${_u}er${_ic}s${_Z} ESC[Xm or ESC[X;Y;Zm
${_S}====================${_Z}
0 z Z Clear format
1 S ${_S}Strong/Bold ${_Z} 30 k ${_k}Black ${_Z}1 K ${_K}Black ${_Z}40 bk ${_bk}Black${_Z}
2 s ${_s}Weak/Bold off ${_Z} 31 r ${_r}Red ${_Z}1 R ${_R}Red ${_Z}41 br ${_br}Red${_Z}
4 U ${_U}Underline ${_Z} 32 g ${_g}Green ${_Z}1 G ${_G}Green ${_Z}42 bg ${_bg}Green${_Z}
24 u ${_u}Underline off ${_Z} 33 y ${_y}Yellow ${_Z}1 Y ${_Y}Yellow ${_Z}43 by ${_by}Yellow${_Z}
7 ic ${_ic}Inverse color ${_Z} 34 b ${_b}Blue ${_Z}1 B ${_B}Blue ${_Z}44 bb ${_bb}Blue${_Z}
27 of ${_io}Inverse off ${_Z} 35 m ${_m}Magenta ${_Z}1 M ${_M}Magenta ${_Z}45 bm ${_bm}Magenta${_Z}
9 st ${_st}Strike ${_Z} 36 c ${_c}Cyan ${_Z}1 C ${_C}Cyan ${_Z}46 bc ${_bc}Cyan${_Z}
29 so ${_so}Strike off ${_Z} 37 w ${_w}White ${_Z}1 W ${_W}White ${_Z}47 bw ${_bw}White${_Z}
Enter raw color code sequence with _qCode function
${_S}Clearing and movement${_Z} ESC[X or ESC[1X
${_S}=====================${_Z}
2J CLR Clear screen
2J ;H CLRSCR _qClr() Clear screen and cursor to upper left
K CLREND Clear to end of line
1K CLRBEG Clear to beginning of line
s _qPos save Save location u _qPos restore Restore location
A _qPos up Up E _qPos lineup Up line
B _qPos down Down F _qPos linedown Down line
C _qPos left Left y;xH _qPos abs y x Absolute Position
D _qPos right Right
"
}