Browse Source

Simplified & fixed ConsoleIO->overwrite, corrected output when downloading with progress

Konstantin Tjuterev 13 years ago
parent
commit
0b7896cdad

+ 0 - 2
src/Composer/Downloader/FileDownloader.php

@@ -84,8 +84,6 @@ class FileDownloader implements DownloaderInterface
         if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
             throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
         }
-
-        $this->io->write('');
     }
 
     /**

+ 20 - 10
src/Composer/IO/ConsoleIO.php

@@ -31,6 +31,7 @@ class ConsoleIO implements IOInterface
     protected $authorizations = array();
     protected $lastUsername;
     protected $lastPassword;
+    protected $lastMessage;
 
     /**
      * Constructor.
@@ -60,31 +61,40 @@ class ConsoleIO implements IOInterface
     public function write($messages, $newline = true)
     {
         $this->output->write($messages, $newline);
+        $this->lastMessage = join($newline ? "\n" : '', (array) $messages);
     }
 
     /**
      * {@inheritDoc}
      */
-    public function overwrite($messages, $newline = true, $size = 80)
+    public function overwrite($messages, $newline = true, $size = null)
     {
-        for ($place = $size; $place > 0; $place--) {
-            $this->write("\x08", false);
+        // messages can be an array, let's convert it to string anyway
+        $messages = join($newline ? "\n" : '', (array) $messages);
+
+        // since overwrite is supposed to overwrite last message...
+        if (!isset($size)) {
+            // removing possible formatting of lastMessage with strip_tags
+            $size = strlen(strip_tags($this->lastMessage));
         }
+        // ...let's fill its length with backspaces
+        $this->write(str_repeat("\x08", $size), false);
 
+        // write the new message
         $this->write($messages, false);
 
-        for ($place = ($size - strlen($messages)); $place > 0; $place--) {
-            $this->write(' ', false);
-        }
-
-        // clean up the end line
-        for ($place = ($size - strlen($messages)); $place > 0; $place--) {
-            $this->write("\x08", false);
+        $fill = $size - strlen(strip_tags($messages));
+        if ($fill > 0) {
+            // whitespace whatever has left
+            $this->write(str_repeat(' ', $fill), false);
+            // move the cursor back
+            $this->write(str_repeat("\x08", $fill), false);
         }
 
         if ($newline) {
             $this->write('');
         }
+        $this->lastMessage = $messages;
     }
 
     /**

+ 1 - 1
src/Composer/Util/RemoteFilesystem.php

@@ -98,7 +98,7 @@ class RemoteFilesystem
         $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
 
         if ($this->progress) {
-            $this->io->overwrite("    Downloading: <comment>connection...</comment>", false);
+            $this->io->write("    Downloading: <comment>connection...</comment>", false);
         }
 
         if (null !== $fileName) {

+ 17 - 17
tests/Composer/Test/IO/ConsoleIOTest.php

@@ -53,35 +53,35 @@ class ConsoleIOTest extends TestCase
     {
         $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
         $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
+
         $outputMock->expects($this->at(0))
             ->method('write')
-            ->with($this->equalTo("\x08"), $this->equalTo(false));
-        $outputMock->expects($this->at(19))
-            ->method('write')
-            ->with($this->equalTo("\x08"), $this->equalTo(false));
-        $outputMock->expects($this->at(20))
+            ->with($this->equalTo('something (<question>strlen = 23</question>)'));
+        $outputMock->expects($this->at(1))
             ->method('write')
-            ->with($this->equalTo('some information'), $this->equalTo(false));
-        $outputMock->expects($this->at(21))
+            ->with($this->equalTo(str_repeat("\x08", 23)), $this->equalTo(false));
+        $outputMock->expects($this->at(2))
             ->method('write')
-            ->with($this->equalTo(' '), $this->equalTo(false));
-        $outputMock->expects($this->at(24))
+            ->with($this->equalTo('shorter (<comment>12</comment>)'), $this->equalTo(false));
+        $outputMock->expects($this->at(3))
             ->method('write')
-            ->with($this->equalTo(' '), $this->equalTo(false));
-        $outputMock->expects($this->at(25))
+            ->with($this->equalTo(str_repeat(' ', 11)), $this->equalTo(false));
+        $outputMock->expects($this->at(4))
             ->method('write')
-            ->with($this->equalTo("\x08"), $this->equalTo(false));
-        $outputMock->expects($this->at(28))
+            ->with($this->equalTo(str_repeat("\x08", 11)), $this->equalTo(false));
+        $outputMock->expects($this->at(5))
             ->method('write')
-            ->with($this->equalTo("\x08"), $this->equalTo(false));
-        $outputMock->expects($this->at(29))
+            ->with($this->equalTo(str_repeat("\x08", 12)), $this->equalTo(false));
+        $outputMock->expects($this->at(6))
             ->method('write')
-            ->with($this->equalTo(''));
+            ->with($this->equalTo('something longer than initial (<info>34</info>)'));
 
         $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
 
         $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
-        $consoleIO->overwrite('some information', true, 20);
+        $consoleIO->write('something (<question>strlen = 23</question>)');
+        $consoleIO->overwrite('shorter (<comment>12</comment>)', false);
+        $consoleIO->overwrite('something longer than initial (<info>34</info>)');
     }
 
     public function testAsk()