mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Backporting overwrite() from 3.0 to 2.6.
This commit is contained in:
parent
460204913a
commit
5e5a776de8
2 changed files with 72 additions and 1 deletions
|
@ -167,6 +167,14 @@ class Shell extends Object {
|
||||||
*/
|
*/
|
||||||
public $stdin;
|
public $stdin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of bytes last written to the output stream
|
||||||
|
* used when overwriting the previous message.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $_lastWritten = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs this Shell instance.
|
* Constructs this Shell instance.
|
||||||
*
|
*
|
||||||
|
@ -609,11 +617,44 @@ class Shell extends Object {
|
||||||
$currentLevel = Shell::QUIET;
|
$currentLevel = Shell::QUIET;
|
||||||
}
|
}
|
||||||
if ($level <= $currentLevel) {
|
if ($level <= $currentLevel) {
|
||||||
return $this->stdout->write($message, $newlines);
|
$this->_lastWritten = $this->stdout->write($message, $newlines);
|
||||||
|
return $this->_lastWritten;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwrite some already output text.
|
||||||
|
*
|
||||||
|
* Useful for building progress bars, or when you want to replace
|
||||||
|
* text already output to the screen with new text.
|
||||||
|
*
|
||||||
|
* **Warning** You cannot overwrite text that contains newlines.
|
||||||
|
*
|
||||||
|
* @param array|string $message The message to output.
|
||||||
|
* @param int $newlines Number of newlines to append.
|
||||||
|
* @param int $size The number of bytes to overwrite. Defaults to the
|
||||||
|
* length of the last message output.
|
||||||
|
* @return int|bool Returns the number of bytes returned from writing to stdout.
|
||||||
|
*/
|
||||||
|
public function overwrite($message, $newlines = 1, $size = null) {
|
||||||
|
$size = $size ? $size : $this->_lastWritten;
|
||||||
|
|
||||||
|
// Output backspaces.
|
||||||
|
$this->out(str_repeat("\x08", $size), 0);
|
||||||
|
|
||||||
|
$newBytes = $this->out($message, 0);
|
||||||
|
|
||||||
|
// Fill any remaining bytes with spaces.
|
||||||
|
$fill = $size - $newBytes;
|
||||||
|
if ($fill > 0) {
|
||||||
|
$this->out(str_repeat(' ', $fill), 0);
|
||||||
|
}
|
||||||
|
if ($newlines) {
|
||||||
|
$this->out($this->nl($newlines), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs a single or multiple error messages to stderr. If no parameters
|
* Outputs a single or multiple error messages to stderr. If no parameters
|
||||||
* are passed outputs just a newline.
|
* are passed outputs just a newline.
|
||||||
|
|
|
@ -368,6 +368,36 @@ class ShellTest extends CakeTestCase {
|
||||||
$this->Shell->out('Quiet', 1, Shell::QUIET);
|
$this->Shell->out('Quiet', 1, Shell::QUIET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test overwriting.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testOverwrite() {
|
||||||
|
$number = strlen('Some text I want to overwrite');
|
||||||
|
|
||||||
|
$this->Shell->stdout->expects($this->at(0))
|
||||||
|
->method('write')
|
||||||
|
->with('Some <info>text</info> I want to overwrite', 0)
|
||||||
|
->will($this->returnValue($number));
|
||||||
|
|
||||||
|
$this->Shell->stdout->expects($this->at(1))
|
||||||
|
->method('write')
|
||||||
|
->with(str_repeat("\x08", $number), 0);
|
||||||
|
|
||||||
|
$this->Shell->stdout->expects($this->at(2))
|
||||||
|
->method('write')
|
||||||
|
->with('Less text', 0)
|
||||||
|
->will($this->returnValue(9));
|
||||||
|
|
||||||
|
$this->Shell->stdout->expects($this->at(3))
|
||||||
|
->method('write')
|
||||||
|
->with(str_repeat(' ', $number - 9), 0);
|
||||||
|
|
||||||
|
$this->Shell->out('Some <info>text</info> I want to overwrite', 0);
|
||||||
|
$this->Shell->overwrite('Less text');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testErr method
|
* testErr method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue