in reply to scalar or sub that can tell me the terminal print offset ?

I don't know of any module that provides this, but it doesn't seem like it would be that hard to implement.

Oh what the heck, Something like this perhaps? (Could probably use some further testing.)

UPDATE: fixed missing \a (bell) handling.

use warnings; use strict; use Test; my ($linecount, $current_col, $tab) = (0, 0, 4); sub pr { for my $string (@_){ print $string; my $lines += () = $string =~ /[\n\r]/g; $current_col = 0 if $lines; $linecount += $lines; $string =~ s/.*[\n\r](.*)$/$1/sg; $string =~ s/\t/' ' x $tab/eg; my $bs = $string =~ tr/\b//; my $bell = $string =~ tr/\a//; $current_col += (length $string) - $bs * 2 - $bell; $current_col = 0 if $current_col < 0; $linecount += int($current_col / 80); $current_col %= 80; } } BEGIN { plan tests => 24 }; my @tests = ( ["1234567890", 0, 10], ["12345\n67890", 1, 5], ["12345\n67890\n12345", 2, 5], ["12345\r67890", 1, 5], ["12345\r67890\r12345", 2, 5], ["12345\t67890", 0, 10 + $tab], ["1234567890\b\b\b\b\b", 0, 5], [qw/1234567890 1234567890 1234567890 1234567890 1234567890/, 0, 50], ["1234567890" x 10, 1, 20], ["1234567890" x 100, 12, 40], ["1234567890" x 100, "\b" x 50, 12, 0], ["1\b2\b3\b4\b5\b6\b7\b8\b9\b0\b\n12345\t67890\n\t12345\b\b\b\b\b67890 +\t", 2, 5 + $tab*2], ["1234567890\a\b\a\b\a\b", 0, 7], ); for my $test (@tests){ ( $linecount, $current_col ) = ( 0, 0 ); my @strings = @$test; splice @strings, -2, 2, ''; pr $_ for @strings; print "<--\n"; ok($linecount, $test->[-2], "Linecount incorrect"); ok($current_col, $test->[-1], "Column count incorrect"); }

Replies are listed 'Best First'.
Re^2: scalar or sub that can tell me the terminal print offset ?
by palkia (Monk) on Mar 19, 2012 at 22:03 UTC
    Very similar to what I've done,
    but note that (at least in my terminal) you can't sub the tab for spaces, since it have a changing length based on previous characters.
    Here's my version (I only needed columns this time):
    my prntOffset = 0; sub prink #(string to print) { my $prntText = shift; print $prntText; if($prntText =~ s/.*[\n\r](.*)$/$1/s){$prntOffset = 0;} while(length $prntText) { if($prntText =~ s/^((\w|[\Q\\\/\{\};'"\[\]\.,\?\~\!@#$%^&* ()\E])+)// ) {$prntOffset += length $1;} #normal chars elsif(substr($prntText,0,1) eq "\b") { $prntText = substr($prntText,1); $prntOffset-- if $prntOffset; } elsif($prntText =~ s/^(\a+)//){} #yes nothing elsif($prntText =~ s/^(\t+)//) {$prntOffset += 8 * (length $1) - ($prntOffset % 8);} else { die "\ano pattern match for \"".substr($prntText,0,1)."\" in \"prink\" function\n"; } } $prntOffset %= 80; }
    Seems to hold for the few but focused tests I ran.