index.js
function preview (source, line, options) {
  let from, to, pos

  // set from/to line
  if (Array.isArray(line)) {
    from = line[0] | 0
    to = line[1] | 0 || from
  } else if (typeof line === 'object') {
    from = to = line.line | 0
    pos = {
      line: line.line | 0,
      column: line.column | 0
    }
  } else {
    from = to = line | 0
  }

  // set options
  options = Object.assign({}, PREVIEW_OPTS, options)

  let color = colorer(options.cliColor)

  // read source by from/to
  let lines = readSource(source, from, to, options.offset, options.delimiter)
  let numberWidth = String(to).length + 4 // [] + two space

  if (!options.lineNumber) {
    numberWidth = 0
  }

  let parts = lines.map(line => {
    let prefix = ''
    let text = ''

    if (options.lineNumber) {
      prefix = rightPad(`[${line.number}]`, numberWidth)
    }

    if (line.number >= from && line.number <= to) {
      text = color('red', `${prefix}${line.source}`)
    } else {
      text = color('grey', prefix) + line.source
    }

    if (pos && pos.line === line.number) {
      text += '\n' + ' '.repeat(numberWidth + pos.column - 1) + '^'
    }

    return text
  })

  // add delimiter
  parts.unshift(color('grey', DELIMITER))
  parts.push(color('grey', DELIMITER))

  return parts.join('\n')
}
function preview (source, line, options) {
  let from, to, pos

  // set from/to line
  if (Array.isArray(line)) {
    from = line[0] | 0
    to = line[1] | 0 || from
  } else if (typeof line === 'object') {
    from = to = line.line | 0
    pos = {
      line: line.line | 0,
      column: line.column | 0
    }
  } else {
    from = to = line | 0
  }

  // set options
  options = Object.assign({}, PREVIEW_OPTS, options)

  let color = colorer(options.cliColor)

  // read source by from/to
  let lines = readSource(source, from, to, options.offset, options.delimiter)
  let numberWidth = String(to).length + 4 // [] + two space

  if (!options.lineNumber) {
    numberWidth = 0
  }

  let parts = lines.map(line => {
    let prefix = ''
    let text = ''

    if (options.lineNumber) {
      prefix = rightPad(`[${line.number}]`, numberWidth)
    }

    if (line.number >= from && line.number <= to) {
      text = color('red', `${prefix}${line.source}`)
    } else {
      text = color('grey', prefix) + line.source
    }

    if (pos && pos.line === line.number) {
      text += '\n' + ' '.repeat(numberWidth + pos.column - 1) + '^'
    }

    return text
  })

  // add delimiter
  parts.unshift(color('grey', DELIMITER))
  parts.push(color('grey', DELIMITER))

  return parts.join('\n')
}
function preview ( source , line , options ) { let from , to , pos // set from/to line if ( Array . isArray ( line ) ) { from = line [ 0 ] | 0 to = line [ 1 ] | 0 || from } else if ( typeof line === 'object' ) { from = to = line . line | 0 pos = { line : line . line | 0 , column : line . column | 0 } } else { from = to = line | 0 } // set options options = Object . assign ( { } , PREVIEW_OPTS , options ) let color = colorer ( options . cliColor ) // read source by from/to let lines = readSource ( source , from , to , options . offset , options . delimiter ) let numberWidth = String ( to ) . length + 4 // [] + two space if ( ! options . lineNumber ) { numberWidth = 0 } let parts = lines . map ( line => { let prefix = '' let text = '' if ( options . lineNumber ) { prefix = rightPad ( ` ${ line . number } ` , numberWidth ) } if ( line . number >= from && line . number <= to ) { text = color ( 'red' , ` ${ prefix } ${ line . source } ` ) } else { text = color ( 'grey' , prefix ) + line . source } if ( pos && pos . line === line . number ) { text += '\n' + ' ' . repeat ( numberWidth + pos . column - 1 ) + '^' } return text } ) // add delimiter parts . unshift ( color ( 'grey' , DELIMITER ) ) parts . push ( color ( 'grey' , DELIMITER ) ) return parts . join ( '\n' ) }
Preview parts of source code

line:

- Array: `[ Number, Number ]`, the line range to preview
- Object: `{ line: Number, column: Number }`, preivew specified line/column
- Number: preivew single line

options:

- offset: the extra lines number before/after specified line range (default: 5)
- lineNumber: show line number or not (default: true)
- delimiter: line delimiter (default: '\n')
- cliColor: show ASCI CLI color (default: true)

@param  {String} source  Source code
@param  {Mixed}  line    Line number to preivew
@param  {Object} options Options (optional)
@return {String} source parts
Preview parts of source code