Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] progress bar fix #16490

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

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ProgressBar: make use of TerminalDimensionsProvider
  • Loading branch information
TomasVotruba committed Nov 14, 2015
commit 066f39176e04e51f84f31f2451293c8fad7a5e86
40 changes: 15 additions & 25 deletions 40 src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface;

/**
* The ProgressBar provides helpers to display progress output.
Expand Down Expand Up @@ -51,19 +52,24 @@ class ProgressBar
private static $formats;

/**
* Constructor.
*
* @param OutputInterface $output An OutputInterface instance
* @param int $max Maximum steps (0 if unknown)
* @var TerminalDimensionsProviderInterface
*/
private $terminalDimensionsProvider;

/**
* @param OutputInterface $output An OutputInterface instance
* @param int $max Maximum steps (0 if unknown)
* @param TerminalDimensionsProviderInterface $terminalDimensionsProvider
*/
public function __construct(OutputInterface $output, $max = 0)
public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProviderInterface $terminalDimensionsProvider = null)
{
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}

$this->output = $output;
$this->setMaxSteps($max);
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();

if (!$this->output->isDecorated()) {
// disable overwrite when output does not support ANSI codes.
Expand Down Expand Up @@ -616,31 +622,15 @@ private static function initFormats()
private function adjustBarWidthToWindowWidth($line)
{
$lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
$windowWidth = $this->getTerminalWidth();
$windowWidth = $this->terminalDimensionsProvider->getTerminalWidth();
if ($lineLength > $windowWidth) {
$barWidthDelta = $lineLength - $windowWidth;
$newBarWidth = $this->barWidth - $barWidthDelta - 20;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where that 20 comes from? Seems useless to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shall be calculated as "terminal width - everything except bar width (number, %, memory etc.)"

But first, I need to find way to test this properly.

$this->setBarWidth($newBarWidth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new width should be checked for negative values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added check to the setBarWidth() method, since other methods depends on it.

It should be always positive number.


return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty line required before the return... but this method looks strange to me anyway... you should change something or return something but not both... why is return required?

}
return false;
}

/**
* @return int
*/
private function getTerminalWidth()
{
$dimensions = $this->getTerminalDimensions();
return $dimensions[0];
}

/**
* @return int[]
*/
private function getTerminalDimensions()
{
$application = new Application();
return $application->getTerminalDimensions();
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.