@danielkalen/print-code

1.0.3 • Public • Published

Build Status

print-code

Print visualized slice of code from its content, line and column for CLI.

Install

$ npm install print-code --save

Usage

var code = require('print-code');
code(content)
  .highlight(4)
  .slice(1, 6)
  .max_columns(68)
  .arrow_mark(4, 10)
  .print()

And then the output might be (Line No 4 is red):

    1| 'use strict';
    2|
    3| module.exports = code
    4| code.Code = Code
----------------^ column: 10
    5|

.slice([from] [, to])

Specify which lines should be printed.

  • from Number= The line number begins to be printed, and the value will be parseInt()ed. If not specified, it will print all lines. If
  • to Number= If not specified, it will only print the line of from
code(content).slice().print();       // print all code
code(content).slice(10).print();     // print line 10
code(content).slice(10, 20).print(); // print lines from [10, 20)

Special cases:

code(content).slice(-1, 10).print(); // will print lines from [0, 10)
code(content).slice(0, -1).print();  // will print lines except for the last line.
code(content).slice(10, 9);          // will only print line 10
code(content).slice(10, 11);         // will print line 10

.highlight(line [, ...])

Specify which line number(s) should be highlighted.

  • line Number The number of code line, starts from 1

.max_columns(max)

Limit the max columns to be printed.

  • max Number= default to the max columns of the current terminal.

.arrow_mark(line, column)

Set a mark, and draw an arrow mark to the target coordinate (line, column).

.get()

Get the formated result content.

.print()

console.log the formated result content.

License

MIT

Package Sidebar

Install

npm i @danielkalen/print-code

Weekly Downloads

2

Version

1.0.3

License

MIT

Last publish

Collaborators

  • danielkalen