swow-cloud/highlighter

dev-master / 3.0.x-dev 2021-12-09 07:24 UTC

This package is auto-updated.

Last update: 2024-12-09 13:50:59 UTC


README

Library, which allows you to highlight your PHP code in terminal

Installation

Installation via composer:

composer require mkgor/highlighter

Printing whole file

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();

echo $highlighter->getWholeFile(__FILE__);

Result

Result

Printing code snippet (and highlighting specified line)

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();

echo $highlighter->getSnippet(__FILE__, 3);

Result

Result

Printing code snippet with specified lines

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();

echo $highlighter->getSpecifiedSnippet(__FILE__, 3, 5);

Result

Result

Printing single line

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();

echo $highlighter->getLine(__FILE__, 3);

Result

Result

You can use same functions with suffix WithoutNumbers to highlight code without line numbers:

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();

echo $highlighter->getWholeFileWithoutNumbers(__FILE__);

Result

Result

Themes

This highlighter supports custom themes. It has built-in themes, but you can create your own by implementing Highlighter\Theme\ThemeInterface

So, to set theme, just call setTheme method of Highlighter

<?php

require_once "vendor/autoload.php";

$highlighter = new \Highlighter\Highlighter();
$highlighter->setTheme(new Highlighter\Theme\DefaultThemes\Minimalistic());

echo $highlighter->getWholeFile(__FILE__);

Built-in themes:

Minimalistic

Theme

Light

Theme

Material

Theme