Skip to content

[Console] Extract customizable BaseOutputStyle and reusable MessageBlock #42093

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

hushker
Copy link

@hushker hushker commented Jul 13, 2021

Q A
Branch? 5.4 for features
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #39184
License MIT

There are two purposes of changes:

  • Make it easy to create custom Console Output Style
  • Protect SymfonyStyle style guide from overriding

Changes:

  • SymfonyStyle became the final class to prevent overriding
  • MessageBlock extracted to provide a reusable method for generating block lines
  • BaseOutputStyle extracted and could be overridden (const and methods are protected) for customization purpose

Example of customization:

<?php

namespace App\OutputStyle;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Style\BaseOutputStyle;

class MyCustomOutputStyle extends BaseOutputStyle
{
    protected const TITLE_CHAR = '=';
    protected const SECTION_CHAR = '-';
    protected const LISTING_CHAR = '*';
    protected const COMMENT_STYLE = '<fg=default;bg=default> // </>';
    protected const SUCCESS_STYLE = 'fg=black;bg=green';
    protected const ERROR_STYLE = 'fg=white;bg=gray';
    protected const WARNING_STYLE = 'fg=black;bg=yellow';
    protected const NOTE_STYLE = 'fg=yellow';
    protected const INFO_STYLE = 'fg=green';

    public function title(string $message)
    {
        $this->autoPrependBlock();

        $this->writeln([
                            'I want to customize it all',
                           sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
                           sprintf('<comment>%s</>', str_repeat(static::TITLE_CHAR, Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))),
                       ]);
        $this->newLine();
    }
}

We can customize easily by overriding the style constants of whole methods.

@hushker hushker requested a review from chalasr as a code owner July 13, 2021 19:17
@hushker hushker force-pushed the SF-39184-symfony-style-decouple-2 branch from 991fa8c to 1e7a013 Compare July 13, 2021 19:21
@OskarStark OskarStark requested a review from javiereguiluz July 13, 2021 19:22
@hushker hushker force-pushed the SF-39184-symfony-style-decouple-2 branch 2 times, most recently from 14fd3a1 to b23ecca Compare July 13, 2021 20:39
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

@chalasr chalasr added this to the 5.4 milestone Jul 14, 2021
@hushker hushker force-pushed the SF-39184-symfony-style-decouple-2 branch from b23ecca to 83c91c6 Compare July 14, 2021 16:47
@hushker hushker force-pushed the SF-39184-symfony-style-decouple-2 branch from 83c91c6 to ac6611a Compare July 14, 2021 17:51
@chalasr chalasr self-requested a review July 14, 2021 17:57
@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 3, 2021
@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Console] Separate styling helpers out of SymfonyStyle
6 participants