Skip to content

Commit

Permalink
starting work on help generation and table class
Browse files Browse the repository at this point in the history
  • Loading branch information
dvnc0 committed Aug 22, 2023
1 parent 367913f commit 739e93f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
19 changes: 19 additions & 0 deletions src/App/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ function($params) use ($version, $title) {
$this->command($command);
}

protected function buildMainHelpCommand(): void {
$command_help = $this->Help->buildHelpOutPut($this->Application_Object);

$command = Command::create('help')
->about('Prints this help information')
->action(function () use ($command_help) {
printf($command_help);
return;
})
->save();

$this->command($command);

}

/**
* Before run completes this should be done
*
Expand All @@ -230,6 +245,10 @@ function($params) use ($version, $title) {
protected function before(): void {
$this->argv = $_SERVER['argv'];
$this->buildVersionCommand();

if(empty($this->Application_Object->commands['help'])) {
$this->buildMainHelpCommand();
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/App/Request/Request_Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Clyde\Request;

/**
* @phpstan-type RRes array{'body'?: mixed, 'error'?: mixed}
* @template RRes of array{'body'?: mixed, 'error'?: mixed}
*/
class Request_Response
{
Expand Down Expand Up @@ -30,9 +30,9 @@ class Request_Response
/**
* construct
*
* @param boolean $success successful
* @param string $message message to attach
* @param array<RRes> $data any additional data
* @param boolean $success successful
* @param string $message message to attach
* @param RRes $data any additional data
*/
public function __construct(bool $success, string $message = '', array $data = []) {
$this->success = $success;
Expand Down
7 changes: 4 additions & 3 deletions src/App/Templates/help.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#application_name#
-----------------------------------------------
#version#
#author#
#website#
#version# #author# #website#

#about#

#commands#

14 changes: 14 additions & 0 deletions src/App/Tools/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Clyde\Tools;

use Clyde\Objects\Application_Object;
use Clyde\Tools\Table;
use Exception;

class Help
Expand Down Expand Up @@ -53,6 +54,19 @@ public function buildHelpOutPut(Application_Object $Application_Object): string

$file = preg_replace($this->help_lexemes, $help_data, $file_contents);

$this->Table = new Table;

Check failure on line 57 in src/App/Tools/Help.php

View workflow job for this annotation

GitHub Actions / build

Access to an undefined property Clyde\Tools\Help::$Table.
$rows = [];
foreach ($Application_Object->commands as $command) {
$rows[] = [$command->command_name, $command->about];
}

$help_info = $this->Table->printTable([

Check failure on line 63 in src/App/Tools/Help.php

View workflow job for this annotation

GitHub Actions / build

Parameter #1 $table of method Clyde\Tools\Table::printTable() expects array{array<array<string>>, array<array<string>>}, array{headers: array{'Command', 'Description'}, rows: array<int<0, max>, array{mixed, mixed}>} given.
'headers' => ['Command', 'Description'],
'rows' => $rows
]);

$file = preg_replace('%\#commands\#%', $help_info, $file);

return $file;
}
}
58 changes: 53 additions & 5 deletions src/App/Tools/Table.php
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) {

Check failure on line 21 in src/App/Tools/Table.php

View workflow job for this annotation

GitHub Actions / build

Offset 'headers' does not exist on array{array<array<string>>, array<array<string>>}.
$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;
}
}

0 comments on commit 739e93f

Please sign in to comment.