adhocore/cli-syntax

PHP Code Syntax Highlighter for CLI

0.4.0 2020-01-08 07:41 UTC

This package is auto-updated.

Last update: 2024-12-06 10:24:45 UTC


README

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

        _   _          _
  ___  | | (_)  ___   | |__
 / __| | | | | / __|  '  _  \
| (__  | | | | \___ \ | | | |
 \___| |_| |_| |____/ |_| |_|

PHP CLI Syntax Highlight Tool
=============================

Installation

As phar binary

curl -SsLo ~/clish.phar https://github.com/adhocore/php-cli-syntax/releases/latest/download/clish.phar

chmod +x ~/clish.phar && sudo ln -s ~/clish.phar /usr/local/bin/clish

Follow same steps to upgrade.

As standalone binary

composer global require adhocore/cli-syntax

Follow same steps to upgrade.

As project dependency

composer require adhocore/cli-syntax

Usage

Shell command

If you installed as binary following any of the above methods, then:

# you will be able to run it as
clish -h
clish -f file.php
echo '<?php date("Ymd");' | clish
cat file.php | clish

# export png
clish -f file.php -o file.png

clish stands for CLI syntax highlight.

Options

Parameter options:

  [-e|--echo]            Forces echo to STDOUT when --output is passed
  [-f|--file]            Input PHP file to highlight and/or export
                         (will read from piped input if file not given)
  [-F|--font]            Font to use for export to png
  [-l|--with-line-no]    Highlight with line number
  [-o|--output]          Output filepath where PNG image is exported

Run clish -h to show help.

Examples
  bin/clish --file file.php                           # print
  cat file.php | bin/clish                            # from piped stream
  bin/clish < file.php                                # from redirected stdin
  bin/clish --file file.php --output file.png         # export
  bin/clish --file file.php --output file.png --echo  # print + export
  bin/clish --file file.php --with-line-no            # print with lineno
  bin/clish -f file.php -o file.png -F dejavu         # export in dejavu font

Programatically

You can either highlight PHP code in terminal output or export to png image.

Highlight

use Ahc\CliSyntax\Highlighter;

// PHP code
echo new Highlighter('<?php echo "Hello world!";');
// OR
echo (new Highlighter)->highlight('<?php echo "Hello world!";', $options);

// PHP file
echo Highlighter::for('/path/to/file.php', $options);

// $options array is optional and can contain:
[
    'lineNo' => true, // bool
];

Export

use Ahc\CliSyntax\Exporter;

// PHP file
Exporter::for('/path/to/file.php')->export('file.png', $options);

// $options array is optional and can contain:
[
    'lineNo' => true, // bool
    'font'   => 'full/path/of/font.ttf', // str
    'size'   => 'font size', // int
];

See example usage. Here's how the export looks like:

adhocore/cli-syntax

And with line numbers:

Example with line numbers

Customisation

If you would like to change color etc, extend the classes Highlighter and Exporter, then override visit() method which recieves DOMNode.

Contributing

Please check the guide.

LICENSE

© MIT | 2019, Jitendra Adhikari

Credits

This project is bootstrapped by phint and releases managed by please.