#!/usr/bin/perl -w use strict; use Text::Tabs; sub do_help { print </dev/null |grep COLUMNS`; s/\D*//g; } if ( !$_ || ! /^\d+$/ ) { $_ = 80; } # fallback to 80 $width = $_; } $width -= $end + 1; # keep track of tail # set colors my $cA = ''; my $cB = ''; # default = no colors if ($color == 1 || ($color == 0 && `dircolors 2>/dev/null` =~ /[a-z]=/) ) { $cA = "\e[7;40m"; $cB = "\e[0;0m"; } while (<>) { my $backup = $_; my $esc = '(?:[^[:print:]]\\[(?:(?:\d{1,2};)?\d{1,2})?m(?:[^[:print:]]\\[K)?)'; # regexp for escapes #my $esc = '[^[:print:]]'; # regexp for escapes # Cease all coloring if escapes are found and not cleared by end of line. if ($color != 1 && /.*($esc)/ && $1 !~ /\e\[(?:00?;)?00?m/) { $cA = $cB = '' } $_ = expand($_); # expand tabs chomp; # note, this might need $end+1 for some reason (I stripped it in an update) if (/^((?:$esc*[[:print:]]){$width})($esc*[[:print:]])((?:$esc*[[:print:]]){$end,})$/) { # width exceeded? $_ = "$1$cA$2$cB"; # assemble trunc'd line my $stripped = "$3"; $stripped =~ s/[[:print:]]+($esc+)/$1/g; # rescue cropped escapes if ($end) { $_ .= "..."; } # this part is broken: #$_ .= "$stripped$cB\n"; # append escapes, break } else { $_ = $backup; } # otherwise use original print; }