CLI Source Code Preview

1.1.0 · active · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

const preview = require('cli-source-preview');

const source = `
'use strict'

const chalk = require('chalk')

const PREVIEW_OPTS = {
  offset: 5,
  lineNumber: true,
  delimiter: '\n'
}
const DELIMITER = '-'.repeat(40)

function rightPad (text, width) {
  return text + (width > text.length ? ' '.repeat(width - text.length) : '')
}
`;

// Preview line 10
console.log('--- Preview line 10 ---');
console.log(preview(source, 10));

// Preview lines 5 to 8
console.log('\n--- Preview lines 5-8 ---');
console.log(preview(source, [5, 8]));

// Preview line 12, column 6
console.log('\n--- Preview line 12:6 ---');
console.log(preview(source, { line: 12, column: 6}));

view raw JSON →