Skip to content

Commit

Permalink
improving output command with Termwind
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Jul 16, 2022
1 parent 9da64aa commit 58245b6
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.0','7.4','8.1' ]
php-versions: [ '8.0','8.1' ]
dependency-stability: [ 'prefer-none' ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
run: composer run-script test

- name: PHP Syntax Checker (Lint)
uses: StephaneBour/actions-php-lint@7.2
uses: StephaneBour/actions-php-lint@8.0
with:
dir: './src'
2 changes: 1 addition & 1 deletion .github/workflows/test-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.1', '8.0','7.4' ]
php-versions: [ '8.1', '8.0' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.1', '8.0','7.4','7.3' ]
php-versions: [ '8.1', '8.0' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
}
],
"require": {
"php": "^7.2|^8.0",
"php": "^7.4|^8.0|^8.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0|^7.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"ext-json": "*"
"nunomaduro/termwind": "^1.13"
},
"require-dev": {
"nunomaduro/larastan": "^0.7.2",
"nunomaduro/larastan": "^0.7.2|^1.0.0",
"orchestra/testbench": "4.*|5.*|6.*|7.*",
"phpunit/phpunit": "^8.0|^9.3",
"psalm/plugin-laravel": "^1.5",
Expand Down
78 changes: 78 additions & 0 deletions resources/views/laralens/term/checks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<div class="mt-1 mx-1">
@if (sizeof($rows) > 0)
<div class="flex space-x-1">
<span class="flex-1 content-repeat-[─] text-red">
</span>
<span class="text-red">
CHECK: issues found
</span>
</div>
@else
<div class="flex space-x-1">
<span class="flex-1 content-repeat-[─] text-green">
</span>
<span class="text-green">
CHECK: everything looks good
</span>
</div>
@endif
<div>
<span>
@foreach ($rows as $row)
@php
$lineType = Arr::get($row, "lineType", HiFolks\LaraLens\ResultLens::LINE_TYPE_DEFAULT);
$label = Arr::get($row, "label", "");
@endphp

@if ($label === "*** HINT")
<div class="flex bg-gray-900 space-x-1">
<span class="px-1 text-gray-100">
💡 Hint:
</span>

<span>
<span class="px-0 text-gray-300">
{{ Arr::get($row, "value", "") }}
</span>
</span>
</div>
@else
<div class="">
</div>
<div class="flex bg-gray-900 space-x-0 ">
<span class=" text-gray-800 px-1 bg-gray-200 font-bold">
@if ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR)
@elseif ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING)
⚠️
@else
✔️
@endif
&nbsp;{{ Arr::get($row, "label", "")}}
</span>

<span class="flex-1 content-repeat-[.] text-gray"></span>

<span>
@if ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR)
<span class="px-0 bg-red text-white font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@elseif ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING)
<span class="px-0 bg-yellow text-gray font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@else

<span class="px-0 bg-green text-gray font-bold">
{{ Str::replace("\\", "/", Arr::get($row, "value", "")) }}
</span>
@endif

</span>
</div>
@endif
@endforeach
</span>
</div>
</div>
62 changes: 62 additions & 0 deletions resources/views/laralens/term/table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div class="mt-1 mx-1">
<div class="flex space-x-1">
<span class="flex-1 content-repeat-[─] text-gray">

</span>

<span class="text-green">
{{ $title }}
</span>
</div>
<div>
<span>
@foreach ($rows as $row)
@php
$lineType = Arr::get($row, "lineType", HiFolks\LaraLens\ResultLens::LINE_TYPE_DEFAULT);
$label = Arr::get($row, "label", "");
@endphp
@if ($label === "*** HINT")
<div class="flex bg-gray-900 space-x-1">
<span class=" text-white font-bold">
Hint:
</span>
<span class="flex-1 content-repeat-[.] text-gray"></span>
<span>
<span class="px-2 text-gray font-bold">
{{ Arr::get($row, "value", "") }}
</span>
</span>
</div>
@else
<div class="flex bg-gray-900 space-x-1">
<span class=" text-white font-bold">
{{ Arr::get($row, "label", "")}}
</span>

<span class="flex-1 content-repeat-[.] text-gray"></span>

<span>
@if ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_ERROR)
<span class="text-gray-100 bg-red font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@elseif ($lineType === HiFolks\LaraLens\ResultLens::LINE_TYPE_WARNING)
<span class="text-gray-100 bg-yellow font-bold">
{{ Arr::get($row, "value", "")}}
</span>
@else

<span class="text-gray-100 ">
{{ Str::replace("\\", "/", Arr::get($row, "value", "")) }}
</span>
@endif

</span>
</div>
@endif
@endforeach
</span>
</div>

</div>
133 changes: 14 additions & 119 deletions src/Console/LaraLensCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace HiFolks\LaraLens\Console;

use HiFolks\LaraLens\Lens\LaraLens;
use HiFolks\LaraLens\Lens\Traits\TermOutput;
use HiFolks\LaraLens\ResultLens;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class LaraLensCommand extends Command
{
use TermOutput;

private const TABLE_STYLES = 'default|borderless|compact|symfony-style-guide|box|box-double';
private const DEFAULT_STYLE = 'box-double';
private const DEFAULT_PATH = '';
Expand Down Expand Up @@ -54,138 +57,30 @@ private function allConfigs(): void
$this->info(json_encode(config()->all(), JSON_PRETTY_PRINT));
}

private function formatCell($string, $width)
{
$retVal = "";
if (strlen($string) > $width) {
$retVal = Str::limit($string, $width, '');
} elseif (strlen($string) < $width) {
$retVal = str_pad($string, $width);
} else {
$retVal = $string;
}
return $retVal;
}

private function printOutput(array $headers, array $rows): void
{
$rowsTable = [];
$rowsLine = [];
foreach ($rows as $key => $row) {
$label = Arr::get($row, "label", "");
$value = Arr::get($row, "value", "");
if (is_array($value)) {
$value = implode(",", $value);
}
$isLine = Arr::get($row, "isLine", false);
$lineType = Arr::get($row, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if (
strlen($value) > $this->widthValue ||
$isLine ||
$lineType === ResultLens::LINE_TYPE_ERROR ||
$lineType === ResultLens::LINE_TYPE_WARNING
) {
$rowsLine[] = $row;
} else {
$row["label"] = $this->formatCell($label, $this->widthLabel);
$row["value"] = $this->formatCell($value, $this->widthValue);
$rowsTable[] = [ $row["label"], $row["value"] ];
}
}
/*
* table style:
* 'default'
* 'borderless'
* 'compact'
* 'symfony-style-guide'
* 'box'
* 'box-double'
*/
$this->table($headers, $rowsTable, $this->styleTable);
foreach ($rowsLine as $key => $line) {
$label = Arr::get($line, "label", "");
$value = Arr::get($line, "value", "");
$lineType = Arr::get($line, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if ($label != "") {
$this->info($label . ":");
}
if ($lineType === ResultLens::LINE_TYPE_ERROR) {
$this->error($value);
} elseif ($lineType === ResultLens::LINE_TYPE_WARNING) {
$this->warn($value);
} else {
$this->line($value);
}
}
}

private function alertGreen($string): void
{
$length = Str::length(strip_tags($string)) + 12;
$this->info(str_repeat('*', $length));
$this->info('* ' . $string . ' *');
$this->info(str_repeat('*', $length));
$this->output->newLine();
}

private function printChecks(array $rows): void
{
if (sizeof($rows) == 0) {
$this->alertGreen("CHECK: everything looks good");
} else {
$this->alert("CHECK: issues found");
}
$idx = 0;
foreach ($rows as $key => $row) {
$label = Arr::get($row, "label", "");
$value = Arr::get($row, "value", "");
$isLine = Arr::get($row, "isLine", false);
$lineType = Arr::get($row, "lineType", ResultLens::LINE_TYPE_DEFAULT);
if (
($label !== "") &&
(($lineType === ResultLens::LINE_TYPE_ERROR) ||
ResultLens::isMessageLine($lineType))
) {
$idx++;
$this->warn("--- " . $idx . " ------------------");
$this->warn("*** " . $label);
}
if ($lineType === ResultLens::LINE_TYPE_ERROR) {
$this->error($value);
} elseif ($lineType === ResultLens::LINE_TYPE_WARNING) {
$this->warn($value);
} elseif ($lineType === ResultLens::LINE_TYPE_INFO) {
$this->info($value);
} else {
$this->comment($value);
}
}
}

private function overview($checkTable = "users", $columnSorting = "created_at", $show = self::OPTION_SHOW_ALL): void
{
$ll = new LaraLens();
if ($show & self::OPTION_SHOW_CONFIGS) {
$output = $ll->getConfigs();
$this->printOutput(["Config keys via config()", "Values"], $output->toArray());
$this->printOutputTerm("Config keys via config()", $output->toArray());
}
if ($show & self::OPTION_SHOW_RUNTIMECONFIGS) {
$output = $ll->getRuntimeConfigs();
$this->printOutput(["Runtime Configs", "Values"], $output->toArray());
$this->printOutputTerm("Runtime Configs", $output->toArray());
$output = $ll->checkServerRequirements();
$this->printOutput(["Laravel Requirements", "Values"], $output->toArray());
$this->printOutputTerm("Laravel Requirements", $output->toArray());
}
if ($show & self::OPTION_SHOW_RUNTIMECONFIGS) {
$output = $ll->checkFiles();
$this->printOutput(["Check files", "Values"], $output->toArray());
$this->printOutputTerm("Check files", $output->toArray());
}
if ($show & self::OPTION_SHOW_CONNECTIONS) {
$output = $ll->getConnections($this->urlPath);
$this->printOutput(["Connections", "Values"], $output->toArray());
$this->printOutputTerm("Connections", $output->toArray());
}
if ($show & self::OPTION_SHOW_DATABASE) {
$output = $ll->getDatabase($checkTable, $columnSorting);
$this->printOutput(["Database", "Values"], $output->toArray());
$this->printOutputTerm("Database", $output->toArray());
}
if ($show & self::OPTION_SHOW_MIGRATION) {
try {
Expand All @@ -201,22 +96,22 @@ private function overview($checkTable = "users", $columnSorting = "created_at",
$e->getMessage(),
"Check the Database configuration"
);
$this->printOutput(["Migration" , "result"], $r->toArray());
$this->printOutputTerm("Migration", $r->toArray());
}
}
if ($show & self::OPTION_SHOW_PHPEXTENSIONS) {
$output = $ll->getPhpExtensions();
$this->printOutput(["PHP Extensions"], $output->toArray());
$this->printOutputTerm("PHP Extensions", $output->toArray());
}
if ($show & self::OPTION_SHOW_PHPINIVALUES) {
$output = $ll->getPhpIniValues();
$this->printOutput(["PHP ini config", "Values"], $output->toArray());
$this->printOutputTerm("PHP ini config", $output->toArray());
}
if ($show & self::OPTION_SHOW_OS) {
$output = $ll->getOsConfigs();
$this->printOutput(["Operating System", "Values"], $output->toArray());
$this->printOutputTerm("Operating System", $output->toArray());
}
$this->printChecks($ll->checksBag->toArray());
$this->printChecksTerm($ll->checksBag->toArray());
}


Expand Down
Loading

0 comments on commit 58245b6

Please sign in to comment.