tin
tin is a PHP code highlighter for the terminal.
Installation
Requires PHP 8.0.0+
You can install the package via composer:
composer require felixdorn/tin
🔞
Screenshots
Yes, this comes from a terminal.
Usage
use Felix\Tin\Themes\JetbrainsDark;
use Felix\Tin\Tin;
$theme = new JetbrainsDark();
$tin = new Tin($theme);
echo $tin->highlight(");
Customizing the output
Apart from using a custom theme to change the colors, you have complete control over the highlighting proccess.
use Felix\Tin\Token;
$tin->process(
$code,
function(Token $token, Token $lastToken): ?string {
// won't print any of the ";" tokens
if ($token->is(";")) {
return null;
}
return $token->text;
}
)
Last token represents the last token in the input code, it is very useful as you can then get the number of lines in the code with $lastToken->line
to properly indent line numbers for example.
If you return null
from the callback, the token will be skipped.
Themes
Creating a theme
You need to extend Felix\Tin\Themes\Theme
and set the colors to whatever you want.
The color are RGB values separated by a ;
.
use Felix\Tin\Themes\Theme;
class OneDark extends Theme
{
public string $keyword = '199;120;221';
public string $variable = '224;107;116';
public string $comment = '91;98;110';
public string $default = '171;178;191';
public string $string = '152;195;121';
public string $function = '98;174;239';
public string $number = '229;192;122';
public string $attribute = '98;174;239';
public string $namedParameter = '98;174;239';
}
Future
- PHPDoc
- Various outputs (cli / web)
- grayscale theme
- better themes (will also boost performances a lot!!)
Known Issues
Named parameters are simply ignored by the built-in PHP parser which means that if you're named parameter is also a keyword such as for, default. The highlighter won't pick up on it and will highlight it as a keyword rather than a named parameter.
There is no solution to that problem unless we implement our own parser (no) or the parser gets fixed,
Testing
composer test
tin was created by Félix Dorn under the MIT license.