* make tables more legible
This commit is contained in:
Sarjuuk
2024-06-17 15:18:09 +02:00
parent c2bbfe17a6
commit 0117916da9
2 changed files with 33 additions and 12 deletions

View File

@@ -155,7 +155,7 @@ abstract class CLI
/* 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)
return;
@@ -173,19 +173,24 @@ abstract class CLI
$nCols = max($nCols, count($row));
for ($j = 0; $j < $nCols - 1; $j++) // don't pad last column
$pads[$j] = max($pads[$j] ?? 0, mb_strlen($row[$j] ?? ''));
for ($j = 0; $j < $nCols; $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
$row[$i] = str_pad($row[$i] ?? '', $pads[$i] + 2);
for ($i = 0; $i < $nCols; $i++)
$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();
}
@@ -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
{
return CLI_HAS_E ? "\e[90m".$str."\e[0m" : $str;
@@ -275,17 +290,23 @@ abstract class CLI
$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;
self::$overwriteLast = $tmpRow;
echo $msg;
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();
}
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
{
$path = '';

View File

@@ -90,7 +90,7 @@ function dbconfig() : void
$buff[] = $result;
$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;
}
@@ -109,7 +109,7 @@ function dbconfig() : void
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;
$tblRows = [];
@@ -124,7 +124,7 @@ function dbconfig() : void
$tblRows[] = ['['.CLI::bold('N').']', 'new characters DB'];
$tblRows[] = ['['.CLI::bold('R').']', 'retest / reload DBs'];
CLI::writeTable($tblRows, true);
CLI::writeTable($tblRows, false, true);
while (true)
{