cli-source-preview is a JavaScript utility designed to render syntax-highlighted source code snippets directly in the command-line interface. It's particularly useful for enhancing error messages or debug outputs by providing relevant code context, complete with line numbers and customizable colorization. The current stable version is 1.1.0. This package offers a focused solution for presenting code in a terminal environment, distinguishing itself by integrating `chalk` for robust ASCII color support. Developers can specify single lines, line ranges, or even precise line/column positions for the preview, with options to control the number of surrounding context lines and the line delimiter. Given its specific utility and the current version, it's maintained to ensure compatibility and address any critical bugs, rather than undergoing frequent feature-driven releases.
npm install cli-source-previewVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install and use `cli-source-preview` to highlight and preview different sections of a source code string, including single lines, line ranges, and specific line/column positions.
Use `const preview = require('cli-source-preview');` in CJS files. For ESM projects, consider using a CJS-compatible wrapper or a bundler that can handle CJS modules.Always validate input line numbers and ranges against the `source` string's actual line count to prevent unexpected behavior.
Rename your file to a `.cjs` extension, or ensure your `package.json` specifies `"type": "commonjs"`. Alternatively, use dynamic `import()` if you need to load a CJS module from an ESM context: `const preview = await import('cli-source-preview');` (note: this still imports the CJS default, which is the `preview` function itself, so `preview.default` might be needed if not destructured).Run `npm install cli-source-preview` in your project directory. If it's installed globally, ensure your Node.js `require` path includes global modules, or install it locally.