Skip to content

Instantly share code, notes, and snippets.

@kevincox
Created May 2, 2013 00:08
Show Gist options
  • Save kevincox/5499318 to your computer and use it in GitHub Desktop.
Save kevincox/5499318 to your computer and use it in GitHub Desktop.
Bash status message function.
#! /bin/bash
# Copyright 2009-2013 Kevin Cox
################################################################################
# #
# This software is provided 'as-is', without any express or implied #
# warranty. In no event will the authors be held liable for any damages #
# arising from the use of this software. #
# #
# Permission is granted to anyone to use this software for any purpose, #
# including commercial applications, and to alter it and redistribute it #
# freely, subject to the following restrictions: #
# #
# 1. The origin of this software must not be misrepresented; you must not #
# claim that you wrote the original software. If you use this software in #
# a product, an acknowledgment in the product documentation would be #
# appreciated but is not required. #
# #
# 2. Altered source versions must be plainly marked as such, and must not be #
# misrepresented as being the original software. #
# #
# 3. This notice may not be removed or altered from any source distribution. # #
# #
################################################################################
# This is a super simple script I made a long time ago to print a status message
# to the last line of a terminal. The idea was to have "Starting X..." then
# add "[Success]" or "[FAIL]" to the end of the line.
getCursorPos()
{
old_settings=$(stty -g)
stty -icanon min 1 time 2 -echo
printf '\e[6n'
IFS='[;R'
set -f
set -- $(dd count=1 2> /dev/null)
stty "$old_settings"
LINE=$2
COLUMN=$3
}
status ()
{
case ${2,} in
'black') esc='\e[30';;
'red') esc='\e[31';;
'green') esc='\e[32';;
'lime') esc='\e[32;1';;
'yellow') esc='\e[33';;
'blue') esc='\e[34';;
'purple') esc='\e[35';;
'cyan') esc='\e[36';;
'white') esc='\e[37';;
*) esc='\e[0';;
esac
case ${3,} in
'black') esc="${esc};40m";;
'red') esc="${esc};41m";;
'green') esc="${esc};42m";;
'yellow') esc="${esc};43m";;
'blue') esc="${esc};44m";;
'purple') esc="${esc};45m";;
'cyan') esc="${esc};46m";;
'white') esc="${esc};47m";;
*) esc="${esc}m";;
esac
getCursorPos;
COLS=$(tput cols)
C=$(( $COLS - ${#1} -1 ))
L=$(( $LINE - 1 ))
echo -en "\e[${L};${C}f"
echo -e "[$esc$1\e[0m]"
}
if (( "$#" > 0 ))
then status "$1" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment