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 ($?);
}
}
In reply to Scrollem
by spm
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.