@@ -562,31 +562,37 @@ final public function progress(?int $total = null): ProgressBar
562562 */
563563 final public function justify (string $ first , ?string $ second = '' , array $ options = []): self
564564 {
565+ $ second = trim ($ second );
566+ $ first = trim ($ first );
567+
568+ $ start = $ first ;
569+ $ end = $ second ;
570+
565571 $ options = [
566572 'first ' => $ options ['first ' ] ?? [],
567573 'second ' => ($ options ['second ' ] ?? []) + ['bold ' => 1 ],
568574 'sep ' => (string ) ($ options ['sep ' ] ?? '. ' ),
569575 ];
570576
571- $ second = trim ((string ) $ second );
572- $ first = trim ($ first );
577+ if (preg_match ('/( \\x1b(?:.+)m)/U ' , $ first , $ matches )) {
578+ $ first = str_replace ($ matches [1 ], '' , $ first );
579+ $ first = preg_replace ('/ \\x1b\[0m/ ' , '' , $ first );
580+ }
581+ if (preg_match ('/( \\x1b(?:.+)m)/U ' , $ second , $ matches )) {
582+ $ second = str_replace ($ matches [1 ], '' , $ second );
583+ $ second = preg_replace ('/ \\x1b\[0m/ ' , '' , $ second );
584+ }
573585
574586 $ firstLength = strlen ($ first );
575587 $ secondLength = strlen ($ second );
576588
577- if (preg_match ('/^ \\x1b(?:.+)m(.+) \\x1b(?:.+)m$/ ' , $ first , $ matches )) {
578- $ firstLength = strlen ($ matches [1 ]);
579- }
580- if (preg_match ('/^ \\x1b(?:.+)m(.+) \\x1b(?:.+)m$/ ' , $ second , $ matches )) {
581- $ secondLength = strlen ($ matches [1 ]);
582- }
583-
584589 $ dashWidth = ($ this ->terminal ->width () ?: 100 ) - ($ firstLength + $ secondLength );
585590 $ dashWidth -= $ second === '' ? 1 : 2 ;
591+ $ dashWidth = $ dashWidth < 0 ? 0 : $ dashWidth ;
586592
587- $ first = $ this ->color ->line ($ first , $ options ['first ' ]);
593+ $ first = $ this ->color ->line ($ start , $ options ['first ' ]);
588594 if ($ second !== '' ) {
589- $ second = $ this ->color ->line ($ second , $ options ['second ' ]);
595+ $ second = $ this ->color ->line ($ end , $ options ['second ' ]);
590596 }
591597
592598 return $ this ->write ($ first . ' ' . str_repeat ($ options ['sep ' ], $ dashWidth ) . ' ' . $ second )->eol ();
@@ -640,6 +646,6 @@ private function initProps()
640646 $ this ->reader = $ this ->io ->reader ();
641647 $ this ->color = $ this ->writer ->colorizer ();
642648 $ this ->cursor = $ this ->writer ->cursor ();
643- $ this ->terminal = new Terminal ();
649+ $ this ->terminal = $ this -> writer -> terminal ();
644650 }
645651}
0 commit comments