diff options
| author | Daniel Friesel <derf@finalrewind.org> | 2010-12-12 10:11:03 +0100 | 
|---|---|---|
| committer | Daniel Friesel <derf@finalrewind.org> | 2010-12-12 10:11:03 +0100 | 
| commit | 7b5ebce9a0c1f36aa926faaa3e49380e04ecc83e (patch) | |
| tree | 079db80d86c878071f386513d23d57636b037cff | |
| parent | 49eb6c4218467ddcac828c48ab86b60bbdcc3c88 (diff) | |
Cover more line breaking corner cases
| -rwxr-xr-x | bin/icli | 21 | 
1 files changed, 15 insertions, 6 deletions
| @@ -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) { | 
