Determine length of input as it is typed

Discussion in 'Perl Misc' started by destraynor@gmail.com, Mar 18, 2006.

  1. Guest

    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
    , Mar 18, 2006
    #1
    1. Advertising

  2. wrote in
    news::

    > 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__


    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 18, 2006
    #2
    1. Advertising

  3. Guest

    A. Sinan Unur wrote:
    > wrote in
    > news::
    >
    > > 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
    , Mar 21, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ersin Gençtürk
    Replies:
    1
    Views:
    3,478
    Ersin Gençtürk
    Oct 6, 2004
  2. macuser55
    Replies:
    4
    Views:
    274
    Dan Pop
    Oct 14, 2004
  3. Yingjie Lan
    Replies:
    4
    Views:
    299
    John Nagle
    Jan 29, 2010
  4. Peña, Botp
    Replies:
    1
    Views:
    231
    Robert Klemme
    Jan 24, 2004
  5. darius
    Replies:
    1
    Views:
    305
    Dan Rumney
    Jun 4, 2008
Loading...

Share This Page