From 0117916da9fa3250a04bfa192e10cb73c6b3579d Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Mon, 17 Jun 2024 15:18:09 +0200 Subject: [PATCH] CLI * make tables more legible --- includes/utilities.php | 39 ++++++++++++++++++++------ setup/tools/clisetup/dbconfig.func.php | 6 ++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index b5a6571d..5dc5b4e4 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -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 = ''; diff --git a/setup/tools/clisetup/dbconfig.func.php b/setup/tools/clisetup/dbconfig.func.php index 7ff15a16..7de5fd54 100644 --- a/setup/tools/clisetup/dbconfig.func.php +++ b/setup/tools/clisetup/dbconfig.func.php @@ -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) {