Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const sheet = (chalkInstance) => ({
formula: chalkInstance.inverse
});

module.exports = (stream) => {
module.exports = (stream, language) => {
language = language || 'js';
let level = 0;
if (stream.isTTY) {
if (stream.getColorDepth() >= 4) level = 1;
Expand All @@ -49,7 +50,7 @@ module.exports = (stream) => {
}
const chalkInstance = new chalk.Instance({ level });
const colorSheet = sheet(chalkInstance);
const highlight = (s) => emphasize.highlight('js', s, colorSheet).value;
const highlight = (s) => emphasize.highlight(language, s, colorSheet).value;
highlight.colorizeMatchingBracket = (s) => chalkInstance.bgBlue(s);
return highlight;
};
5 changes: 3 additions & 2 deletions lib/pretty-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class PrettyREPLServer extends repl.REPLServer {
constructor(options = {}) {
super(options);
options.output = options.output || process.stdout;
this.colorize = (options && options.colorize) || highlight(options.output);
options.language = options.language || 'js';
this.colorize = (options && options.colorize) || highlight(options.output, options.language);
this.colorizeMatchingBracket = (options && options.colorizeMatchingBracket) ||
highlight(options.output).colorizeMatchingBracket;
highlight(options.output, options.language).colorizeMatchingBracket;
this.lineBeforeInsert = undefined;
this.highlightBracketPosition = -1;

Expand Down
25 changes: 25 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,28 @@ it('full pass-through test', (done) => {
});
input.write('let foo = 12\n');
});

it('highlights typescript', (done) => {
process.stdout.isTTY = undefined;
const input = new PassThrough();
const output = new PassThrough();
output.isTTY = true;
output.getColorDepth = () => 8;
repl.start({
prompt: 'test-prompt > ',
input: input,
output: output,
terminal: true,
language: 'ts'
});
let out = '';
output.setEncoding('utf8').on('data', data => {
out += data;
if (out.endsWith('\n') && !out.startsWith('<done>')) {
assert.equal(out, '\x1B[1G\x1B[0Jtest-prompt > \x1B[15Gtyp\b\b\b\x1B[36mtype\x1B[39m metastatic = \x1B[32m"\x1B[39m\x1B[32mf\x1B[39m\x1B[32mo\x1B[39m\x1B[32mo\x1B[39m\x1B[32m"\x1B[39m|\x1B[32m"\x1B[39m\x1B[32mb\x1B[39m\x1B[32ma\x1B[39m\x1B[32mr\x1B[39m\x1B[32m"\x1B[39m|\x1B[32m"\x1B[39m\x1B[32mb\x1B[39m\x1B[32ma\x1B[39m\x1B[32mz\x1B[39m\x1B[32m"\x1B[39m\r\n');
out = '<done>';
done();
}
});
input.write('type metastatic = "foo"|"bar"|"baz"\n');
});
Loading