@@ -21,23 +21,34 @@ protected function tabs(
21
21
): string {
22
22
$ strippedWidth = fn (string $ value ): int => mb_strwidth ($ this ->stripEscapeSequences ($ value ));
23
23
24
+ // Build the top row for the tabs by adding whitespace equal
25
+ // to the width of each tab plus padding, or by adding an
26
+ // equal number of box characters for the selected tab.
24
27
$ top_row = $ tabs ->map (fn ($ value , $ key ) => $ key === $ selected
25
28
? '╭ ' . str_repeat ('─ ' , $ strippedWidth ($ value ) + 2 ) . '╮ '
26
29
: str_repeat (' ' , $ strippedWidth ($ value ) + 4 )
27
30
)->implode ('' );
28
31
32
+ // Build the middle row for the tabs by adding the tab name
33
+ // surrounded by some padding. But if the tab is selected
34
+ // then highlight the tab and surround it in box chars.
29
35
$ middle_row = $ tabs ->map (fn ($ value , $ key ) => $ key === $ selected
30
36
? "{$ this ->dim ('│ ' )} {$ this ->{$ color }($ value )} {$ this ->dim ('│ ' )}"
31
37
: " {$ value } "
32
38
)->implode ('' );
33
39
40
+ // Build the bottom row for the tabs by adding box characters equal to the width
41
+ // of each tab, plus padding. If the tab is selected, add the appropriate box
42
+ // characters instead. Finally, pad the whole line to fill the width fully.
34
43
$ bottom_row = $ tabs ->map (fn ($ value , $ key ) => $ key === $ selected
35
44
? '┴ ' . str_repeat ('─ ' , $ strippedWidth ($ value ) + 2 ) . '┴ '
36
45
: str_repeat ('─ ' , $ strippedWidth ($ value ) + 4 )
37
46
)->implode ('' );
38
47
$ bottom_row = $ this ->pad ($ bottom_row , $ width , '─ ' );
39
48
40
- // automatic horizontal tab scrolling
49
+ // If the tabs are wider than the provided width, we need to trim the tabs to fit.
50
+ // We remove the appropriate number of characters from the beginning and end of
51
+ // each row by using the highlighted tab's index to get it's scroll position.
41
52
if ($ strippedWidth ($ top_row ) > $ width ) {
42
53
$ scroll = $ selected / ($ tabs ->count () - 1 );
43
54
$ chars_to_kill = $ strippedWidth ($ top_row ) - $ width ;
@@ -47,6 +58,9 @@ protected function tabs(
47
58
}
48
59
}
49
60
61
+ // We wait until now to dim the top and bottom
62
+ // rows, otherwise the horizontal scrolling
63
+ // could easily strip those instructions.
50
64
return collect ([$ this ->dim ($ top_row ), $ middle_row , $ this ->dim ($ bottom_row )])->implode (PHP_EOL );
51
65
}
52
66
}
0 commit comments