Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions nowrap
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use Text::CharWidth::PurePerl qw(mbwidth);
use open ':locale';

my $TABSTOP = 8;
my $ESCAPE_SEQUENCE_PATTERN = qr/(\e\[\d*(;\d+)*m)/;
my $ESCAPE_SEQUENCE_PATTERN = qr/(\e(\[\d*(;\d+)*m|\e*[ \t]*[^\e]))/;

my $columns = `tput cols`;
chomp($columns);
Expand Down Expand Up @@ -138,15 +138,20 @@ while (my $line = <>) {
$cursor += $TABSTOP - ($cursor % $TABSTOP);
++$nchars;
}
elsif ($c eq "\e") {
elsif ($c eq "\e" && substr($line, $i, length($line) - $i) =~ m/$ESCAPE_SEQUENCE_PATTERN/) {
# handle escape sequences
substr($line, $i, length($line) - $i) =~ m/$ESCAPE_SEQUENCE_PATTERN/;
die "\$` should be empty, stopped" if $`;
my $esc_seq = $1;
# skip over the sequence
$i += length($esc_seq) - 1; # -1 b/c of ++$i at loop top
$nchars += length($esc_seq);
# $cursor is unchanged

my $tabCount = () = $esc_seq =~ /\t/g;
if ($tabCount > 0) {
# In contrast to spaces, tabs within the escape sequence are not
# swallowed.
$cursor += ($tabCount * $TABSTOP) - ($cursor % $TABSTOP);
}

$append = $esc_seq;
}
Expand Down
13 changes: 9 additions & 4 deletions script/nowrap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use open ':locale';

my $TABSTOP = 8;
my $ESCAPE_SEQUENCE_PATTERN = qr/(\e\[\d*(;\d+)*m)/;
my $ESCAPE_SEQUENCE_PATTERN = qr/(\e(\[\d*(;\d+)*m|\e*[ \t]*[^\e]))/;

my $columns = `tput cols`;
chomp($columns);
Expand Down Expand Up @@ -92,15 +92,20 @@
$cursor += $TABSTOP - ($cursor % $TABSTOP);
++$nchars;
}
elsif ($c eq "\e") {
elsif ($c eq "\e" && substr($line, $i, length($line) - $i) =~ m/$ESCAPE_SEQUENCE_PATTERN/) {
# handle escape sequences
substr($line, $i, length($line) - $i) =~ m/$ESCAPE_SEQUENCE_PATTERN/;
die "\$` should be empty, stopped" if $`;
my $esc_seq = $1;
# skip over the sequence
$i += length($esc_seq) - 1; # -1 b/c of ++$i at loop top
$nchars += length($esc_seq);
# $cursor is unchanged

my $tabCount = () = $esc_seq =~ /\t/g;
if ($tabCount > 0) {
# In contrast to spaces, tabs within the escape sequence are not
# swallowed.
$cursor += ($tabCount * $TABSTOP) - ($cursor % $TABSTOP);
}

$append = $esc_seq;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/tcescape.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ordinary t
thisboldte
singleescap
twoescapeocc
adjacentesc
adjacentesc
after single
after manysp
after tab
after
endescape
endescap
12 changes: 12 additions & 0 deletions tests/tcescape.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ordinary text
thisboldtext
singleescape
twoescapeoccurrences
adjacentescapes
adjacentescapes
after singlespace
after manyspaces
after tabulator
after tabulators
endescape
endescap
3 changes: 3 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ do_test tc6 --columns=72
# ==== case 7: UTF-8-demo.txt @ 40 columns
do_test tc7 --columns=40

# ==== escape characters and colored text
do_test tcescape --columns=10

# ==== wrap
do_test tcwrap --wrap --columns=10
do_test tcindent-plain --wrap --indent-string '> ' --columns=10
Expand Down