From 7b5ebce9a0c1f36aa926faaa3e49380e04ecc83e Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 12 Dec 2010 10:11:03 +0100 Subject: Cover more line breaking corner cases --- bin/icli | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'bin') diff --git a/bin/icli b/bin/icli index ddf7978..a88c287 100755 --- a/bin/icli +++ b/bin/icli @@ -116,19 +116,28 @@ sub split_by_words { my @words = split(/ /, $str); my @ret; - foreach my $word (@words) { - if (length($word) > $max_w) { - # FIXME we can do better - $word = substr($word, 0, $max_w); + + while (grep { length($_) > $max_w } @words) { + for my $i ( 0 .. $#words ) { + my $word = $words[$i]; + + if (length($word) > $max_w) { + splice(@words, $i, 1, substr($word, 0, $max_w), substr($word, $max_w)); + last; + } } } while (@words) { my $cur_str = q{}; - while (@words and ((length($cur_str) + length($words[0])) < $max_w)) { - if (length($cur_str)) { + my $tr_space = 0; + while (@words and ((length($cur_str) + length($words[0]) + $tr_space) <= $max_w)) { + if ($tr_space) { $cur_str .= ' '; } + else { + $tr_space = 1; + } $cur_str .= shift(@words); } if (@ret) { -- cgit v1.2.3