How to Color and Style the Console Output

By using colors in the command output, you can distinguish different types of output (e.g. important messages, titles, comments, etc.).

コマンド出力で色を使用することにより、さまざまなタイプの出力 (重要なメッセージ、タイトル、コメントなど) を区別できます。

Note

ノート

By default, the Windows command console doesn't support output coloring. The Console component disables output coloring for Windows systems, but if your commands invoke other scripts which emit color sequences, they will be wrongly displayed as raw escape characters. Install the Cmder, ConEmu, ANSICON, Mintty (used by default in GitBash and Cygwin) or Hyper free applications to add coloring support to your Windows command console.

デフォルトでは、Windows コマンド コンソールは出力の色分けをサポートしていません。コンソール コンポーネントは Windows システムの出力カラーリングを無効にしますが、コマンドがカラー シーケンスを出力する他のスクリプトを呼び出すと、生のエスケープ文字として誤って表示されます。 Cmder、ConEmu、ANSICON、Mintty (GitBash および Cygwin でデフォルトで使用) または Hyperfree アプリケーションをインストールして、Windows コマンド コンソールにカラーリング サポートを追加します。

Using Color Styles

Whenever you output text, you can surround the text with tags to color its output. For example:

テキストを出力するときはいつでも、テキストをタグで囲んで出力に色を付けることができます。例えば:
1
2
3
4
5
6
7
8
9
10
11
// green text
$output->writeln('<info>foo</info>');

// yellow text
$output->writeln('<comment>foo</comment>');

// black text on a cyan background
$output->writeln('<question>foo</question>');

// white text on a red background
$output->writeln('<error>foo</error>');

The closing tag can be replaced by </>, which revokes all formatting options established by the last opened tag.

終了タグは で置き換えることができます。これにより、最後に開いたタグによって確立されたすべての書式設定オプションが取り消されます。

It is possible to define your own styles using the OutputFormatterStyle class:

OutputFormatterStyle クラスを使用して、独自のスタイルを定義することができます。
1
2
3
4
5
6
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

// ...
$outputStyle = new OutputFormatterStyle('red', '#ff0', ['bold', 'blink']);
$output->getFormatter()->setStyle('fire', $outputStyle);

$output->writeln('<fire>foo</>');

Any hex color is supported for foreground and background colors. Besides that, these named colors are supported: black, red, green, yellow, blue, magenta, cyan, white, gray, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan and bright-white.

前景色と背景色には、任意の 16 進数の色がサポートされています。それに加えて、次の名前付きの色がサポートされています: 黒、赤、緑、黄、青、マゼンタ、シアン、白、灰色、明るい赤、明るい緑、明るい黄色、明るい青、明るいマゼンタ、明るいシアンそして明るい白。

Note

ノート

If the terminal doesn't support true colors, the given color is replaced by the nearest color depending on the terminal capabilities. E.g. #c0392b is degraded to #d75f5f in 256-color terminals and to red in 8-color terminals.

端末がトゥルー カラーをサポートしていない場合、指定された色は端末の機能に応じて最も近い色に置き換えられます。例えば。 #c0392b は、256 色端末では #d75f5f に、8 色端末では赤に劣化します。

6.2

6.2

The support for 256-color terminals was introduced in Symfony 6.2.

256 色の端末のサポートは、Symfony 6.2 で導入されました。

And available options are: bold, underscore, blink, reverse (enables the "reverse video" mode where the background and foreground colors are swapped) and conceal (sets the foreground color to transparent, making the typed text invisible - although it can be selected and copied; this option is commonly used when asking the user to type sensitive information).

使用可能なオプションは次のとおりです: ボールド、アンダースコア、点滅、リバース (背景色と前景色が入れ替わる「リバース ビデオ」モードを有効にします) および隠し (前景色を透明に設定し、入力したテキストを非表示にします - ただし、選択してコピーすることはできます) ; このオプションは、ユーザーに機密情報の入力を求める場合によく使用されます)。

You can also set these colors and options directly inside the tag name:

これらの色とオプションをタグ名内で直接設定することもできます:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// using named colors
$output->writeln('<fg=green>foo</>');

// using hexadecimal colors
$output->writeln('<fg=#c0392b>foo</>');

// black text on a cyan background
$output->writeln('<fg=black;bg=cyan>foo</>');

// bold text on a yellow background
$output->writeln('<bg=yellow;options=bold>foo</>');

// bold text with underscore
$output->writeln('<options=bold,underscore>foo</>');

Note

ノート

If you need to render a tag literally, escape it with a backslash: \<info> or use the escape() method to escape all the tags included in the given string.

タグを文字どおりにレンダリングする必要がある場合は、バックスラッシュでエスケープします: \または、escape() メソッドを使用して、指定された文字列に含まれるすべてのタグをエスケープします。

Commands can use the special <href> tag to display links similar to the <a> elements of web pages:

コマンドは、Web ページの要素に似たリンクを表示するために specialtag を使用できます。
1
$output->writeln('<href=https://symfony.com>Symfony Homepage</>');

If your terminal belongs to the list of terminal emulators that support links you can click on the "Symfony Homepage" text to open its URL in your default browser. Otherwise, you'll see "Symfony Homepage" as regular text and the URL will be lost.

お使いの端末がリンクをサポートする端末エミュレーターのリストに属している場合は、「Symfony ホームページ」テキストをクリックして、デフォルトのブラウザーでその URL を開くことができます。そうしないと、「Symfony ホームページ」が通常のテキストとして表示され、URL が失われます。