Skip to content

[RFC][Console] Added console style guide helpers (v2) #14057

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 34 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0c653a0
added console style guide helpers
kbond Sep 25, 2014
b2bd430
added FormatterInterface and default formatters
kbond Sep 25, 2014
c71222f
added warning result bar
kbond Sep 25, 2014
e20283f
added OutputDecorator
kbond Sep 25, 2014
797a485
added comments
kbond Sep 25, 2014
c1c49c8
adjusted some formats
kbond Sep 25, 2014
bdb3704
added newline helper
kbond Sep 25, 2014
7110316
FormatterInterface::format returns string only
kbond Sep 25, 2014
4b30f14
fix cs
kbond Sep 25, 2014
a358431
fix string padding
kbond Sep 25, 2014
99ebf0e
refactored and simplified
kbond Sep 26, 2014
4d3e078
rename subtitle to section, fix docblocks
kbond Oct 2, 2014
72708d7
refactored
kbond Oct 2, 2014
a59fe02
added table helper
kbond Oct 2, 2014
bafefd6
added question helpers
kbond Oct 2, 2014
d767d28
added style guide table style
kbond Oct 2, 2014
273c187
added StandardQuestionHelper
kbond Oct 2, 2014
b0d2e6a
added table style to Table defaults
kbond Oct 2, 2014
9d82d6a
added `askHidden` helper
kbond Oct 2, 2014
525be2b
fix cs
kbond Oct 2, 2014
16ad7b9
refactored, removed FormatterInterface
kbond Oct 3, 2014
de14472
match current style guide
kbond Oct 3, 2014
d4d6f52
added vertical padding for blocks
kbond Oct 3, 2014
a33d8a7
various fixes
kbond Oct 3, 2014
18dbdb3
revert changes to FormatterHelper
kbond Mar 25, 2015
ae6d7a7
ensure formatter helper exists in QuestionHelper::writeError()
kbond Mar 26, 2015
5c774cc
simplify style tag
kbond Mar 26, 2015
c36189a
rename StyleInterface::ln() to StyleInterface:newLine()
kbond Mar 26, 2015
85cc04c
add StyleInterface::progress()
kbond Mar 26, 2015
e0efaa8
fix bug
kbond Mar 26, 2015
91941f8
add OutputStyle::progressStart, progressAdvance, progressFinish
kbond Mar 26, 2015
8143e77
remove StyleInterface::multipleChoice()
kbond Mar 26, 2015
f0cb90a
set custom progressbar characters only in non-windows environments
kbond Mar 27, 2015
bbdb37c
use DIRECTORY_SEPARATOR to detect Windows
kbond Mar 27, 2015
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
Next Next commit
added console style guide helpers
  • Loading branch information
kbond committed Mar 25, 2015
commit 0c653a0c4826f9977001541f53b29b317eff163d
156 changes: 151 additions & 5 deletions src/Symfony/Component/Console/Helper/FormatterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function formatSection($section, $message, $style = 'info')
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string $style The style to apply to the whole block
* @param bool $large Whether to return a large block
* @param string|array $messages The message to write in the block
* @param string $style The style to apply to the whole block
* @param bool $large Whether to return a large block
* @param int $padLength ength to pad the messages
*
* @return string The formatter message
*/
public function formatBlock($messages, $style, $large = false)
public function formatBlock($messages, $style, $large = false, $padLength = 0)
{
if (!is_array($messages)) {
$messages = array($messages);
Expand All @@ -66,17 +67,162 @@ public function formatBlock($messages, $style, $large = false)
}

for ($i = 0; isset($messages[$i]); ++$i) {
$messages[$i] = sprintf('<%s>%s</%s>', $style, $messages[$i], $style);
$messages[$i] = sprintf('<%s>%s</%s>', $style, str_pad($messages[$i], $padLength), $style);
}

return implode("\n", $messages);
}

/**
* Formats a command title
*
* @param string $message
*
* @return array
*/
public function formatTitle($message)
{
return array(
'',
sprintf('<fg=blue>%s</fg=blue>', $message),
sprintf('<fg=blue>%s</fg=blue>', str_repeat('=', strlen($message))),
''
);
}

/**
* Formats a section title
*
* @param string $message
*
* @return array
*/
public function formatSectionTitle($message)
{
return array(
sprintf('<fg=blue>%s</fg=blue>', $message),
sprintf('<fg=blue>%s</fg=blue>', str_repeat('-', strlen($message))),
''
);
}

/**
* Formats a list element
*
* @param string|array $messages
*
* @return array
*/
public function formatListElement($messages)
{
$messages = array_values((array) $messages);

$messages[0] = sprintf(' * %s', $messages[0]);

foreach ($messages as $key => &$message) {
if (0 === $key) {
continue;
}

$message = sprintf(' %s', $message);
}

return array_merge($messages, array(''));
}

/**
* Formats informational or debug text
*
* @param string $message
*
* @return string
*/
public function formatText($message)
{
return sprintf(' // %s', $message);
}

/**
* Formats a success result bar
*
* @param string|array $messages
*
* @return array
*/
public function formatSuccessResultBar($messages)
{
return $this->formatStyledBlock($messages, 'OK', 'fg=white;bg=green');
}

/**
* Formats an error result bar
*
* @param string|array $messages
*
* @return array
*/
public function formatErrorResultBar($messages)
{
return $this->formatStyledBlock($messages, 'ERROR', 'fg=white;bg=red');
}

/**
* Formats a note admonition
*
* @param string|array $messages
*
* @return array
*/
public function formatNoteBlock($messages)
{
return $this->formatStyledBlock($messages, 'NOTE', 'fg=white', '! ');
}

/**
* Formats a caution admonition
*
* @param string|array $messages
*
* @return array
*/
public function formatCautionBlock($messages)
{
return $this->formatStyledBlock($messages, 'CAUTION', 'fg=white;bg=red', '! ');
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'formatter';
}

/**
* Formats a styled block
*
* @param string|array $messages
* @param string $type
* @param string $style
* @param string $prefix
*
* @return array
*/
protected function formatStyledBlock($messages, $type, $style, $prefix = '')
{
$messages = array_values((array) $messages);

$messages[0] = sprintf('[%s] %s', $type, $messages[0]);

$messages = array_map(function ($value) use ($prefix) {
return sprintf('%s%s', $prefix, $value);
},
$messages
);

return array(
$this->formatBlock($messages, $style, false, 120),
''
);
}
}