@@ -293,6 +293,7 @@ protected function substr($string, $start = 0, $length = null)
293293 protected function wordwrap ($ str , $ width = 75 , $ break = "\n" , $ cut = false )
294294 {
295295 $ lines = explode ($ break , $ str );
296+ $ color_reset = $ this ->colors ->getColorCode (Colors::C_RESET );
296297 foreach ($ lines as &$ line ) {
297298 $ line = rtrim ($ line );
298299 if ($ this ->strlen ($ line ) <= $ width ) {
@@ -301,18 +302,30 @@ protected function wordwrap($str, $width = 75, $break = "\n", $cut = false)
301302 $ words = explode (' ' , $ line );
302303 $ line = '' ;
303304 $ actual = '' ;
305+ $ color = '' ;
304306 foreach ($ words as $ word ) {
307+ if (preg_match_all (Colors::C_CODE_REGEX , $ word , $ color_codes ) ) {
308+ # Word contains color codes
309+ foreach ($ color_codes [0 ] as $ code ) {
310+ if ($ code == $ color_reset ) {
311+ $ color = '' ;
312+ } else {
313+ # Remember color so we can reapply it after a line break
314+ $ color = $ code ;
315+ }
316+ }
317+ }
305318 if ($ this ->strlen ($ actual . $ word ) <= $ width ) {
306319 $ actual .= $ word . ' ' ;
307320 } else {
308321 if ($ actual != '' ) {
309322 $ line .= rtrim ($ actual ) . $ break ;
310323 }
311- $ actual = $ word ;
324+ $ actual = $ color . $ word ;
312325 if ($ cut ) {
313326 while ($ this ->strlen ($ actual ) > $ width ) {
314327 $ line .= $ this ->substr ($ actual , 0 , $ width ) . $ break ;
315- $ actual = $ this ->substr ($ actual , $ width );
328+ $ actual = $ color . $ this ->substr ($ actual , $ width );
316329 }
317330 }
318331 $ actual .= ' ' ;
@@ -322,4 +335,4 @@ protected function wordwrap($str, $width = 75, $break = "\n", $cut = false)
322335 }
323336 return implode ($ break , $ lines );
324337 }
325- }
338+ }
0 commit comments