Last active
August 10, 2017 09:17
-
-
Save lsongdev/0e54610db7b134c95350aaf0dcc98608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const draw = (src, line, column, n) => { | |
n = n || 3; | |
const lines = src.split('\n'); | |
const start = Math.max(line - n, 0); | |
const end = Math.min(lines.length, line + n); | |
const context = lines.slice(start, end).map((text, i) => { | |
const curr = start + i + 1; | |
const out = `${curr} ${text}`; | |
if(curr === line && column){ | |
out += '\n>' + '-'.repeat(column) + '^'; | |
} | |
return out; | |
}).join('\n'); | |
return context; | |
} | |
console.log(draw(fs.readFileSync('error-context.js', 'utf8'), 7, 24)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/song940/0e54610db7b134c95350aaf0dcc98608#file-error-context-js-L3
draw接受 file 怎么样