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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.