#!/usr/bin/env perl use strict; use warnings; use constant BASE_LINE_LENGTH => 40; use Term::ANSIColor 'colored'; my $base_line_length = @ARGV ? $ARGV[0] : BASE_LINE_LENGTH; my @strings = ( 'A message indicating success might look' . " some\N{SOFT HYPHEN}thing like this: " . _good("OK: some\N{SOFT HYPHEN}thing worked.") . ' That is, it should be green.', 'A message indicating failure might look some-thing like this: ' . _bad("Error: something didn't work.") . ' That is, it should be yellow on red.', ); my $off_code = "\x{1b}[0m"; my $last_code = $off_code; for my $string (@strings) { my $char_count = 0; for my $substr (split /(\x{1b}\[[0-9;]+m)/, $string) { if ($substr =~ /^\x{1b}/) { $last_code = $substr; } else { print $last_code; CHAR: for my $char (split //, $substr) { if ($char_count == 0) { print $last_code; } if ($char_count > $base_line_length && $char =~ /[\x{ad} -]/) { print '-' unless $char eq ' '; print "$off_code\n"; $char_count = 0; next CHAR; } unless ($char eq "\x{ad}") { print $char; ++$char_count; } } } } print "$off_code\n"; } sub _good { my ($string) = @_; return colored($string, 'green'); } sub _bad { my ($string) = @_; return colored($string, 'yellow on_red'); }