diff --git a/README.md b/README.md index 64b4f98..612ba41 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # tin -tin is a PHP code highlighter for the terminal. - +tin is a PHP code highlighter for the terminal. + [![Tests](https://github.com/felixdorn/tin/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/felixdorn/tin/actions/workflows/tests.yml) [![Formats](https://github.com/felixdorn/tin/actions/workflows/formats.yml/badge.svg?branch=main)](https://github.com/felixdorn/tin/actions/workflows/formats.yml) [![Version](https://poser.pugx.org/felixdorn/tin/version)](//packagist.org/packages/felixdorn/tin) @@ -47,11 +47,21 @@ use Felix\Tin\Token; $tin->process( $code, - fn(Token $token, Token $lastToken) => $token->line . '| ' . $token->text + function(Token $token, Token $lastToken): ?string { + // won't print any of the ";" tokens + if ($token->is(";")) { + return null; + } + + return $token->line . '| ' . $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. +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 @@ -87,6 +97,7 @@ class OneDark extends Theme * 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 diff --git a/src/Tin.php b/src/Tin.php index 9e0bc89..3a95af3 100644 --- a/src/Tin.php +++ b/src/Tin.php @@ -61,9 +61,14 @@ public function process(string $code, callable $transformer): string }; } - $text = $token->text; - $token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m"; - $highlighted .= $transformer($token, $lastToken); + $text = $token->text; + $token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m"; + $highlightedToken = $transformer($token, $lastToken); + + if ($highlightedToken !== null) { + $highlighted .= $highlightedToken; + } + $token->text = $text; } diff --git a/tests/TinTest.php b/tests/TinTest.php index 83c0d33..8358c7d 100644 --- a/tests/TinTest.php +++ b/tests/TinTest.php @@ -27,3 +27,17 @@ expect($output)->toBe(T_OPEN_TAG . T_ECHO . T_WHITESPACE . T_CONSTANT_ENCAPSED_STRING . ord(';')); }); + +it('can skip tokens', function () { + $tin = Tin::from(new OneDark()); + + $output = $tin->process('id === T_ECHO) { + return null; + } + + return $token->id; + }); + + expect($output)->toBe(T_OPEN_TAG . T_WHITESPACE . T_CONSTANT_ENCAPSED_STRING . ord(';')); +});