Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Just as an excuse to play around with single line ANSI control sequences, I came up with this. It runs in an xterm on ArchLinux ( mostly because that's all I have to play with ).

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11115029 use warnings; my $newline = promptandreviseline( "Previous Value : ", 'previous line +' ); print "The New Line is: $newline\n"; ###################################################################### use Term::ReadKey; use Data::Dump qw( pp ); sub promptandreviseline { my ($prompt, $line) = @_; $line //= ''; $line =~ tr/\t\n/ /; local $| = 1; my $startcolumn = 1 + length $prompt; print "\e[1G$prompt"; my $cursor = length $line; my $pos = $startcolumn + $cursor; print "\e[${startcolumn}G\e[K$line\e[${pos}G"; my $more = 1; ReadMode 'raw'; while( $more ) { sysread STDIN, my $input, 4096 or last; for my $key ( $input =~ /\e[^\e]+|./gs ) { if( $key =~ /^[\cc\e]\z/ ) { $more = 0 } # ^c escape elsif( $key =~ /^[\n\r]\z/ ) { print "\n"; $line .= "\n"; $more = 0 } elsif( $key =~ /^[ -~]$/ ) { substr $line, $cursor++, 0, $key } +# char elsif( $key eq "\b" ) # backspace { $cursor > 0 and substr $line, --$cursor, 1, ''; } elsif( $key eq "\e[3~" ) # Delete { $cursor < length($line) and substr $line, $cursor, 1, ''; } elsif( $key eq "\e[D" ) { $cursor > 0 and $cursor-- } # left arr +ow elsif( $key eq "\e[C" ) { $cursor < length($line) and ++$cursor +} # right elsif( $key eq "\e[F" ) { $cursor = length($line) } # End elsif( $key eq "\e[H" ) { $cursor = 0 } # Home else # invalid, show sequence { my $seq = pp $key; substr $line, $cursor, 0, $seq; $cursor += length $seq; } $pos = $startcolumn + $cursor; $more and print "\e[${startcolumn}G$line\e[K\e[${pos}G"; } } ReadMode 'restore'; return $line; }

In reply to Re: pre-texted <STDIN> by tybalt89
in thread pre-texted <STDIN> by ShainEdge

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found