Skip to content

Instantly share code, notes, and snippets.

@hypersoft
Last active August 25, 2017 18:58
Show Gist options
  • Save hypersoft/4641248 to your computer and use it in GitHub Desktop.
Save hypersoft/4641248 to your computer and use it in GitHub Desktop.
Extended printf (printfx)
#
# This file is a part of Hypersoft bash-masters
#
# Copyright (C) 2013, Triston J. Taylor (pc.wiz.tt@gmail.com)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# triston@1359202440
#
printfx ()
{
TEMP=`getopt -o D:d:hulCks:ebw:c:i:f:p: --long delimiter:dupchar:,auto-clear,underline,help,keep,bold,format:,script:,color:,print: -n "$FUNCNAME" -- "$@"`
if [ $? != 0 ] ; then return 1 ; fi
eval set -- "$TEMP";
declare format='%s\n' escape='-E' line='' script delimiter;
declare clear='eval tput sgr0; escape="-E" line= script= format="%s\\n" delimiter=';
declare clearcode="$clear";
declare clean='if [[ "${clear:0:4}" != true ]]; then $clear; fi';
declare prints='if [[ "$script" ]]; then tput -S <<<"$script"; fi'
declare auto='if [[ -z "$delimiter" ]]; then printf "$format" "$2"; else IFS="$delimiter"; eval printf \"\$format\" \$2; IFS=$'\'' \t\n'\''; fi'
while [[ ${1:0:1} == - ]]; do
[[ $1 =~ ^-h|--help ]] && {
echo ''
echo 'USAGE: $FUNCNAME [OPTIONS] [TEXT]'
echo ''
echo 'OPTIONS'
echo ''
echo ' -s Sets a (tput) terminal attribute'
echo ' -c Sets the terminal foreground color'
echo ' -d Duplicates the char expression: cN, where c is the char, N is the count'
echo ' -D Sets IFS to expand the print argument, set to IFS to expand normally.'
echo ' -b Sets the terminal bold attribute'
echo ' -u Sets terminal underline attribute'
echo ' -f Sets the print format to argument'
echo ' -p Immediately prints argument according to format on standard out'
echo ' -w Immediately writes argument to standard out'
echo ' -e Enables write escape parsing (per call)'
echo ' -l Enables write line (per call)'
echo ' -k Always keep the terminal profile'
echo ' -i Optimally write multiple lines of tput script from a shell variable label'
echo ''
echo ' -C Reverses the effect of -k'
echo ''
echo ' -h Show this help screen'
echo ''
echo ' --script Same as -s'
echo ' --color Same as -c'
echo ' --dupchar Same as -d'
echo ' --bold Same as -b'
echo ' --underline Same as -u'
echo ' --format Same as -f'
echo ' --write Same as -w'
echo ' --print Same as -p'
echo ' --keep Same as -k'
echo ' --import Same as -i'
echo ''
echo ' --delimiter Same as -D'
echo ' --auto-clear Same as -C'
echo ''
echo ' --help Same as -h'
return;
};
[[ $1 == -- ]] && { shift; break; };
[[ $1 == -l ]] && { line='-n'; shift 1; continue; };
[[ $1 == -w ]] && { eval $prints; echo $escape $line "${2}"; eval $clean; shift 2; continue; };
[[ $1 == -i ]] && { script+="${!2}"$'\n'; shift 2; continue; };
[[ $1 == -e ]] && { escape=$1; shift 1; continue; };
[[ $1 =~ ^-s|--script$ ]] && { script+="$2"$'\n'; shift 2; continue; };
[[ $1 =~ ^-D|--delimiter$ ]] && { delimiter="$2"; shift 2; continue; };
[[ $1 =~ ^-d|--dupchar$ ]] && {
declare v="$(printf "%0${2:1}s" '')"; eval $prints; printf %s "${v// /${2:0:1}}";
eval $clean; shift 2; continue;
};
[[ $1 =~ ^-c|--color$ ]] && { script+="setf $2"$'\n'; shift 2; continue; };
[[ $1 =~ ^-b|--bold$ ]] && { script+="bold"$'\n'; shift 1; continue; };
[[ $1 =~ ^-u|--underline$ ]] && { script+="smul"$'\n'; shift 1; continue; };
[[ $1 =~ ^-f|--format$ ]] && { format="${2}"; shift 2; continue; };
[[ $1 =~ ^-p|--print$ ]] && { eval $prints; eval $auto; eval $clean; shift 2; continue; };
[[ $1 =~ ^-k|--keep$ ]] && { clear='true'; shift 1; continue; };
[[ $1 =~ ^-C|--auto-clear$ ]] && { clear="$clearcode"; shift 1; continue; };
break;
done
eval $prints;
(( $# )) && {
[[ -n "$format" ]] && printf "$format" "$@" || printf "$@"
}
eval $clear;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment