Skip to content

Commit

Permalink
1.34
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Sep 20, 2024
1 parent 951b1c9 commit 2e255e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 43 additions & 27 deletions src/CliOne.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php /** @noinspection UnknownInspectionInspection */
<?php /** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection UnknownInspectionInspection */
/** @noinspection ForgottenDebugOutputInspection */
/** @noinspection DuplicatedCode */

Expand All @@ -17,12 +18,12 @@
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
* @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:<br>
* 1) every operation will be recorded in $this->debugHistory<br>
Expand Down Expand Up @@ -264,7 +265,7 @@ public function clearMenu(?string $idMenu = null): CliOne
/**
* It adds a new menu that could be called by evalMenu()<br/>
* <b>Example:</b><br/>
* ```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.
Expand Down Expand Up @@ -299,7 +300,7 @@ public function addMenu(string $idMenu,
/**
* It adds a menu item.<br/>
* <b>Example:</b><br/>
* ```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');
Expand Down Expand Up @@ -338,7 +339,7 @@ public function addMenuItem(string $idMenu, string $indexMenuItem, string $descr
/**
* It adds multiples items to a menu<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $this->addMenu('menu1');
* $this->addMenuItems('menu1',[
* 'op1'=>['operation #1','action1'], // with description & action
Expand Down Expand Up @@ -371,7 +372,7 @@ public function addMenuItems(string $idMenu, ?array $items): CliOne
* then it is only called by the first object.<br/>
* If evalMenu() uses a service then, the services defined here are ignored.<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $objService=new Class1();
* $this->addMenuService('menu1',$objService);
* // or:
Expand All @@ -394,7 +395,7 @@ public function addMenuService(string $idMenu, $service): CliOne
/**
* Eval (executes) a menu previously defined.<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $this->addMenu('menu1');
* // pending: add items to the menu
* $this->evalMenu('menu1',$myService);
Expand Down Expand Up @@ -582,7 +583,7 @@ public function getVariable(string $variableName, $valueIfNotFound = null)
/**
* It adds a callback function.<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $t->addVariableCallBack('call1', function(CliOne $cli) {
* $cli->setVariable('v2', 'world',false); // the false is important if you don't want recursivity
* });
Expand Down Expand Up @@ -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<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $this->createParam('k1','first'); // php program.php thissubcommand
* $this->createParam('k1','flag',['flag2','flag3']); // php program.php -k1 <val> or --flag2 <val> or --flag3
* <val>
Expand Down Expand Up @@ -851,7 +852,7 @@ public function downLevel(int $number = 1): CliOne
* It evaluates the parameters obtained from the syntax of the command.<br/>
* The parameters must be defined before call this method<br/>
* <b>Example:</b><br/>
* ```php
* ```
* // shell:
* php mycode.php -argument1 hello -argument2 world
*
Expand Down Expand Up @@ -1078,7 +1079,7 @@ public function getParameter(string $key): CliOneParam
/**
* It reads a value of a parameter.
* <b>Example:</b><bt>
* ```php
* ```
* // [1] option1
* // [2] option2
* // select a value [] 2
Expand Down Expand Up @@ -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.<br/>
* <b>Example:</b><br/>
* ```php
* ```
* // [1] option1
* // [2] option2
* // select a value [] 2
Expand All @@ -3530,7 +3531,7 @@ public function getValueKey(string $key)
* It will return true if the PHP is running on CLI<br/>
* If the constructor specified a file, then it is also used for validation.
* <b>Example:</b><br/>
* ```php
* ```
* // page.php:
* $inst=new CliOne('page.php'); // this security avoid calling the cli when this file is called by others.
* if($inst->isCli()) {
Expand Down Expand Up @@ -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.<br/>
* <b>example:</b><br/>
* ```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.
Expand Down Expand Up @@ -3790,7 +3791,7 @@ public function saveDataPHPFormat(string $filename, $content, string $defaultExt
/**
* It sets the alignment. This method is stackable.<br/>
* <b>Example:</b><br/>
* ```php
* ```
* $cli->setAlign('left','left','right')->setStyle('double')->showTable($values);
* ```>
* @param string $title =['left','right','middle'][$i] the alignment of the title
Expand Down Expand Up @@ -4068,7 +4069,7 @@ public function show(string $content, ?string $stream = null): CliOne
* To add values you could use the method uplevel()<br/>
* To remove a value (going down a level) you could use the method downlevel()<br/>
* You can also change the style using setPattern1(),setPattern2(),setPattern3()<br/>
* ```php
* ```
* $cli->setPattern1('{value}{type}') // the level
* ->setPattern2('<bred>{value}</bred>{type}') // the current level
* ->setPattern3(' -> ') // the separator
Expand Down Expand Up @@ -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:<br/>
* ```php
* &lt;red&gt;error&lt;/red&gt; (color red)
* ```
* <red>error&lt;/red&gt; (color red)
* &lt;yellow&gt;warning&lt;/yellow&gt; (color yellow)
* &lt;blue&gt;information&lt;/blue&gt; (blue)
* &lt;yellow&gt;yellow&lt;/yellow&gt; (yellow)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -4799,7 +4812,7 @@ public function showValuesColumn(array $values, string $type, ?string $patternCo
/**
* It shows a waiting cursor.<br>
* <b>Example:</b><br/>
* ```php
* ```
* $this->hideCursor()->showWaitCursor(true);
* $this->showWaitCursor(); // inside a loop.
* $this->hideWaitCursor()->showCursor(); // at the end of the loop
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
* ```
Expand Down Expand Up @@ -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);
* ```
Expand Down Expand Up @@ -5507,7 +5523,7 @@ protected function readline(string $content, CliOneParam $parameter)

/**
* It sets the color of the cli<br/>
* ```php
* ```
* <red>error</red> (color red)
* <yellow>warning</yellow> (color yellow)
* <blue>information</blue> (blue)
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit 2e255e2

Please sign in to comment.