【PHP】Bash Colorizer の使い方

Bash

PHPでBashターミナルへテキストをカラー出力するツール Bash Colorizer の使い方についての解説です。

GitHub - macocci7/BashColorizer: Let's make your bash terminal full of colors!
Let's make your bash terminal full of colors! Contribute to macocci7/BashColorizer development by creating an account on...

検証済のターミナル

  • VSCode Terminal (VSCode 1.99.1 / Windows 11)
  • Windows Terminal 1.22.10731.0

前提条件

  • PHP8.1以降インストール済
  • Composer v2インストール済
  • Bash v5 インストール済
  • Ubuntu24.04.1 (WSL2)上で作業しています

インストール

プロジェクトフォルダ内でComposerコマンドを実行します。

composer require macocci7/bash-colorizer

基本的な使い方:テキスト出力

「src/colorizer.php」なるファイルを前提にコードを書いていきます。

▼まずはComposerの「autoload.php」をインポートしてから、use宣言します。

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Macocci7\BashColorizer\Colorizer;

▼普通にテキスト出力してみます。(色指定なし)

※静的呼出の例

Colorizer::echo("Hi, there!");
Colorizer::echo(" How's it going with you?", PHP_EOL);

※メソッドチェーンの例

Colorizer::echo("Hi, there!");
    ->echo(" How's it going with you?", PHP_EOL);

※インスタンス生成の例

$colorizer = new Colorizer;
$colorizer->echo("Hi, there!")
    ->echo(" How's it going with you?", PHP_EOL);

全て実行結果は同じで、普通の echo と同じです。

Colorizer の echo() メソッドは次のようになっており、

    public static function echo(string $string, string $eol = ''): self
    {
        echo static::encode($string, $eol);
        return new self(static::$config);
    }

第二引数はEOLで、デフォルト空文字(改行しない)です。

出力に付け足しているだけなので、<br />などの指定もできますし、

\n\n\n\n とかの指定もできます。

基本的な使い方:出力設定

出力設定には3つの項目があります。

1.[ attributes ] ・・・属性(太字、イタリック、取消線、下線等)

2.[ foreground ] ・・・テキスト色

3.[ background ] ・・・背景色

配列に収めて丸ごと突っ込むと楽でしょう。

$config = [
    'attributes' => ['italic', 'bold'],
    'foreground' => 'black',
    'background' => 'green',
];

※静的呼出の例

Colorizer::config($config);
Colorizer::echo("Hi, there!", PHP_EOL);

※メソッドチェーンの例

Colorizer::config($config)
    ->echo("Hi, there!", PHP_EOL);

※インスタンス生成の例

$colorizer = new Colorizer;                // 生成例1(configなし)
$colorizer = new Colorizer($config);       // 生成例2(configあり)
$colorizer = Colorizer::config($config);   // 生成例3(configあり)

$colorizer->config($config)  // config何度でも上書きできる
    ->echo("Hi, there!", PHP_EOL);

どれも実行結果は同じです。

基本的な使い方:属性の設定

attributes() メソッドで属性の設定が可能です。

引数の配列内に複数指定可能です。

Colorizer::attributes(['underline', 'strike'])
    ->echo("Hi, there!", PHP_EOL);

属性の詳細については「設定できる属性」をご覧ください。

基本的な使い方:テキスト色の設定

foreground() メソッドでテキストの色指定が可能です。

引数は1つだけですが、受け入れ可能な型が3つあります。

▼1.文字列型 (string): 

  • 色の名称(black, red, green等 8色)
  • 16進数カラーコード(例:#ffcc00, または #fc0)
Colorizer::foreground('green')
    ->echo("Hi, there!", PHP_EOL);

▼2.整数型 (int): 色番号(0 ~ 255) 256色

Colorizer::foreground(3)
    ->echo("Hi, there!", PHP_EOL);

▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色

Colorizer::foreground([0, 0, 255])
    ->echo("Hi, there!", PHP_EOL);

色の詳細については「設定できる色」をご覧ください。

基本的な使い方:背景色の設定

background() メソッドで、テキストの背景色の設定が可能です。

こちらも同様に、引数は1つですが、受入可能な型が3つあります。

▼1.文字列型 (string): 

  • 色の名称(black, red, green等 8色)
  • 16進数カラーコード(例: #ffcc00, または #fc0 )
Colorizer::background("red")
    ->echo("Hi, there!", PHP_EOL);

▼2.整数型 (int): 色番号(0 ~ 255) 256色

Colorizer::background(4)
    ->echo("Hi, there!", PHP_EOL);

▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色

Colorizer::background([255, 182, 0])
    ->echo("Hi, there!", PHP_EOL);

色の詳細について「設定できる色」をご覧ください。

基本的な使い方:属性・テキスト色・背景色の設定

それぞれをメソッドチェーンで繋げることで、config() メソッドと同等のことができます。

Colorizer::attributes(['double-underline', 'italic'])
    ->foreground("yellow")
    ->background("blue")
    ->echo("Hi, there!", PHP_EOL);

基本的な使い方:下線の色指定

underline() メソッドでテキストの色指定が可能です。

※VSCode Terminalでは有効。Windows Terminalでは無効。

引数は1つだけですが、受け入れ可能な型が3つあります。

▼1.文字列型 (string): 

  • 16進数カラーコード(例:#ffcc00, または #fc0)
Colorizer::underline('#ffcc00')  // または #fc0
    ->echo("Hi, there!", PHP_EOL);

▼2.整数型 (int): 色番号(0 ~ 255) 256色

Colorizer::underline(4)
    ->echo("Hi, there!", PHP_EOL);

▼3.整数配列型 (int[]): RGBの色番号(0 ~ 255) 24bit色

Colorizer::underline([255, 182, 0])
    ->echo("Hi, there!", PHP_EOL);

色の詳細について「設定できる色」をご覧ください。

基本的な使い方:カラー化した文字列の取得

encode() メソッドでカラー化した文字列の取得が可能です。

▼ echo の引数として渡す例

echo "[" . Colorizer::attributes(['double-underline', 'italic', 'bold'])
    ->foreground("blue")
    ->background("yellow")
    ->encode("Hi, there!") . "]" . PHP_EOL;

▼ sprintf() の引数として渡して echo で出力する例

echo sprintf(
    "%s: %s%s",
    Colorizer::attributes(['bold'])
        ->foreground("blue")
        ->background("yellow")
        ->encode(" Donaldo "),
    "Hi, there!",
    PHP_EOL
);

▼カラー化した文字列のエスケープシーケンスを可視化して出力する例

※エスケープシーケンスを文字列置換

echo Colorizer::attributes(["bold"])
    ->background([255, 255, 0])
    ->foreground([0, 128, 255])
    ->readable('Hi, There!', PHP_EOL);

この文字列に対して、bashの echo コマンドを -e オプション付で実行すると、色付きで表示されます。

echo -e '\033[1;38;2;0;128;255;48;2;255;255;0mHi, there!\033[m'

設定できる属性

設定できる属性は次の通りです。

属性名説明VSCode TerminalWindows Terminal
resetリセット
bold太字×
faint低輝度
italicイタリック
underline下線
blink点滅×
※1
fast-blink高速点滅×
※1、2
reverse反転
conceal非表示
strike取消線
gothicゴシック××
double-underline二重下線
normal太字解除
低輝度解除
no-italicイタリック解除
no-underline下線解除
no-blink点滅解除
※3

※4
proportional-spacing等幅スペース
no-reverse反転解除
no-conceal非表示解除
no-strike取消線解除
no-proportional-spacing等幅スペース解除
framed枠付××
encircled丸付××
overlined上線
no-framed-no-encircled枠解除、丸解除
※3

※3
no-overlined上線解除
underline-color下線色指定
※5

※1:低輝度だと無効。

※2:高速ではない。blink と同じテンポ。

※3:対になる属性が無効のため不明。

※4:fast-blink にも有効

※5:一部有効

▼出力例(VSCode Terminal)

※「vendor/macocci7/bash-colorizer/playground」をプロジェクトフォルダにコピーして実行。

設定できる色

テキスト色、背景色ともに共通です。

▼名称で設定できる色:

名称説明
black
red
green
yellow
blue
magentaマジェンタ
cyanシアン
white
extended拡張色
(名称のみ)
(効果なし)
default標準

VSCode Terminal での例

▼テキスト色

playground/foreground.php

▼ 256色での設定 ( 0 ~ 255 )

・テキスト色の例(VSCode Terminal)

playground/foreground_256colors.php

・背景色の例(VSCode Terminal)

playground/background_256colors.php

・下線色の例(VSCode Terminal)

playground/underline_256colors.php

▼ 24bit (16777216) 色 (RGBそれぞれ 0 ~ 255)

・テキスト色の例(VSCode Terminal)

playground/foreground_24bitcolors.php

・背景色の例(VSCode Terminal)

playground/background_24bitcolors.php

・下線色の例(VSCode Terminal)

playground/underline_24bitcolors.php

おまけ

playground/colorizer.php

の実行結果

playground/converter.php

の実行結果

以上です。

コメント

タイトルとURLをコピーしました