You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.5 KiB

3 months ago
  1. # cli-highlight
  2. > Syntax highlighting in your terminal
  3. [![npm](https://img.shields.io/npm/v/cli-highlight.svg)](https://www.npmjs.com/package/cli-highlight)
  4. [![downloads](https://img.shields.io/npm/dm/cli-highlight.svg)](https://www.npmjs.com/package/cli-highlight)
  5. [![CI status](https://github.com/felixfbecker/cli-highlight/workflows/build/badge.svg?branch=main)](https://github.com/felixfbecker/cli-highlight/actions)
  6. [![codecov](https://codecov.io/gh/felixfbecker/cli-highlight/branch/main/graph/badge.svg)](https://codecov.io/gh/felixfbecker/cli-highlight)
  7. ![node](http://img.shields.io/node/v/cli-highlight.svg)
  8. [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  9. [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
  10. [![license](https://img.shields.io/npm/l/cli-highlight.svg)](https://github.com/felixfbecker/cli-highlight/blob/main/LICENSE.txt)
  11. ## Example
  12. ![Example Output](media/screenshot.svg)
  13. ## CLI Usage
  14. Output a file
  15. ```sh
  16. $ highlight package.json
  17. ```
  18. Color output of another program with piping. Example: A database migration script that logs SQL Queries
  19. ```sh
  20. $ db-migrate --dry-run | highlight
  21. ```
  22. Command line options:
  23. ```html
  24. Usage: highlight [options] [file]
  25. Outputs a file or STDIN input with syntax highlighting
  26. Options:
  27. --language, -l Set the langugage explicitely
  28. If omitted will try to auto-detect
  29. --theme, -t Use a theme defined in a JSON file
  30. --version, -v Show version number [boolean]
  31. --help, -h Show help [boolean]
  32. ```
  33. ## Programmatic Usage
  34. You can use this module programmatically to highlight logs of your Node app. Example:
  35. ```js
  36. const highlight = require('cli-highlight').highlight
  37. const Sequelize = require('sequelize')
  38. const db = new Sequelize(process.env.DB, {
  39. logging(log) {
  40. console.log(highlight(log, {language: 'sql', ignoreIllegals: true}))
  41. }
  42. })
  43. ```
  44. Detailed API documenation can be found [here](http://cli-highlight.surge.sh/).
  45. ## Themes
  46. You can write your own theme in a JSON file and pass it with `--theme`.
  47. The key must be one of the [highlight.js CSS class names](http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html) or `"default"`,
  48. and the value must be one or an array of [Chalk styles](https://github.com/chalk/chalk#styles) to be applied to that token.
  49. ```json
  50. {
  51. "keyword": "blue",
  52. "built_in": ["cyan", "dim"],
  53. "string": "red",
  54. "default": "gray"
  55. }
  56. ```
  57. 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.
  58. The theme is combined with the [default theme](http://cli-highlight.surge.sh/globals.html#default_theme).
  59. The default theme is still not colored a lot or optimized for many languages, PRs welcome!
  60. ## Supported Languages
  61. [All languages of highlight.js](https://highlightjs.org/static/demo/) are supported.
  62. Check a [CI build](https://travis-ci.org/felixfbecker/cli-highlight) for examples of all the different languages and their highlighting.
  63. ## Contributing
  64. The module is written in TypeScript and can be compiled with `npm run build`.
  65. `npm run watch` starts `tsc` in watch mode. Tests are written with mocha.
  66. Improving language support is done by adding more colors to the tokens in the default theme and writing more tests.