Socket
Socket
Sign inDemoInstall

cli-highlight

Package Overview
Dependencies
27
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cli-highlight

Syntax highlighting in your terminal


Version published
Weekly downloads
2.6M
increased by1.47%
Maintainers
1
Install size
3.03 MB
Created
Weekly downloads
 

Package description

What is cli-highlight?

The cli-highlight npm package is a syntax highlighter for the command line. It takes code as input and outputs it with syntax highlighting, making it easier to read and understand. It supports a wide range of programming languages and can be used both as a command-line tool and as a library in Node.js applications.

What are cli-highlight's main functionalities?

Syntax highlighting in the terminal

This feature allows you to highlight syntax of code snippets directly in the terminal. The code sample demonstrates how to use the highlight function from the cli-highlight package to syntax highlight a JavaScript code snippet.

const highlight = require('cli-highlight').highlight;
console.log(highlight('const x = 1;', {language: 'javascript', theme: 'default'}));

Highlight code from a file

This feature allows you to read code from a file and output it with syntax highlighting in the terminal. The code sample shows how to read a JavaScript file and use the highlight function to apply syntax highlighting.

const highlight = require('cli-highlight').highlight;
const fs = require('fs');

fs.readFile('example.js', 'utf8', (err, code) => {
  if (err) throw err;
  console.log(highlight(code, {language: 'javascript', theme: 'default'}));
});

Use as a command-line tool

cli-highlight can also be used as a standalone command-line tool. This code sample demonstrates how to use cli-highlight to highlight a JavaScript file named 'example.js' using the default theme.

highlight -l javascript -t default example.js

Other packages similar to cli-highlight

Readme

Source

cli-highlight

Syntax highlighting in your terminal

npm downloads CI status codecov node code style: prettier semantic-release license

Example

Example Output

CLI Usage

Output a file

$ highlight package.json

Color output of another program with piping. Example: A database migration script that logs SQL Queries

$ db-migrate --dry-run | highlight

Command line options:

Usage: highlight [options] [file]

Outputs a file or STDIN input with syntax highlighting

Options:
  --language, -l  Set the langugage explicitely
                  If omitted will try to auto-detect
  --theme, -t     Use a theme defined in a JSON file
  --version, -v   Show version number                                   [boolean]
  --help, -h      Show help                                             [boolean]

Programmatic Usage

You can use this module programmatically to highlight logs of your Node app. Example:

const highlight = require('cli-highlight').highlight
const Sequelize = require('sequelize')

const db = new Sequelize(process.env.DB, {
  logging(log) {
    console.log(highlight(log, {language: 'sql', ignoreIllegals: true}))
  }
})

Detailed API documenation can be found here.

Themes

You can write your own theme in a JSON file and pass it with --theme. The key must be one of the highlight.js CSS class names or "default", and the value must be one or an array of Chalk styles to be applied to that token.

{
  "keyword": "blue",
  "built_in": ["cyan", "dim"],
  "string": "red",
  "default": "gray"
}

The style for "default" will be applied to any substrings not handled by highlight.js. The specifics depend on the language but this typically includes things like commas in parameter lists, semicolons at the end of lines, etc.

The theme is combined with the default theme. The default theme is still not colored a lot or optimized for many languages, PRs welcome!

Supported Languages

All languages of highlight.js are supported. Check a CI build for examples of all the different languages and their highlighting.

Contributing

The module is written in TypeScript and can be compiled with npm run build. npm run watch starts tsc in watch mode. Tests are written with mocha.

Improving language support is done by adding more colors to the tokens in the default theme and writing more tests.

Keywords

FAQs

Last updated on 28 Mar 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc