Commit d59f0871 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Added a terminal print functions file

parent 258343e0
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -17,6 +17,12 @@
#


set -e

# LIBDIR is populated by the install script
LIBDIR=`dirname $0`
source $LIBDIR/string_functions

BIN=`basename "$0"`
GIT="git ${GIT_DIR+--git-dir "${GIT_DIR}"}"
GIT_DIR="${GIT_DIR:-`git rev-parse --git-dir`}"
@@ -32,12 +38,6 @@ usage ()
	USAGE
}

die ()
{
	printf -- "$@"
	echo
	exit 1
} >&2

add_arg ()
{
@@ -130,7 +130,7 @@ parse_args ()
		case "$1" in
			--help|-h)
				printf "$BIN $command"
				sumarise_args
				sumarise_args | wrap "    "
				return 1
				;;
			--summary)

string_functions

0 → 100644
+117 −0
Original line number Diff line number Diff line
#! /bin/bash
#
#  This file contains functions for printing well formatted messages to a text 
#  terminal from bash scripts.
#
#  Copyright (C) 2013  Dom Sekotill
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


##
# column()
# Find the column the terminal cursor is currently in.
#
# Usage:
# column > column number
##
column ()
{
	local oldstty pos
	exec < /dev/tty
	oldstty=`stty -g`
	stty raw -echo min 0
	echo -en '\033[6n' > /dev/tty
	read -sdR pos
	stty $oldstty
	printf "${pos#*;}"
}


##
# wrap()
# Intelligently word wrap standard input, with an optional prefix on each line.
#
# If printing does not start at the left-most column the first prefix will be 
# omitted.
#
# Usage:
# wrap [prefix] < pre-wrapped text > post-wrapped text
##
wrap ()
{
	local prefix="$1"
	local width=`tput cols`
	local maxspace=$((width/4))
	local line column word space
	local IFS

	# minimum terminal width is 80 characters, less is just silly
	[ ${width:-0} -lt 80 ] && width=80
	[ ${#prefix} -gt $((width/4)) ] && prefix="${prefix::$((width/4-4))}... "

	while read -a line; do
		column=`column`

		# only print the prefix if starting at the beginning of a line
		if [ $column -le 1 ]; then
			printf "$prefix"
			column=$((column+${#prefix}))
		fi

		for word in ${line[*]}; do
			while [ "$word" ]; do
				space=$((width - $column))
				if [ ${#word} -gt $space ]; then
					if [ $space -gt 4 ] && [ ${#word} -gt $maxspace ]; then
						space=$((space-1))
						printf " ${word::$space}-"
						word="${word:$space}"
					fi
					printf "\n$prefix"
					column=$((${#prefix}+1))
				else
					printf " $word"
					column=$((column+${#word}+1))
					word=
				fi
			done
		done
		echo
	done
}


##
# die()
# Print an error message and exit.
#
# Prints an error message given by the arguments, well wrapped and prefixed 
# with the string "ERROR:". If accepts arguments identical to printf except for 
# '-v <variable>', which will be treated as strings if given.
#
# The value of EXIT_CODE will be used as the exit code if it exists, otherwise 
# the exit code will be 1
#
# Usage:
# [EXIT_CODE={X}]
# die "<format string>" [<option 1> [<option 2> ...]]
##
die ()
{
	printf -- "$@" | wrap ERROR:
	echo
	exit ${EXIT_CODE:-1}
} >&2