Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// sudo easy_install -U Pygments
// alias c='pygmentize -O style=monokai -f console256 -g'
// Performance-wise they appear to be pretty much the same.
const fs = require('fs');
const emphasize = require('emphasize');
const filePath = process.argv[2];
if (!filePath) {
console.error('USAGE: c filename.ext');
process.exit(1);
}
const doc = fs.readFileSync(filePath, 'utf8');
const output = emphasize.highlightAuto(doc).value;
process.stdout.write(output);
printData(response) {
if (! response.data) {
return print(chalk.gray(''));
}
// Handle pretty-printing common types of content (by header).
if (response.headers && response.headers['content-type']) {
let type = response.headers['content-type'];
if (type.includes('application/json')) {
return printJson(response.data);
} else if (type.includes('text/html') || type.includes('application/xml')) {
return print(emphasize.highlight('xml', response.data).value);
} else if (type.includes('text/plain')) {
return print(response.data);
}
}
// ...or try to guess!
return print(emphasize.highlightAuto(response.data).value);
}
}
const highlightSheet: Sheet = {
keyword: c.magenta,
built_in: c.cyan.italic,
literal: c.cyan,
number: c.yellow,
regexp: c.red,
string: c.green,
symbol: c.cyan,
class: c.yellow.italic,
attr: c.cyan,
comment: c.gray
}
/* eslint-enable @typescript-eslint/camelcase */
return [
emphasize.highlight(
'js',
output,
highlightSheet
).value,
cursor
]
})
}
const stringToAnsi = exports.stringToAnsi = ( str, language ) => {
// accept case-insensitive language
language = _.isEmpty( language ) ? null : language.toLowerCase();
// if `language` is not supported, return `str` back without syntax highlighting
if( ! _.includes( _.values( LANGUAGES ), language ) ) {
return str;
}
// highlight `str` using `emphasize` package in ANSI color format
const { value } = emphasize.highlight( language, str );
// return ANSI formatted string
return value;
};
// Handle pretty-printing common types of content (by header).
if (response.headers && response.headers['content-type']) {
let type = response.headers['content-type'];
if (type.includes('application/json')) {
return printJson(response.data);
} else if (type.includes('text/html') || type.includes('application/xml')) {
return print(emphasize.highlight('xml', response.data).value);
} else if (type.includes('text/plain')) {
return print(response.data);
}
}
// ...or try to guess!
return print(emphasize.highlightAuto(response.data).value);
}
}
const highlightSyntax = (txt: string, lang?: string): string => {
if (lang) {
try {
return emphasize.highlight(lang, txt).value;
} catch (e) {
// fallback for unknown languages...
}
}
return emphasize.highlightAuto(txt).value;
};
var log_snippet = function (lines, code, filepath, file_ext, nocolors) {
if (nocolors) {
console.log(_.zip(lines, code.split("\n"))
.map(function (s) { return s.join(" "); }).join("\n"));
}
else {
var colored = void 0;
try {
colored = _.get(emphasize
.highlight(file_ext, code), "value", "");
}
catch (e) {
log.warn("highlighting failed for " + filepath + ":");
colored = code;
}
var colored_lines = lines.map(function (l) { return chalk.gray(l); });
console.log(_.zip(colored_lines, colored.split("\n"))
.map(function (s) { return s.join(""); }).join("\n"));
}
};
var to_console_snippet = function (issue, nocolors) {
const log_snippet = (
lines : any[],
code : string,
filepath : string,
file_ext : string,
nocolors : boolean
) : void => {
if (nocolors) {
console.log(_.zip(lines, code.split("\n"))
.map((s : any[]) => s.join(" ")).join("\n"))
} else {
let colored : string
try {
colored = _.get(emphasize
.highlight(file_ext, code), "value", "")
} catch (e) {
log.warn(`highlighting failed for ${filepath}:`)
colored = code
}
const colored_lines = lines.map((l) => chalk.gray(l))
console.log(_.zip(colored_lines, colored.split("\n"))
.map((s : any[]) => s.join("")).join("\n"))
}
}
export function highlight(language: string, value: string): string {
return mark.reset(emphasize.highlight(language, value, sheet).value);
}
function renderPrompt(): void {
const cols = stdout.columns || Infinity
const completion = scroller && menuItems.length > 0 ?
menuItems[scroller.selected === -1 ? 0 : scroller.selected][0] :
''
const lineHighlightTime = time('lineHighlight')
const highlightedLine =
emphasize.highlight('js', rl.line, highlightSheet).value
debug(lineHighlightTime())
const slice = (start: number, end?: number) =>
sliceAnsi(highlightedLine, start, end)
const beforeCursor = (() => {
const sliced = slice(0, rl.cursor)
if (menuItems.length === 0) {
return sliced
}
if (
((scroller && scroller.selected === -1) || !scroller) &&
!stopping
) {