View Full Version : Twirling Cursor
soup4you2
June 2nd, 2003, 14:02
hehe sorry to bug you all w/ my scripting difficulties but hey thats what were here for right..
just a though.. would anybody happen to know a bash script or function to do the twirling cursor like you see when bsd first loads up..
i have some scripts that take time to do it's magic and i thought i would add in some eye candy while i wait.. plus most of the time it takes about 5 to 10 minuites to process my files i'm parsing and the network will kill my ssh session if it's been inactive for more than 3 minuites,, so my main idea is to have some kinda thing displaying while processing..
frisco
June 2nd, 2003, 14:18
In perl there are a few progress bar modules, for example:
http://search.cpan.org/author/FLUFFY/Term-ProgressBar-2.03/lib/Term/ProgressBar.pm
but those are a little different.
Essentially what you need to do is print a bunch of / - \ and | in the correct order and writing over eachother, so something like this should work:
[code:1:80afaad416]
#!/bin/sh
while [ true ]; do
echo -n \\
sleep 1;
echo -n ^H
echo -n \|
sleep 1;
echo -n ^H
echo -n /
sleep 1;
echo -n ^H
echo -n -
sleep 1;
echo -n ^H
echo -n \\
sleep 1;
echo -n ^H
echo -n \|
sleep 1;
echo -n ^H
echo -n /
sleep 1;
echo -n ^H
echo -n -
sleep 1
echo -n ^H
done
[/code:1:80afaad416]
The ^H is a backspace, you can get it by typing ctrl-v ctrl-backspace
And keep on asking away!
EDIT:
I think the above code won't work if the user has a TERMCAP defined differently. Perhaps running 'set tty erase ^H' beforehand would fix that? I'm not sure.
frisco
June 2nd, 2003, 14:26
Another really cool pulsing progress meter is the one from cdparanoia. It changes between the following characters: .o0O0o. all in the same place and so appears to grow and shrink while it's running.
soup4you2
June 2nd, 2003, 15:13
verry nice and thanks for the quick reply... you all are great... if anything else these questions should help us build a nice library of functions...
soup4you2
June 2nd, 2003, 17:47
now i need an example here.. the twirlie thing works great.. but say i this script below... how would i tie in the twirls to be run while waiting on the rm -rf /*
function testing {
rm -rf /*
}
function twirlie {
while [ true ]; do
echo -n \\
sleep 1;
echo -n ^H
echo -n \|
sleep 1;
echo -n ^H
echo -n /
sleep 1;
echo -n ^H
echo -n -
sleep 1;
echo -n ^H
echo -n \\
sleep 1;
echo -n ^H
echo -n \|
sleep 1;
echo -n ^H
echo -n /
sleep 1;
echo -n ^H
echo -n -
sleep 1
echo -n ^H
done
}
testing
frisco
June 2nd, 2003, 19:10
The only thing i can think of is creating a temp file before the rm and deleting it afterwards. Run the rm function in the background and keep the twirlie function running only while the file exists.
[code:1:c97a003472]
TMPFILE=`mktemp`
function testing {
sleep 10
rm $TMPFILE
}
function twirlie {
while [ -f $TMPFILE ]; do
echo -n .... # place all the echo's and sleep's here.
done
}
testing &
twirlie
[/code:1:c97a003472]
Note: since the while loop in twirlie takes 8 seconds to complete, there exists the possibility that the testing function will complete but the twirlie function will still keep going for another 8 seconds. To eliminate this you will need to put the following before each sleep:
[code:1:c97a003472]
if [ ! -f $TMPFILE ]; then
break
fi
[/code:1:c97a003472]
soup4you2
June 3rd, 2003, 00:26
thanks....... appriciate all your help.....
soup4you2
June 3rd, 2003, 10:13
works great... thanks again....... i left out the break so once it completes it still sits there and twirls away so i can goto my other work and not have to worry about my ssh connection being dropped...
YOU DA MAN
elmore
October 20th, 2003, 20:26
EDIT:
I think the above code won't work if the user has a TERMCAP defined differently. Perhaps running 'set tty erase ^H' beforehand would fix that? I'm not sure.
portable backspace delete character would be
[code:1:30071a0f9a]
printf '\177'
[/code:1:30071a0f9a]
I was trying to incorporate this twirly bar into the script I wrote the other day by my while command doesn't want to terminate no matter what I do once the find is done. I'm gonna play with it a little longer to see if I can figure it before I post. :(
soup4you2
October 20th, 2003, 21:03
i had no issues getting it to terminate..
i did
funk() {
touch $TWIRL_LOCK
twirlie &
command'age
rm $TWIRL_LOCK
}
workes great for me..
elmore
October 21st, 2003, 16:53
[quote="elmore"]
[code:1:8527ea1894]
printf '\177'
[/code:1:8527ea1894]
Correcting myself here, I have found \177 doesn't always work, but I have had good success with
[code:1:8527ea1894]
printf '\010'
[/code:1:8527ea1894]
which is backspace, vs delete. ascii(7)
soup4you2
October 21st, 2003, 16:58
and one annoying thing about the twirling cursor is if somebody breaks outta the process they gotta remove the lockfile or else it keeps twirling and twirling.. So a good measure i found was:
touch $TWIRL_LOCK
stty ignbrk
twirlie &
command
rm $TWIRL_LOCK
stty -ignbrk
stty ignbrk will tell your script not to allow control-c or breaks..
elmore
October 21st, 2003, 18:34
yeah I nioticed that. I was wondering how to stop that. Now I know.
I made a couple of twirlie doo-dad thingys. This is the one frisco mntioned.
[code:1:06c16bcca4]
##
## Eye Candy, meaningless, gives something to look at
##
Candy () {
while [ -f /tmp/tmp ]; do
echo -n .
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n o
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n O
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n 0
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n O
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n o
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n .
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
done
[/code:1:06c16bcca4]
The other is one I did. Is a little long but cool ntl it looks like this:
[code:1:06c16bcca4]
##
## Eye Candy, meaningless, gives something to look at
##
Candy () {
while [ -f /tmp/tmp ]; do
echo -n f
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n fi
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
echo -n fin
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
echo -n find
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n findi
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n findin
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n finding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n Finding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FInding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDing
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDIng
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDINg
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDING
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDINg
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDIng
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINDing
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FINding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n FInding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n Finding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n finding
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n findin
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n findi
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n find
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
printf '\177'
echo -n fin
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
printf '\177'
echo -n fi
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
printf '\177'
echo -n f
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
printf '\177'
echo -n
if [ ! -f /tmp/tmp ]; then
break
fi
sleep .1;
done
[/code:1:06c16bcca4]
soup4you2
October 21st, 2003, 19:30
nice.......
i wanted to write one but too lazy of the ======> that goes back and forth.. easy to do.. just dont feel like it..
is this going to be a new era of ascii art shell scripting?
soup4you2
October 24th, 2003, 12:05
oops
sorry my above post should have been
#!/bin/sh
SigInt() {
echo "No CTRL+C allowed!"
}
trap "SigInt 2" INT
while true
do
touch $TWIRL_LOCK
twirlie &
command
rm $TWIRL_LOCK
done
nutznboltz
April 8th, 2004, 17:49
I can't believe everyone forgot to use tput to get terminal independence.
This requires a shell that does SysV-style backslashes. GNU bash does it the same way on both Solaris and FreeBSD which simplifies portability. I can't remember why I made this write all it's output on stderr since it's been so long.
[code:1:6a0e58fd8c]
tw=1
twiddle() {
case $tw in
1)
echo -e '|\c' 1>&2
;;
2)
echo -e '/\c' 1>&2
;;
3)
echo -e '-\c' 1>&2
;;
4)
echo -e '\\\c' 1>&2
;;
esac
case `uname` in
FreeBSD)
tput le 1>&2
;;
SunOS)
tput cub 1 1>&2
;;
esac
(( tw += 1 ))
if [ "$tw" = "5" ];then
tw=1
fi
}
[/code:1:6a0e58fd8c]
repeatedly call twiddle in between steps. This can also be run as a background job that calls sleep and then the twiddle function until it gets killed by its parent. Sun does exactly that in Solaris when you are booting diskless, probably when you are booting from CD-ROM too.
Use trap to clean up. This code is somewhat self-documenting:
[code:1:6a0e58fd8c]
twiddle &
childpid=$!
trap "kill $childpid" 0 1 2 3 15
[/code:1:6a0e58fd8c]
"signal" 0 is an indication to do the trap at exit time from non-signal condition.
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.