From 2e255e2602895429195388dc108cd8c7da4e055e Mon Sep 17 00:00:00 2001 From: jorgecc Date: Fri, 20 Sep 2024 17:28:08 -0300 Subject: [PATCH] 1.34 --- README.md | 3 +++ src/CliOne.php | 70 +++++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a82a4b4..0a9ebe4 100644 --- a/README.md +++ b/README.md @@ -652,6 +652,9 @@ CliOne::testUserInput([...]); // put your story here. * The screen size width is -1 column less in older version of Windows. C'mon, Microsoft! ## Changelog +* 1.34 (2024-09-20) + * fixed a couple of bugs in showTable(). + * fixed comments in the code * 1.33 (2024-08-02) * json_encode does not throw an exception. Now it fails with empty or default value. * 1.32.1 (2024-03-02) diff --git a/src/CliOne.php b/src/CliOne.php index 858f821..09a1225 100644 --- a/src/CliOne.php +++ b/src/CliOne.php @@ -1,4 +1,5 @@ - * @copyright Copyright (c) 2022 Jorge Patricio Castro Castillo. Dual Licence: MIT License and Commercial. * Don't delete this comment, its part of the license. - * @version 1.33 + * @version 1.34 * @link https://github.com/EFTEC/CliOne */ class CliOne { - public const VERSION = '1.33'; + public const VERSION = '1.34'; /** * @var bool if debug is true then:
* 1) every operation will be recorded in $this->debugHistory
@@ -264,7 +265,7 @@ public function clearMenu(?string $idMenu = null): CliOne /** * It adds a new menu that could be called by evalMenu()
* Example:
- * ```php + * ``` * //"fnheader" call to $this->menuHeader(CliOne $cli); * $this->addMenu('idmenu','fnheader',null,'What do you want to do?','option3'); * // you can use a callable argument, the first argument is of type CliOne. @@ -299,7 +300,7 @@ public function addMenu(string $idMenu, /** * It adds a menu item.
* Example:
- * ```php + * ``` * $this->addMenu('menu1'); * // if op1 is selected then it calls method menufnop1(), the prefix is for protection. * $this->addMenuItem('menu1','op1','option #1','fnop1'); @@ -338,7 +339,7 @@ public function addMenuItem(string $idMenu, string $indexMenuItem, string $descr /** * It adds multiples items to a menu
* Example:
- * ```php + * ``` * $this->addMenu('menu1'); * $this->addMenuItems('menu1',[ * 'op1'=>['operation #1','action1'], // with description & action @@ -371,7 +372,7 @@ public function addMenuItems(string $idMenu, ?array $items): CliOne * then it is only called by the first object.
* If evalMenu() uses a service then, the services defined here are ignored.
* Example:
- * ```php + * ``` * $objService=new Class1(); * $this->addMenuService('menu1',$objService); * // or: @@ -394,7 +395,7 @@ public function addMenuService(string $idMenu, $service): CliOne /** * Eval (executes) a menu previously defined.
* Example:
- * ```php + * ``` * $this->addMenu('menu1'); * // pending: add items to the menu * $this->evalMenu('menu1',$myService); @@ -582,7 +583,7 @@ public function getVariable(string $variableName, $valueIfNotFound = null) /** * It adds a callback function.
* Example:
- * ```php + * ``` * $t->addVariableCallBack('call1', function(CliOne $cli) { * $cli->setVariable('v2', 'world',false); // the false is important if you don't want recursivity * }); @@ -781,7 +782,7 @@ protected static function removeTrailSlash(string $txt): string /** * It creates a new parameter to be read from the command line and/or to be input manually by the user
* Example:
- * ```php + * ``` * $this->createParam('k1','first'); // php program.php thissubcommand * $this->createParam('k1','flag',['flag2','flag3']); // php program.php -k1 or --flag2 or --flag3 * @@ -851,7 +852,7 @@ public function downLevel(int $number = 1): CliOne * It evaluates the parameters obtained from the syntax of the command.
* The parameters must be defined before call this method
* Example:
- * ```php + * ``` * // shell: * php mycode.php -argument1 hello -argument2 world * @@ -1078,7 +1079,7 @@ public function getParameter(string $key): CliOneParam /** * It reads a value of a parameter. * Example: - * ```php + * ``` * // [1] option1 * // [2] option2 * // select a value [] 2 @@ -3507,7 +3508,7 @@ protected function fontAtr(string $letter, ?string $bit1 = null, string $bit2 = /** * It reads the value-key of a parameter selected. It is useful for a list of elements.
* Example:
- * ```php + * ``` * // [1] option1 * // [2] option2 * // select a value [] 2 @@ -3530,7 +3531,7 @@ public function getValueKey(string $key) * It will return true if the PHP is running on CLI
* If the constructor specified a file, then it is also used for validation. * Example:
- * ```php + * ``` * // page.php: * $inst=new CliOne('page.php'); // this security avoid calling the cli when this file is called by others. * if($inst->isCli()) { @@ -3758,7 +3759,7 @@ protected function varexport($expression): ?string /** * It saves the information into a file. The content will be converted into a PHP file.
* example:
- * ```php + * ``` * $this->saveDataPHPFormat('file',[1,2,3]); // it will save a file with the next content: $config=[1,2,3]; * ``` * @param string $filename the filename (without extension) to where the value will be saved. @@ -3790,7 +3791,7 @@ public function saveDataPHPFormat(string $filename, $content, string $defaultExt /** * It sets the alignment. This method is stackable.
* Example:
- * ```php + * ``` * $cli->setAlign('left','left','right')->setStyle('double')->showTable($values); * ```> * @param string $title =['left','right','middle'][$i] the alignment of the title @@ -4068,7 +4069,7 @@ public function show(string $content, ?string $stream = null): CliOne * To add values you could use the method uplevel()
* To remove a value (going down a level) you could use the method downlevel()
* You can also change the style using setPattern1(),setPattern2(),setPattern3()
- * ```php + * ``` * $cli->setPattern1('{value}{type}') // the level * ->setPattern2('{value}{type}') // the current level * ->setPattern3(' -> ') // the separator @@ -4184,8 +4185,8 @@ public function showFrame($lines, $titles = null): void /** * It shows (echo) a colored line. The syntax of the color is similar to html as follows:
- * ```php - * <red>error</red> (color red) + * ``` + * error</red> (color red) * <yellow>warning</yellow> (color yellow) * <blue>information</blue> (blue) * <yellow>yellow</yellow> (yellow) @@ -4691,11 +4692,21 @@ public function showTable(array $assocArray, $contentwCorrected = $contentw - count($columns) + 1; $totalCol = array_sum($maxColumnSize); foreach ($columns as $column) { - $maxColumnSize[$column] = (int)round($maxColumnSize[$column] * $contentwCorrected / $totalCol); + $maxColumnSize[$column] = (int)round(($maxColumnSize[$column] * $contentwCorrected / $totalCol)-0.4); if ($maxColumnSize[$column] <= 2) { $maxColumnSize[$column] = 3; } } + if (array_sum($maxColumnSize) < $contentwCorrected) { + $dif=$contentwCorrected-array_sum($maxColumnSize); + foreach ($columns as $column) { + $maxColumnSize[$column]++; + $dif--; + if($dif<=0) { + break; + } + } + } if (array_sum($maxColumnSize) > $contentwCorrected) { // we correct the precision error of round by removing 1 to the first column that is bigger than 3 foreach ($columns as $column) { @@ -4740,17 +4751,19 @@ public function showTable(array $assocArray, $rowSwift = $pageSize * ($curpage - 1); $totalPage = ceil(count($assocArray) / $pageSize); // content + $numRow=-1; foreach ($assocArray as $k => $line) { $txt = $ml; + $numRow++; $lineDisplay = @$assocArray[$k + $rowSwift]; if ($lineDisplay) { foreach ($maxColumnSize as $colName => $size) { - if ($k > $this->rowSize - $curRow - $reduceRows - 3) { + if ($numRow > $this->rowSize - $curRow - $reduceRows - 3) { $lineDisplay[$colName] = '...'; } $lineDisplay[$colName] = $lineDisplay[$colName] ?? '(null)'; $txt .= $this->alignText( - $lineDisplay[$colName], + is_string($lineDisplay[$colName])?$lineDisplay[$colName]:json_encode($lineDisplay[$colName]), $size, is_numeric($lineDisplay[$colName]) ? $alignContentNumber : $alignContentText) . $mmv; } @@ -4799,7 +4812,7 @@ public function showValuesColumn(array $values, string $type, ?string $patternCo /** * It shows a waiting cursor.
* Example:
- * ```php + * ``` * $this->hideCursor()->showWaitCursor(true); * $this->showWaitCursor(); // inside a loop. * $this->hideWaitCursor()->showCursor(); // at the end of the loop @@ -4933,6 +4946,9 @@ public function showParamValue(CliOneParam $parameter): string */ public function strlen($content, bool $visual = true) { + if(!is_string($content)) { + $content=json_encode($content); + } $contentClear = $this->colorLess($content); if ($this->multibyte && $visual) { /** @noinspection PhpComposerExtensionStubsInspection */ @@ -5026,7 +5042,7 @@ protected function refreshParamValueKey(CliOneParam $parameter): void // /** - * ```php + * ``` * // up left, up middle, up right, middle left, middle right, down left, down middle, down right. * [$ul, $um, $ur, $ml, $mm, $mr, $dl, $dm, $dr, $mmv]=$this->border(); * ``` @@ -5102,7 +5118,7 @@ protected function border(string $style): array } /** - * ```php + * ``` * // cut left, cut top, cut right, cut bottom , cut middle * [$cutl, $cutt, $cutr, $cutd, $cutm] = $this->borderCut($style); * ``` @@ -5507,7 +5523,7 @@ protected function readline(string $content, CliOneParam $parameter) /** * It sets the color of the cli
- * ```php + * ``` * error (color red) * warning (color yellow) * information (blue) @@ -5605,7 +5621,7 @@ public function initialEndStyle(string $contentAnsi, ?string &$initial, ?string /** * [full,light,soft,double], light usually it is a space. - * ```php + * ``` * [$bf,$bl,$bm,$bd]=$this->shadow(); * ``` * @param string $style =['mysql','simple','double','style']