A little app that scrolls text around. A few command line options, including "--help"... I must have dozed off or something while writing it ;)
I wondered for a bit if this belongs in the Code node instead of the snippets node...
#!/usr/bin/perl
################
$| = 1; # No buffering plz.
$SIG{INT} = \&sig; # Signal handler.
$timeout = 0.20; # Timeout.
$newline = 0; # Show newline on exit?
$termwidth = 80; # Terminal Width?
$color = "\e[0m";
$default_color = "\e[0m";
$command = "";
################
sub sig {
#~ Catches Interrupt Signals (defined above)
print($default_color);
if (!$newline) { print("\r" . " " x $termwidth . "\r"); }
exit();
}
sub bs {
#~ The "backspace" routine. - scrolls everything
#~ forward a bit.
$str =~ s/^(.)(?{$f=$1;})//;
$str .= $f;
s/(\n|\t)/ /g;
$str =~ s/(\n|\t)/ /g;
s/$/$f/;
s/^(.)//;
if (length($_) > ($termwidth - 1)) {
s/(.{$termwidth - 3}).*/$1\.\.\./;
}
print("\r" . " " x 80 . "\r");
$color = "\e[0m" if ($color eq "white");
$color = "\e[31m" if ($color eq "red");
$color = "\e[34m" if ($color eq "blue");
$color = "\e[32m" if ($color eq "green");
print("\r$color$_$default_color");
select(undef, undef, undef, $timeout);
}
sub init {
#~ Load values ...
map {
if (/^--(.*)=(.*)/) {
if (defined($$1)) {
$$1 = $2;
chomp($$1);
}
} elsif (/^--(.*)/) {
if (defined($$1)) {
$$1 = 1;
}
}
} @ARGV;
#~ Scroller initialization routine.
$str = $_;
$str =~ s/\n/ /g;
s/\n/ /g;
$_ = $"x($termwidth - 1);
$str .= $"x($termwidth - 1 - length($str));
if (length($_) > ($termwidth - 1)) { s/(.{77}).*/$1\.\.\./; }
print;
}
################
#~ The main code
if(defined($ARGV[0]) && $ARGV[0] ne "") {
if ($ARGV[0] =~ /^--help/) {
$| = 0;
print("Use: $0 <text> [--option[=value]]\n");
print("Options:\n");
print(" --timeout=<value>\tThe timeout between each l
+etter scroll. Default is 0.20\n");
print(" --newline\t\tMake a new line when exiting.\n"
+);
print(" --color=<value>\tColor the text this color. D
+efault is \"white\"\n");
print(" \t\t\tColors: white, blue, red, green\n");
print(" --termwidth=<value>\tThe terminal width. Defa
+ult is 80\n");
print(" \t\t\tdo '--termwidth=`echo \$COLUMNS`' to au
+todetect\n");
print(" --command=<value>\tA command to run every tim
+e the buffer is empty.\n");
print(" \t\t\tDefault is blank, which means no comman
+d. (just the\n");
print(" \t\t\ttext, over and over and over again.)\n"
+);
exit();
} else {
$_ = shift @ARGV;
}
} else {
$_ = "Author: Smári P. McCarthy. Just Another Perl Hack
+er. E-mail: spm\@suse.eyjar.is Execute '$0 --help' fo
+r help. ";
}
init();
while(1) {
for ($i = 0; $i < length($_); $i++) {
bs();
}
if ($command ne "") {
$str = `$command` . " ";
die("\nDead: Application '$command' not found, so I di
+dn't know what to do.\n") if ($?);
}
}