mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
CLI
* make tables more legible
This commit is contained in:
@@ -155,7 +155,7 @@ abstract class CLI
|
|||||||
/* formatted output */
|
/* formatted output */
|
||||||
/********************/
|
/********************/
|
||||||
|
|
||||||
public static function writeTable(array $out, bool $timestamp = false) : void
|
public static function writeTable(array $out, bool $timestamp = false, bool $headless = false) : void
|
||||||
{
|
{
|
||||||
if (!$out)
|
if (!$out)
|
||||||
return;
|
return;
|
||||||
@@ -173,19 +173,24 @@ abstract class CLI
|
|||||||
|
|
||||||
$nCols = max($nCols, count($row));
|
$nCols = max($nCols, count($row));
|
||||||
|
|
||||||
for ($j = 0; $j < $nCols - 1; $j++) // don't pad last column
|
for ($j = 0; $j < $nCols; $j++)
|
||||||
$pads[$j] = max($pads[$j] ?? 0, mb_strlen($row[$j] ?? ''));
|
$pads[$j] = max($pads[$j] ?? 0, mb_strlen(self::purgeEscapes($row[$j] ?? '')));
|
||||||
}
|
}
|
||||||
self::write();
|
|
||||||
|
|
||||||
foreach ($out as $row)
|
foreach ($out as $j => $row)
|
||||||
{
|
{
|
||||||
for ($i = 0; $i < $nCols - 1; $i++) // don't pad last column
|
for ($i = 0; $i < $nCols; $i++)
|
||||||
$row[$i] = str_pad($row[$i] ?? '', $pads[$i] + 2);
|
$row[$i] = str_pad($row[$i] ?? '', $pads[$i] ?? 0);
|
||||||
|
|
||||||
self::write(' '.implode($row), -1, $timestamp);
|
if ($j || $headless)
|
||||||
|
self::write(' '.implode(' ' . self::tblDelim(' ') . ' ', $row), -1, $timestamp);
|
||||||
|
else
|
||||||
|
self::write(self::tblHead(' '.implode(' ', $row)), -1, $timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$headless)
|
||||||
|
self::write(self::tblHead(str_pad('', array_sum($pads) + (count($pads) - 1) * 3)), -1, $timestamp);
|
||||||
|
|
||||||
self::write();
|
self::write();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +220,16 @@ abstract class CLI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function tblHead(string $str) : string
|
||||||
|
{
|
||||||
|
return CLI_HAS_E ? "\e[1;39;100m".$str."\e[0m" : $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function tblDelim(string $str) : string
|
||||||
|
{
|
||||||
|
return CLI_HAS_E ? "\e[39;100m".$str."\e[0m" : $str;
|
||||||
|
}
|
||||||
|
|
||||||
public static function grey(string $str) : string
|
public static function grey(string $str) : string
|
||||||
{
|
{
|
||||||
return CLI_HAS_E ? "\e[90m".$str."\e[0m" : $str;
|
return CLI_HAS_E ? "\e[90m".$str."\e[0m" : $str;
|
||||||
@@ -275,17 +290,23 @@ abstract class CLI
|
|||||||
$msg .= $txt;
|
$msg .= $txt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://shiroyasha.svbtle.com/escape-sequences-a-quick-guide-1#movement_1
|
||||||
$msg = (self::$overwriteLast && CLI_HAS_E ? "\e[1G\e[0K" : "\n") . $msg;
|
$msg = (self::$overwriteLast && CLI_HAS_E ? "\e[1G\e[0K" : "\n") . $msg;
|
||||||
self::$overwriteLast = $tmpRow;
|
self::$overwriteLast = $tmpRow;
|
||||||
|
|
||||||
echo $msg;
|
echo $msg;
|
||||||
|
|
||||||
if (self::$logHandle) // remove control sequences from log
|
if (self::$logHandle) // remove control sequences from log
|
||||||
fwrite(self::$logHandle, preg_replace(["/\e\[\d+[mK]/", "/\e\[\d+G/"], ['', "\n"], $msg));
|
fwrite(self::$logHandle, self::purgeEscapes($msg));
|
||||||
|
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function purgeEscapes(string $msg) : string
|
||||||
|
{
|
||||||
|
return preg_replace(["/\e\[\d+[mK]/", "/\e\[\d+G/"], ['', "\n"], $msg);
|
||||||
|
}
|
||||||
|
|
||||||
public static function nicePath(string $fileOrPath, string ...$pathParts) : string
|
public static function nicePath(string $fileOrPath, string ...$pathParts) : string
|
||||||
{
|
{
|
||||||
$path = '';
|
$path = '';
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ function dbconfig() : void
|
|||||||
|
|
||||||
$buff[] = $result;
|
$buff[] = $result;
|
||||||
$buff[] = 'mysqli://'.$dbInfo['user'].':'.($dbInfo['pass'] ? '**********' : '').'@'.$dbInfo['host'].'/'.$dbInfo['db'];
|
$buff[] = 'mysqli://'.$dbInfo['user'].':'.($dbInfo['pass'] ? '**********' : '').'@'.$dbInfo['host'].'/'.$dbInfo['db'];
|
||||||
$buff[] = $dbInfo['prefix'] ? 'table prefix: '.$dbInfo['prefix'] : '';
|
$buff[] = $dbInfo['prefix'] ? 'pre.: '.$dbInfo['prefix'] : '';
|
||||||
$buff[] = $note;
|
$buff[] = $note;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -109,7 +109,7 @@ function dbconfig() : void
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
CLI::write('select an index to use the corresponding entry');
|
CLI::write('select an index to use the corresponding entry', -1, false);
|
||||||
|
|
||||||
$nCharDBs = 0;
|
$nCharDBs = 0;
|
||||||
$tblRows = [];
|
$tblRows = [];
|
||||||
@@ -124,7 +124,7 @@ function dbconfig() : void
|
|||||||
|
|
||||||
$tblRows[] = ['['.CLI::bold('N').']', 'new characters DB'];
|
$tblRows[] = ['['.CLI::bold('N').']', 'new characters DB'];
|
||||||
$tblRows[] = ['['.CLI::bold('R').']', 'retest / reload DBs'];
|
$tblRows[] = ['['.CLI::bold('R').']', 'retest / reload DBs'];
|
||||||
CLI::writeTable($tblRows, true);
|
CLI::writeTable($tblRows, false, true);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user