Understanding cli-highlight: The Ultimate Highlighting Tool for Your Command Line Interface
The cli-highlight
package is an essential tool for developers who work extensively on the command line. It provides robust syntax highlighting for various programming languages directly in your terminal.
Installation
To install cli-highlight
, use npm:
npm install -g cli-highlight
Basic Usage
To highlight code from a file:
highlight < yourfile.js
To highlight code from standard input:
cat yourfile.js | highlight
Available Languages
The following languages are supported:
- JavaScript
- Python
- Ruby
- Go
- Java
- PHP
Advanced API Examples
Utilize various options to modify the appearance of the highlighted code:
To set the theme:
highlight --style=monokai < yourfile.js
To use a specific language mode:
highlight --language=javascript < yourfile.js
Embedding highlight in a project:
const highlight = require('cli-highlight').highlight;
const code = 'const x = 10;';
console.log(highlight(code, { language: 'javascript', theme: 'monokai' }));
Creating a Custom Command Line Tool
Let's put it all together in a small example application:
const { highlight } = require('cli-highlight');
const fs = require('fs');
const file = process.argv[2];
const language = process.argv[3] || 'javascript';
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
return console.error(err);
}
console.log(highlight(data, { language }));
});
Save this code into a file named cli-highlight-app.js
and run it using:
node cli-highlight-app.js yourfile.js
This command will highlight the code from yourfile.js
using the default language or one specified in the command line arguments.
With cli-highlight
, you can enhance the readability and maintainability of code snippets right in your terminal. Happy coding!
Hash: 74d733bab3a8fdea9b4ed1bbd86e1fa91d9fa088c6c059d73e416abbaa40b793