At A Glance Coloured Real Time Bargraph Generator...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting At A Glance Coloured Real Time Bargraph Generator...
# 1  
Old 01-12-2013
At A Glance Coloured Real Time Bargraph Generator...

Not sure if anyone is interested but I am just getting into UNIX like shell scripting...

I have great interest in pseudo-animations in text mode and accessing HW like /dev/dsp for example...

...

Have fun, I do... ;o)

Code:
# !/bin/sh
#
# Bargraph_Generator.sh
#
# A DEMO 6 bit coloured bargraph animation for a default Bash and Terminal window on OSX 10.7.5...
# A simple Shell script to display an _AT_A_GLANCE_ real time analogue bargraph generator. It
# starts off with GREEN for OK, then YELLOW for warning and finally ending with RED for danger
# with a critical beep for values 61 to 63 inclusive.
# It assumes an 8 bit value being injected into the script which is then divided by 4 to give
# a 6 bit value which is 64 spaces width inside the Terminal. The DEMO uses a random number
# generator to give a representation of an 8 bit value so you can see it working...
#
# A shell derivative of my Python code:-
# http://code.activestate.com/recipes/577612-seven-bit-colored-analogue-bar-graph-generator-dem/?in=user-4177147
#
# To run, ensure the script is executable and change if required, then type from a Terminal:-
#
# xxxxx$ /full/path/to/Bargrapth_Generator.sh<CR>
#
# And away you go...
#
# Written in such a way that kids and newbies can understand what is going on.
#
# Originally written for a Macbook Pro 13 inch, OSX 10.7.5 using the default Terminal.
# It MIGHT work on some Linux variants but WAS intended for MacOS OSX 10.7.x and above only.
#
# The Terminal colours WILL be changed to Black background and Various foreground colours.
# It will NOT be returned back to its original state although it can be easily. If you
# need to rerturn back to default state then there are a couple of easy methods the
# simplest being type:-
#
# xxxxx$ reset<CR>
#
# And all will be corrected...
#
# Issued entirely as Public Domain and you may do with it as you please
#
# $VER Bargraph_Generator.sh_Version_0.00.10_(C)2012_B.Walker_G0LCU.
#
# Enjoy finding simple solutions to often very difficult problems...

# The required _varibales_ for ease of coding, these are the colours...
# White On Black.
WOB="\x1B[1;37;40m"
# Black On Green.
BOG="\x1B[1;30;42m"
# Black On Yellow.
BOY="\x1B[1;30;43m"
# Black On red.
BOR="\x1B[1;30;41m"
# Green On Black.
GOB="\x1B[1;32;40m"
# Yellow On Black.
YOB="\x1B[1;33;40m"
# Red On Black.
ROB="\x1B[1;31;40m"

# Set the pseudo 6 bit value to zero.
SIX_BIT_DEPTH=0

# Do a clear screen to White On Black.
printf $WOB
clear

while true
do
	# Set up the screen per scan and prepare for the bargraph.
	clear
	printf $WOB"\n \$VER: Bargraph_Generator.sh_Version_0.00.10_(C)2012_B.Walker_G0LCU.\n\n"
	printf " A horizontal, at a glance, coloured, analogue bargraph display for\n"
	printf " a default Terminal inside OSX 10.7.5..\n\n\n\n\n"
	printf "        0         10        20        30        40        50        60"
	printf $GOB"\n        +----+----+----+----+----+----+----+----+----+"$YOB"----+----+"$ROB"----+--\n"
	printf $GOB"       (|                                                              "$ROB")\n"
	printf $GOB"        +----+----+----+----+----+----+----+----+----+"$YOB"----+----+"$ROB"----+--\n\n\n\n"
	# If the 6 bit value is 0, zero, do no more until printing the 6 bit value and generating another 6 bit value...
	# Anything greater than or equal to 1 enters this conditional branch.
	if [ "$SIX_BIT_DEPTH" -ge "1" ]
	then
		# If the 6 bit value is less than or equal to 46 then _plot_ the green section only.
		# The '\x1B[12;8f' is the ANSI 'Esc' code that forces the print position to 12 lines by 8 columns.
		if [ "$SIX_BIT_DEPTH" -le "46" ]
		then
			BARGRAPH=$GOB"\x1B[12;8f("$BOG
			for green in $(seq 1 "$SIX_BIT_DEPTH")
			do
				BARGRAPH=$BARGRAPH" "
			done
		fi
		# If the 6 bit value is greater than or equal to 47 then print the green section and _plot_ the yellow section.
		if [ "$SIX_BIT_DEPTH" -ge "47" ]
		then
			BARGRAPH=$GOB"\x1B[12;8f("$BOG"                                              "$BOY
			for yellow in $(seq 47 "$SIX_BIT_DEPTH")
			do
				BARGRAPH=$BARGRAPH" "
			done
		fi
		# If the 6 bit value is greater than or equal to 57 then print the green and yellow section and _plot_ the red section.
		if [ "$SIX_BIT_DEPTH" -ge "57" ]
		then
			BARGRAPH=$GOB"\x1B[12;8f("$BOG"                                              "$BOY"          "$BOR
			for red in $(seq 57 "$SIX_BIT_DEPTH")
			do
				BARGRAPH=$BARGRAPH" "
			done
		fi
		printf "$BARGRAPH"$GOB"\n\n\n\n\n"
	fi
	# When the 6 bit value is greater than or equal to 61 sound a system error beep.
	if [ "$SIX_BIT_DEPTH" -ge "61" ]
	then
		printf "\a"
	fi
	# Print the 6 bit value in White On Black...
	printf $WOB" Random number generated "$SIX_BIT_DEPTH"...\n\n"
	printf " Press Ctrl-C to stop the program...\n\n"
	# Generate another 6 bit value as though from an 8 bit value...
	SIX_BIT_DEPTH=$[($RANDOM % (256/4))]
	# A practical lower limit for the sleep command is 'sleep 0.05'...
	sleep 1
done

# End of Bargraph_Generator.sh DEMO.
# Enjoy finding simple solutions to often very difficult problems... ;o)

These 3 Users Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting real time to epoch time

# date +%s -d "Mon Feb 11 02:26:04" 1360567564 # perl -e 'print scalar localtime(1360567564), "\n";' Mon Feb 11 02:26:04 2013 the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input 1359453135154 rather than 10 digit epoch time 1360567564... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. UNIX for Dummies Questions & Answers

Real time processing

Hi Not sure if this can be achieved by unix , but still would like to know if there is any way by which I can do the below given logic cat sam1 > out1 cat sam2 > out2 when either one of this finished the the next file shd be written in that file, meaning cat sam3 >> out1/out2... (2 Replies)
Discussion started by: Sri3001
2 Replies

3. HP-UX

Glance and trace over the time

Do you know if Glance is capable to trace a process (or processes) per memory use over the time? I want to have snapshots for example morning and midnigth then compare the info after one weak in order to determine if I have a memory leak in some process I am observing. Is it possible with glace... (1 Reply)
Discussion started by: roche
1 Replies

4. Shell Programming and Scripting

time generator

Hi experts, I'd like to generate the table/file containing: number of milliseconds elapsed since midnight till midnight. It should contain 5 columns (hours minutes seconds milliseconds): Table will have theoretically 86 400 000 rows. My question is , is there somewhere the file or source... (7 Replies)
Discussion started by: hernand
7 Replies

5. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

6. Programming

problem with real-time

hello every1, i'm very hope so anyone here have experience with lib rt like aio linux based. In first I've a problem with receiving data from aio_buf, i.e. I have received it, but if the next data size less then pervious I've got a noise from a socket. I've tried to fix it by different ways, but... (0 Replies)
Discussion started by: quant
0 Replies

7. UNIX for Advanced & Expert Users

EPOCH to real time?

hi all :confused: i am wondering if there is a way to convert from EPOCH time to the standard tim, may be using a script or some thing else??????? thanks............................ (5 Replies)
Discussion started by: TheEngineer
5 Replies
Login or Register to Ask a Question