~ubuntu-branches/ubuntu/wily/symfony/wily

« back to all changes in this revision

Viewing changes to src/Symfony/Component/Console/Style/SymfonyStyle.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Fabien Potencier
  • Date: 2015-06-14 17:15:34 UTC
  • mfrom: (1.3.3)
  • Revision ID: package-import@ubuntu.com-20150614171534-h74z7c7x7hdhz3ra
Tags: 2.7.1+dfsg-1
[ Fabien Potencier ]
updated VERSION for 2.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
19
19
use Symfony\Component\Console\Helper\Table;
20
20
use Symfony\Component\Console\Input\InputInterface;
 
21
use Symfony\Component\Console\Output\BufferedOutput;
21
22
use Symfony\Component\Console\Output\OutputInterface;
22
23
use Symfony\Component\Console\Question\ChoiceQuestion;
23
24
use Symfony\Component\Console\Question\ConfirmationQuestion;
36
37
    private $questionHelper;
37
38
    private $progressBar;
38
39
    private $lineLength;
 
40
    private $bufferedOutput;
39
41
 
40
42
    /**
41
43
     * @param InputInterface  $input
44
46
    public function __construct(InputInterface $input, OutputInterface $output)
45
47
    {
46
48
        $this->input = $input;
47
 
        $this->lineLength = min($this->getTerminalWidth(), self::MAX_LINE_LENGTH);
 
49
        $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
 
50
        // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
 
51
        $this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
48
52
 
49
53
        parent::__construct($output);
50
54
    }
60
64
     */
61
65
    public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
62
66
    {
 
67
        $this->autoPrependBlock();
63
68
        $messages = is_array($messages) ? array_values($messages) : array($messages);
64
69
        $lines = array();
65
70
 
71
76
        // wrap and add newlines for each element
72
77
        foreach ($messages as $key => $message) {
73
78
            $message = OutputFormatter::escape($message);
74
 
            $lines = array_merge($lines, explode("\n", wordwrap($message, $this->lineLength - Helper::strlen($prefix))));
 
79
            $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
75
80
 
76
81
            if (count($messages) > 1 && $key < count($messages) - 1) {
77
82
                $lines[] = '';
92
97
            }
93
98
        }
94
99
 
95
 
        $this->writeln(implode("\n", $lines)."\n");
 
100
        $this->writeln($lines);
 
101
        $this->newLine();
96
102
    }
97
103
 
98
104
    /**
100
106
     */
101
107
    public function title($message)
102
108
    {
103
 
        $this->writeln(sprintf("\n<comment>%s</>\n<comment>%s</>\n", $message, str_repeat('=', strlen($message))));
 
109
        $this->autoPrependBlock();
 
110
        $this->writeln(array(
 
111
            sprintf('<comment>%s</>', $message),
 
112
            sprintf('<comment>%s</>', str_repeat('=', strlen($message))),
 
113
        ));
 
114
        $this->newLine();
104
115
    }
105
116
 
106
117
    /**
108
119
     */
109
120
    public function section($message)
110
121
    {
111
 
        $this->writeln(sprintf("<comment>%s</>\n<comment>%s</>\n", $message, str_repeat('-', strlen($message))));
 
122
        $this->autoPrependBlock();
 
123
        $this->writeln(array(
 
124
            sprintf('<comment>%s</>', $message),
 
125
            sprintf('<comment>%s</>', str_repeat('-', strlen($message))),
 
126
        ));
 
127
        $this->newLine();
112
128
    }
113
129
 
114
130
    /**
116
132
     */
117
133
    public function listing(array $elements)
118
134
    {
 
135
        $this->autoPrependText();
119
136
        $elements = array_map(function ($element) {
120
 
                return sprintf(' * %s', $element);
121
 
            },
122
 
            $elements
123
 
        );
 
137
            return sprintf(' * %s', $element);
 
138
        }, $elements);
124
139
 
125
 
        $this->writeln(implode("\n", $elements)."\n");
 
140
        $this->writeln($elements);
 
141
        $this->newLine();
126
142
    }
127
143
 
128
144
    /**
130
146
     */
131
147
    public function text($message)
132
148
    {
 
149
        $this->autoPrependText();
 
150
 
133
151
        if (!is_array($message)) {
134
152
            $this->writeln(sprintf(' // %s', $message));
135
153
 
205
223
        $question = new Question($question, $default);
206
224
        $question->setValidator($validator);
207
225
 
208
 
        return $this->askQuestion($question, $validator);
 
226
        return $this->askQuestion($question);
209
227
    }
210
228
 
211
229
    /**
214
232
    public function askHidden($question, $validator = null)
215
233
    {
216
234
        $question = new Question($question);
 
235
 
217
236
        $question->setHidden(true);
 
237
        $question->setValidator($validator);
218
238
 
219
 
        return $this->askQuestion($question, $validator);
 
239
        return $this->askQuestion($question);
220
240
    }
221
241
 
222
242
    /**
290
310
     */
291
311
    public function askQuestion(Question $question)
292
312
    {
 
313
        if ($this->input->isInteractive()) {
 
314
            $this->autoPrependBlock();
 
315
        }
 
316
 
293
317
        if (!$this->questionHelper) {
294
318
            $this->questionHelper = new SymfonyQuestionHelper();
295
319
        }
296
320
 
297
321
        $answer = $this->questionHelper->ask($this->input, $this, $question);
298
322
 
299
 
        $this->newLine();
 
323
        if ($this->input->isInteractive()) {
 
324
            $this->newLine();
 
325
            $this->bufferedOutput->write("\n");
 
326
        }
300
327
 
301
328
        return $answer;
302
329
    }
303
330
 
304
331
    /**
 
332
     * {@inheritdoc}
 
333
     */
 
334
    public function writeln($messages, $type = self::OUTPUT_NORMAL)
 
335
    {
 
336
        parent::writeln($messages, $type);
 
337
        $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
 
338
    }
 
339
 
 
340
    /**
 
341
     * {@inheritdoc}
 
342
     */
 
343
    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
 
344
    {
 
345
        parent::write($messages, $newline, $type);
 
346
        $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
 
347
    }
 
348
 
 
349
    /**
 
350
     * {@inheritdoc}
 
351
     */
 
352
    public function newLine($count = 1)
 
353
    {
 
354
        parent::newLine($count);
 
355
        $this->bufferedOutput->write(str_repeat("\n", $count));
 
356
    }
 
357
 
 
358
    /**
305
359
     * @return ProgressBar
306
360
     */
307
361
    private function getProgressBar()
320
374
 
321
375
        return $dimensions[0] ?: self::MAX_LINE_LENGTH;
322
376
    }
 
377
 
 
378
    private function autoPrependBlock()
 
379
    {
 
380
        $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
 
381
 
 
382
        if (false === $chars) {
 
383
            return $this->newLine(); //empty history, so we should start with a new line.
 
384
        }
 
385
        //Prepend new line for each non LF chars (This means no blank line was output before)
 
386
        $this->newLine(2 - substr_count($chars, "\n"));
 
387
    }
 
388
 
 
389
    private function autoPrependText()
 
390
    {
 
391
        $fetched = $this->bufferedOutput->fetch();
 
392
        //Prepend new line if last char isn't EOL:
 
393
        if ("\n" !== substr($fetched, -1)) {
 
394
            $this->newLine();
 
395
        }
 
396
    }
 
397
 
 
398
    private function reduceBuffer($messages)
 
399
    {
 
400
        // We need to know if the two last chars are PHP_EOL
 
401
        // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
 
402
        return array_map(function ($value) {
 
403
            return substr($value, -4);
 
404
        }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
 
405
    }
323
406
}