/
ShellTablePrinter.php
284 lines (255 loc) · 7.82 KB
/
ShellTablePrinter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/*
* __ __ _ _ _ _
* | \/ |(_) ___ | |__ ___ | || |
* | |\/| || |/ __|| '_ \ / _ \| || |
* | | | || |\__ \| | | || __/| || |
* |_| |_||_||___/|_| |_| \___||_||_|
*
* This file is part of Kristuff\Mishell.
* (c) Kristuff <contact@kristuff.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 1.1.0
* @copyright 2017-2020 Kristuff
*/
namespace Kristuff\Mishell;
abstract class ShellTablePrinter extends \Kristuff\Mishell\ShellColoredPrinter
{
/**
* Align left constant
*
* @const int
*/
const ALIGN_LEFT = STR_PAD_RIGHT;
/**
* Align right constant
*
* @const int
*/
const ALIGN_RIGHT = STR_PAD_LEFT;
/**
* Align center constant
*
* @const int
*/
const ALIGN_CENTER = STR_PAD_BOTH;
/**
* The default horizontal sign.
*
* @access protected
* @var string
*/
protected static $defaultHorizontalSign = '-';
/**
* The default vertical sign.
*
* @access protected
* @var string
*/
protected static $defaultVerticalSign = '|';
/**
* The default vertical sign.
*
* @access protected
* @var string
*/
protected static $defaultVerticalInnerSign = '+';
/**
* The default table cell padding.
*
* @access protected
* @var string
*/
protected static $defaultCellPadding = ' ';
/**
* The cell padding.
*
* @access public
* @var string
*/
public static $tableCellPadding = ' ';
/**
* The horizontal separator sign.
*
* @access public
* @var string
*/
public static $horizontalSeparator = '-';
/**
* The vertical separator sign.
*
* @access public
* @var string
*/
public static $verticalSeparator = '|';
/**
* The vertical header separator sign.
*
* @access public
* @var string
*/
public static $verticalHeaderSeparator = '+';
/**
* The vertical separator sign.
*
* @access public
* @var string
*/
public static $verticalInnerSeparator = '+';
/**
* Resets the default options
*
* @access public
* @static method
*
* @return void
*/
public static function resetDefaults()
{
self::$verticalSeparator = self::$defaultVerticalSign;
self::$horizontalSeparator = self::$defaultHorizontalSign;
self::$tableCellPadding = self::$defaultCellPadding;
self::$verticalInnerSeparator = self::$defaultVerticalInnerSign;
self::$tableCellPadding = self::$defaultCellPadding;
}
/**
* Gets a formated table row separator
*
* @access public
* @static method
* @param string [$color] The text color for the wall row
* @param string [$bgcolor] The back color for the wall row
* @param string [$option]+... The text styles for the wall row
*
* @return string
*/
public static function tableRowSeparator()
{
$args = func_get_args();
$columnsPads = !empty($args) ? $args[0] : [];
array_shift($args);
$str = self::$verticalSeparator ;
$cellPaddingLenght = mb_strlen(self::$tableCellPadding) *2;
foreach ($columnsPads as $key => $pad){
$str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$verticalInnerSeparator;
}
return self::getCliString(self::$verticalInnerSeparator === '' ? $str : substr($str, 0, mb_strlen($str) -1). self::$verticalSeparator, $args);
}
/**
* Gets a formated table row separator
*
* @access public
* @static method
* @param string [$color] The text color for the wall row
* @param string [$bgcolor] The back color for the wall row
* @param string [$option]+... The text styles for the wall row
*
* @return string
*/
public static function tableSeparator()
{
$args = func_get_args();
$columnsPads = !empty($args) ? $args[0] : [];
array_shift($args);
$str = self::$verticalHeaderSeparator ;
$cellPaddingLenght = mb_strlen(self::$tableCellPadding) *2;
foreach ($columnsPads as $key => $pad){
$str .= str_repeat(self::$horizontalSeparator , $pad + $cellPaddingLenght) .self::$verticalInnerSeparator ;
}
return self::getCliString(self::$verticalInnerSeparator === '' ? $str : substr($str, 0, mb_strlen($str) -1). self::$verticalHeaderSeparator, $args);
}
/**
* Gets a formated table blank row
*
* @access public
* @static method
* @param string [$color] The text color for the wall row
* @param string [$bgcolor] The back color for the wall row
* @param string [$option]+... The text styles for the wall row
*
* @return string
*/
public static function tableRowEmpty()
{
// get and parse arguments:
// - extract first argument (columns list)
// - keep following arguments (if exist) as styles
$args = func_get_args();
$columnsPads = !empty($args) ? $args[0] : [];
array_shift($args);
$str = self::$verticalSeparator ;
foreach ($columnsPads as $pad){
$str .= self::$tableCellPadding.
str_pad(' ', $pad).
self::$tableCellPadding.
self::$verticalSeparator ;
}
return self::getCliString($str, $args);
}
/**
* Return a table row start.
*
* @access public
* @static method
*
* @return void
*/
public static function tableRowStart()
{
return self::$verticalSeparator;
}
/**
* Return a table cell string.
*
* @access public
* @static method
* @param string $column The column text.
* @param int $lenght The column length. Default is 0 (no pad)
* @param int $align The column alignement (Console::ALIGN_LEFT, Console::ALIGN_RIGHT or Console::ALIGN_CENTER). Default is Console::ALIGN_LEFT.
*
* @return string
*/
public static function tableRowCell($column, $length = 0, $align = self::ALIGN_LEFT)
{
return self::$tableCellPadding.
self::pad($column, $length, ' ', $align).
self::$tableCellPadding.
self::$verticalSeparator;
}
/**
* Return a table row string.
*
* @access public
* @static method
* @param array $columns The columns arrays.
* @param string [$color] The text color for the wall row
* @param string [$bgcolor] The back color for the wall row
* @param string [$option]+... The text styles for the wall row
*
* @return string
*/
public static function tableRow()
{
// get and parse arguments:
// - extract first argument (columns list)
// - keep following arguments (if exist) as styles
$args = func_get_args();
$columns = !empty($args) ? $args[0] : [];
array_shift($args);
// build the row string
// start with separator
$str = self::$verticalSeparator;
// add columns
foreach ($columns as $column => $pad){
$str .= self::$tableCellPadding.
self::pad($column, $pad).
self::$tableCellPadding.
self::$verticalSeparator;
}
// format full row
return self::getCliString($str, $args) ;
}
}