Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\yii\console\widgets\Table::widget problem with screenwidth #15552

Closed
vinpel opened this issue Jan 24, 2018 · 10 comments
Closed

\yii\console\widgets\Table::widget problem with screenwidth #15552

vinpel opened this issue Jan 24, 2018 · 10 comments
Labels
Milestone

Comments

@vinpel
Copy link
Contributor

vinpel commented Jan 24, 2018

What steps will reproduce the problem?

use console widget table when screen size can't be determined

print '<pre>';
print \yii\console\widgets\Table::widget([
          'headers' => ['a','b'],
          'rows'    => [['a','b']]
        ]);

What is the expected result?

image

What do you get instead?

image

Additional info

yii2\console\widgets\Table.php::getScreenWidth() doesn't assign $this->_screenWidth when Console::getScreenSize() return false.
for you'r information on my system :

stty -a 2>&1

produce :

speed 38400 baud; rows 55; columns 207; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

so when yii2\console\widgets\Table.php: calculateRowsSize() do :

      $screenWidth = $this->getScreenWidth() - 3;

it give a negative value for $screenWidth and produce the php error.

Proposed fix

Set a default value for screenWidth:

    /**
     * Getting screen width.
     *
     * @return int screen width
     */
    protected function getScreenWidth()
    {
        if (!$this->_screenWidth) {
            $size = Console::getScreenSize();
            if (isset($size[0])) {
                $this->_screenWidth = $size[0];
            }else{
              $this->_screenWidth=1000;
            }
        }

        return $this->_screenWidth;
    }

Will produce :
image

Only Fix the php error

    /**
     * Calculate the size of rows to draw anchor of columns in console.
     *
     * @see \yii\console\widgets\Table::render()
     */
    protected function calculateRowsSize()
    {
        $this->_columnWidths = $columns = [];
        $totalWidth = 0;
        $screenWidth = $this->getScreenWidth() - 3;
        if ($screenWidth < 0){
          $screenWidth=0;
        }

will produce :
image

ugly but no error

Q A
Yii version 2.0.13
PHP version 7.1
Operating system Alpine Linux
@samdark samdark added type:bug Bug status:ready for adoption Feel free to implement this issue. labels Jan 24, 2018
@samdark samdark added this to the 2.0.15 milestone Jan 24, 2018
@nadar
Copy link
Contributor

nadar commented Jan 30, 2018

Maybe having something similar on OSX with MAMP installed:

Exception in file "/Applications/MAMP/htdocs/luya-env-dev/vendor/yiisoft/yii2/helpers/BaseConsole.php" on line #630: exec(): Unable to fork [stty -a 2>&1]
php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

@nadar
Copy link
Contributor

nadar commented May 14, 2018

Maybe also something which could be related:

PHP Warning 'yii\base\ErrorException' with message 'str_repeat(): Second argument has to be greater than or equal to 0'

in /app/vendor/yiisoft/yii2/console/widgets/Table.php:289

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'str_repeat(): S...', '/app/vendor/yii...', 289, Array)
#1 /app/vendor/yiisoft/yii2/console/widgets/Table.php(289): str_repeat('\xE2\x95\x90', -2)
#2 /app/vendor/yiisoft/yii2/console/widgets/Table.php(181): yii\console\widgets\Table->renderSeparator('\xE2\x95\x94', '\xE2\x95\xA4', '\xE2\x95\x90', '\xE2\x95\x97')
#3 /app/vendor/luyadev/luya-core/console/commands/ImportController.php(218): yii\console\widgets\Table->run()

@samdark
Copy link
Member

samdark commented May 14, 2018

Sounds like it...

@nadar
Copy link
Contributor

nadar commented May 16, 2018

It happens randomly, therefore i thought its good to post for someone to debug, maybe it helps.

@nadar
Copy link
Contributor

nadar commented Jun 2, 2018

Another exception:

PHP Warning 'yii\base\ErrorException' with message 'Division by zero'

in /app/vendor/yiisoft/yii2/console/widgets/Table.php:356

Stack trace:
#0 /app/vendor/yiisoft/yii2/console/widgets/Table.php(356): yii\base\ErrorHandler->handleError(2, 'Division by zer...', '/app/vendor/yii...', 356, Array)
#1 [internal function]: yii\console\widgets\Table->yii\console\widgets\{closure}(2, 3)
#2 /app/vendor/yiisoft/yii2/console/widgets/Table.php(364): array_map(Object(Closure), Array, Array)
#3 /app/vendor/yiisoft/yii2/console/widgets/Table.php(231): yii\console\widgets\Table->calculateRowHeight(Array)
#4 /app/vendor/yiisoft/yii2/console/widgets/Table.php(187): yii\console\widgets\Table->renderRow(Array, '\xE2\x95\x91', '\xE2\x94\x82', '\xE2\x95\x91')
#5 /app/repos/luya/core/console/commands/ImportController.php(218): yii\console\widgets\Table->run()
#6 /app/repos/luya/core/console/commands/ImportController.php(190): luya\console\commands\ImportController->logValueToTable(Array)
#7 [internal function]: luya\console\commands\ImportController->actionIndex()
#8 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#9 /app/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#10 /app/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('', Array)
#11 /app/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('', Array)
#12 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('import', Array)
#13 /app/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('import', Array)
#14 /app/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#15 /app/repos/luya/core/base/Boot.php(205): yii\base\Application->run()
#16 /app/repos/luya/core/base/Boot.php(162): luya\base\Boot->applicationConsole()
#17 /app/public_html/index.php(8): luya\base\Boot->run()
#18 {main}

@samdark would you accept a PR which checks if $size (https://github.com/yiisoft/yii2/blob/master/framework/console/widgets/Table.php#L356) is empty in order to prevent the exception and just use a default height like

public $fallbackHeight = 25;

public function getFallbackHeight($size)
{
    if ($size <= 2) {
         return $this->fallbackHeight;
    }

    return $size;
}

Something simliar for $rowSize (https://github.com/yiisoft/yii2/blob/master/framework/console/widgets/Table.php#L289).

When running commands in dockerized environments this problem appears quite often.

@samdark
Copy link
Member

samdark commented Jun 3, 2018

Yes.

@schmunk42
Copy link
Contributor

@machour This one could also be moved to 2.0.17 IMHO

@samdark samdark modified the milestones: 2.0.16, 2.0.17 Jan 14, 2019
@samdark
Copy link
Member

samdark commented Jan 14, 2019

Agree. Moved.

@samdark
Copy link
Member

samdark commented Mar 8, 2019

See #16830 for more comments (specifically ones by @cebe).

@samdark samdark removed this from the 2.0.17 milestone Mar 8, 2019
@samdark
Copy link
Member

samdark commented May 18, 2020

Should be fixed by 3b87c5f

@samdark samdark closed this as completed May 18, 2020
@samdark samdark added this to the 2.0.36 milestone May 18, 2020
@samdark samdark removed the status:ready for adoption Feel free to implement this issue. label May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants