|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\OutputFormatters\Transformations\Wrap; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Helper\TableStyle; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Calculate column widths for table cells. |
|
8
|
|
|
* |
|
9
|
|
|
* Influenced by Drush and webmozart/console. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ColumnWidths |
|
12
|
|
|
{ |
|
13
|
|
|
protected $widths; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->widths = []; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Given the total amount of available space, and the width of |
|
22
|
|
|
* the columns to place, calculate the optimum column widths to use. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function calculate($totalWidth, DataCellWidths $dataWidths, $minimumWidths = []) |
|
25
|
|
|
{ |
|
26
|
|
|
// First, check to see if all columns will fit at their full widths. |
|
27
|
|
|
// If so, do no further calculations. (This may be redundant with |
|
28
|
|
|
// the short column width calculation.) |
|
29
|
|
|
$widths = $dataWidths->widths(); |
|
30
|
|
|
if (DataCellWidths::sumWidth($widths) <= $totalWidth) { |
|
31
|
|
|
$widths = $this->enforceMinimums($widths, $minimumWidths); |
|
32
|
|
|
return $widths; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Get the short columns first. If there are none, then distribute all |
|
36
|
|
|
// of the available width among the remaining columns. |
|
37
|
|
|
$shortColWidths = $this->getShortColumns($totalWidth, $dataWidths, $minimumWidths); |
|
38
|
|
|
if (empty($shortColWidths)) { |
|
39
|
|
|
return $this->distributeLongColumns($totalWidth, $dataWidths, $minimumWidths); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// If some short columns were removed, then account for the length |
|
43
|
|
|
// of the removed columns and make a recursive call (since the average |
|
44
|
|
|
// width may be higher now, if the removed columns were shorter in |
|
45
|
|
|
// length than the previous average). |
|
46
|
|
|
$totalWidth -= DataCellWidths::sumWidth($shortColWidths); |
|
47
|
|
|
$remainingColWidths = $this->calculate($totalWidth, $dataWidths); |
|
48
|
|
|
|
|
49
|
|
|
return array_merge($shortColWidths, $remainingColWidths); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return all of the columns whose longest line length is less than or |
|
54
|
|
|
* equal to the average width. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getShortColumns($totalWidth, DataCellWidths $dataWidths, $minimumWidths) |
|
57
|
|
|
{ |
|
58
|
|
|
$averageWidth = $this->averageWidth($totalWidth, $dataWidths); |
|
59
|
|
|
$shortColWidths = $dataWidths->removeShortColumns($averageWidth); |
|
60
|
|
|
$shortColWidths = $this->enforceMinimums($shortColWidths, $minimumWidths); |
|
61
|
|
|
return $shortColWidths; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Distribute the remainig space among the columns that were not |
|
66
|
|
|
* included in the list of "short" columns. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function distributeLongColumns($totalWidth, DataCellWidths $dataWidths, $minimumWidths) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
// Just distribute the remainder without regard to the |
|
71
|
|
|
// minimum widths. For now. |
|
72
|
|
|
return $dataWidths->distribute($totalWidth); |
|
73
|
|
|
/* |
|
74
|
|
|
// Check to see if there is only one column remaining. |
|
75
|
|
|
// If so, give it all of the remaining width. |
|
76
|
|
|
$remainingWidths = $dataWidths->widths(); |
|
77
|
|
|
if (count($remainingWidths) <= 1) { |
|
78
|
|
|
return $remainingWidths; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Start by giving each column its minimum width |
|
82
|
|
|
$result = $minimumWidths; |
|
83
|
|
|
$totalWidth -= DataCellWidths::sumWidth($result); |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return $result; |
|
87
|
|
|
*/ |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Ensure that every item in $widths that has a corresponding entry |
|
92
|
|
|
* in $minimumWidths is as least as large as the minimum value held there. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function enforceMinimums($widths, $minimumWidths) |
|
95
|
|
|
{ |
|
96
|
|
|
$result = []; |
|
97
|
|
|
$minimumWidths += $widths; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($widths as $key => $value) { |
|
100
|
|
|
$result[$key] = min($value, $minimumWidths[$key]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Calculate how much space is available on average for all columns. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function averageWidth($totalWidth, DataCellWidths $dataWidths) |
|
110
|
|
|
{ |
|
111
|
|
|
$colkeys = $dataWidths->keys(); |
|
112
|
|
|
return $totalWidth / count($colkeys); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.