Determine length of input as it is typed

D

destraynor

Hi all,
I have a quick question, I have a command line app that takes
text input. What I'd like to be able to do is determine the length of
the input, as it is typed. So, for example, if the string goes above
100 characters, I can let the user know. Something like this would be
ideal...
---------------------------------------------------------------
~dez > perl sms.pl
Characters remaining 100
|
---------------------------------------------------------------
and then onto...
---------------------------------------------------------------
~dez > perl sms.pl
Characters remaining 64
Hi there, long time no see. Check out nakatomiweb.com
---------------------------------------------------------------

I'm (obviously) not looking for someone to write this for me, just some
information beyond "omg curses.h dude".

Thanks for your time,
Des
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I have a quick question, I have a command line app that takes
text input. What I'd like to be able to do is determine the length of
the input, as it is typed. So, for example, if the string goes above
100 characters, I can let the user know. Something like this would be
ideal...
....

I'm (obviously) not looking for someone to write this for me, just
some information beyond "omg curses.h dude".

Well, here is something that is obviously primitive and flawed (no
editing facilities):

#!/usr/bin/perl

use strict;
use warnings;

use Term::ReadKey;

my $s = read_input(100);
print "$s\n";

sub read_input {
my ($limit) = @_;
my $s = q{};

ReadMode 4;
READKEY: while ( my $left = $limit - length $s ) {
syswrite STDOUT, "\r(Limit = $left): $s";

my $key;
1 while not defined ($key = ReadKey -1);
last READKEY if $key eq "\015";
next READKEY unless $key =~ /[[:alnum:][:punct:] ]/;
$s .= $key;
}
ReadMode 0;
return $s;
}

__END__
 
D

destraynor

A. Sinan Unur said:
(e-mail address removed) wrote in
I have a quick question, I have a command line app that takes
text input. What I'd like to be able to do is determine the length of
the input, as it is typed. So, for example, if the string goes above
100 characters, I can let the user know. Something like this would be
ideal...
...

I'm (obviously) not looking for someone to write this for me, just
some information beyond "omg curses.h dude".

Well, here is something that is obviously primitive and flawed (no
editing facilities):

#!/usr/bin/perl

use strict;
use warnings;

use Term::ReadKey;

my $s = read_input(100);
print "$s\n";

sub read_input {
my ($limit) = @_;
my $s = q{};

ReadMode 4;
READKEY: while ( my $left = $limit - length $s ) {
syswrite STDOUT, "\r(Limit = $left): $s";

my $key;
1 while not defined ($key = ReadKey -1);
last READKEY if $key eq "\015";
next READKEY unless $key =~ /[[:alnum:][:punct:] ]/;
$s .= $key;
}
ReadMode 0;
return $s;
}

__END__
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Thank you, thats a good start
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top