Reputation: 125
Not sure how to do this, but I've go a program that every time I run I have to enter in two data points, and they are hard to find in the console--squinting and so on. Is there a way to colorize the INPUT, so while I'm looping through the program repetitively (putting these tuples in and so on) I can keep track of where I am from the last program run. I'd like it to be whatever color is the inverted color of lime green--I'm in "Invert colors" mode now.
What I'd like to achieve is a colorization that colorizes as I type input in--you know, an input stream colorization or something.
print "Enter where you'd like me to begin: ";
my $begin = <STDIN>;
chomp $begin;
exit 0 if ($begin eq "");
print "\n";
print "Enter where you'd like me to end: ";
my $end = <STDIN>;
chomp $end;
exit 0 if ($end eq "");
print "\n";
Upvotes: 1
Views: 129
Reputation: 5159
Perl programs echo the STDIN
as you type it. Since you want to colorize only what is typed in, you'll have to capture it and then print it with color. I modified your program to show how to do this.
#!/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
use Term::ReadKey;
ReadMode('noecho'); # don't echo
print "Enter where you want me to begin: ";
my $begin = <STDIN>;
chomp $begin;
exit 0 if ($begin eq "");
print colored("$begin\n", 'red');
print "Enter where you'd like me to end: ";
my $end = <STDIN>;
chomp $end;
exit 0 if ($end eq "");
print colored("$end\n", 'red');
ReadMode(0); # back to normal
Example output:
Enter where you want me to begin: 1
Enter where you'd like me to end: 2
(The 1 and 2 are in red)
Red is the inverse of green, so that's why I picked it since you're running in inverse color mode on your console. You could also try bright_red if that's not close enough to lime green as you'd like.
Here is the documentation for Term::ANSIColor and Term::ReadKey
Upvotes: 1