-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starting work on help generation and table class
- Loading branch information
Showing
5 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#application_name# | ||
----------------------------------------------- | ||
#version# | ||
#author# | ||
#website# | ||
#version# #author# #website# | ||
|
||
#about# | ||
|
||
#commands# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
<?php | ||
namespace Clyde\Tools; | ||
|
||
/** | ||
* @phpstan-type THColumn array<string> | ||
* @phpstan-type TableHeader array<THColumn> | ||
* @phpstan-type TableColumn array<string> | ||
* @phpstan-type TableRow array<TableColumn> | ||
*/ | ||
class Table | ||
{ | ||
/** | ||
* Eventually this will have a print table helper method | ||
* | ||
* @param array $table the table data | ||
* @return void | ||
* @param array{0:TableHeader, 1: TableRow} $table the table data | ||
* @return string | ||
*/ | ||
public function printTable(array $table): void { | ||
// table should be an array of rows/columns | ||
// str_pad to get longest in column | ||
public function printTable(array $table): string { | ||
$column_lengths = []; | ||
|
||
foreach ($table['headers'] as $row_key => $value) { | ||
$length = strlen($value) + 2; | ||
if (empty($column_lengths[$row_key]) || $column_lengths[$row_key] < $length) { | ||
$column_lengths[$row_key] = $length; | ||
} | ||
} | ||
foreach ($table['rows'] as $key => $row){ | ||
foreach($row as $row_key => $value) { | ||
$length = strlen($value) + 2; | ||
if (empty($column_lengths[$row_key]) || $column_lengths[$row_key] < $length) { | ||
$column_lengths[$row_key] = $length; | ||
} | ||
} | ||
} | ||
$output = ''; | ||
foreach($table['headers'] as $row_key => $value) { | ||
$value = str_pad($value, $column_lengths[$row_key], ' '); | ||
$output .= '|' . $value; | ||
if (array_key_last($table['headers']) === $row_key) { | ||
$output .= '|'; | ||
} | ||
} | ||
$output .= PHP_EOL; | ||
foreach ($column_lengths as $key => $length) { | ||
$output_row = '|'; | ||
$row_cur = str_pad($output_row, (int)$length + 1, '-'); | ||
$output .= $row_cur; | ||
} | ||
$output .= '|'; | ||
$output .= PHP_EOL; | ||
foreach($table['rows'] as $key => $row) { | ||
foreach ($row as $row_key => $value) { | ||
$value = str_pad($value, $column_lengths[$row_key], ' '); | ||
$output .= '|' . $value; | ||
if (array_key_last($row) === $row_key) { | ||
$output .= '|'; | ||
} | ||
} | ||
$output .= PHP_EOL; | ||
} | ||
|
||
return $output; | ||
} | ||
} |