Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add combined styles and inbuilt syntax highlighting themes in the REPL #2341

Merged
merged 2 commits into from
Jun 10, 2024
Merged
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
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/repl/lib/ansi_colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ var ANSI = {
'brightCyan': '\u001b[96m',
'brightWhite': '\u001b[97m',

// Background colors:
'bgBlack': '\u001b[40m',
kgryte marked this conversation as resolved.
Show resolved Hide resolved
'bgRed': '\u001b[41m',
'bgGreen': '\u001b[42m',
'bgYellow': '\u001b[43m',
'bgBlue': '\u001b[44m',
'bgMagenta': '\u001b[45m',
'bgCyan': '\u001b[46m',
'bgWhite': '\u001b[47m',

// Bright background colors:
'bgBrightBlack': '\u001b[100m',
'bgBrightRed': '\u001b[101m',
'bgBrightGreen': '\u001b[102m',
'bgBrightYellow': '\u001b[103m',
'bgBrightBlue': '\u001b[104m',
'bgBrightMagenta': '\u001b[105m',
'bgBrightCyan': '\u001b[106m',
'bgBrightWhite': '\u001b[107m',

// Styles:
'bold': '\u001b[1m',
'underline': '\u001b[4m',
'reversed': '\u001b[7m',
'italic': '\u001b[3m',
'strikethrough': '\u001b[9m',

// Reset colors:
'reset': '\u001b[0m'
};
Expand Down
29 changes: 18 additions & 11 deletions lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,35 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, '_highlightLine', functio
var highlightedLine = '';
var resetCode = ANSI[ 'reset' ];
var colorCode;
var colors = this._themes[ this._theme ];
var offset = 0;
var color;
var theme = this._themes[ this._theme ];
var token;
var i;
var j;

// Sort and traverse the tokens...
tokens.sort( tokenComparator );
for ( i = 0; i < tokens.length; i++ ) {
token = tokens[ i ];
colorCode = ANSI[ colors[ token.type ] ];

// Highlight token if it's color exists in the theme...
if ( colorCode ) {
highlightedLine += line.slice( offset, token.start ); // add text before token
highlightedLine += colorCode; // insert colorCode
highlightedLine += line.slice( token.start, token.end ); // add token
highlightedLine += resetCode; // reset color
offset = token.end;
color = theme[ token.type ];
if ( !color ) {
continue; // no color defined for the token type
}
color = color.split( ' ' ); // allow combination of styles. eg: `bold magenta`
highlightedLine += line.slice( offset, token.start ); // add text before token
for ( j = 0; j < color.length; j++ ) {
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// Highlight token if it's color is supported...
colorCode = ANSI[ color[ j ] ];
if ( colorCode ) {
highlightedLine += colorCode; // insert colorCode
}
}
highlightedLine += line.slice( token.start, token.end ); // add token
highlightedLine += resetCode; // reset color
offset = token.end;
}
highlightedLine += line.slice( offset ); // add remaining text

return highlightedLine;
});

Expand Down
156 changes: 150 additions & 6 deletions lib/node_modules/@stdlib/repl/lib/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,172 @@
*/
var THEMES = {
'stdlib-ansi-basic': {
// Keywords:
'control': 'brightMagenta',
'keyword': 'bold brightBlue',
'specialIdentifier': 'cyan',

// Literals:
'string': 'green',
'number': 'yellow',
'literal': 'italic brightCyan',
'regexp': 'underline brightRed',

// Identifiers:
'command': 'bold magenta',
'function': 'bold yellow',
'object': 'cyan',
'variable': null,
'name': null,

// Others:
'comment': 'brightBlack',
'punctuation': null,
'operator': null
},
'stdlib-ansi-dark': {
// Keywords:
'control': 'bold brightMagenta',
'keyword': 'green',
'specialIdentifier': 'cyan',

// Literals:
'string': 'yellow',
'number': 'brightBlue',
'literal': 'italic brightCyan',
'regexp': 'bold red',

// Identifiers:
'command': 'brightGreen',
'function': 'bold magenta',
'object': 'cyan',
'variable': null,
'name': null,

// Others:
'comment': 'bold brightBlack',
'punctuation': null,
'operator': null
},
'stdlib-ansi-light': {
// Keywords:
'control': 'magenta',
'keyword': 'blue',
'specialIdentifier': 'cyan',
'specialIdentifier': 'bold cyan',

// Literals:
'string': 'brightYellow',
'number': 'brightGreen',
'literal': 'brightBlue',
'regexp': 'red',
'string': 'yellow',
'number': 'green',
'literal': 'italic brightBlue',
'regexp': 'underline red',

// Identifiers:
'command': 'brightMagenta',
'function': 'yellow',
'object': 'brightCyan',
'object': 'bold',
'variable': null,
'name': null,

// Others:
'comment': 'brightBlack',
'punctuation': null,
'operator': null
},
'stdlib-ansi-strong': {
// Keywords:
'control': 'bold magenta',
'keyword': 'bold blue',
'specialIdentifier': 'italic cyan',

// Literals:
'string': 'bold brightGreen',
'number': 'bold brightYellow',
'literal': 'italic brightBlue',
'regexp': 'underline red',

// Identifiers:
'command': 'underline brightBlue',
'function': 'bold yellow',
'object': 'italic cyan',
'variable': 'green',
'name': null,

// Others:
'comment': 'italic brightBlack',
'punctuation': null,
'operator': null
},
'minimalist': {
// Keywords:
'control': 'underline',
'keyword': 'bold',
'specialIdentifier': 'italic',

// Literals:
'string': 'underline',
'number': 'bold',
'literal': 'italic',
'regexp': 'bold underline',

// Identifiers:
'command': 'underline bold',
'function': 'italic bold',
'object': 'italic',
'variable': null,
'name': null,

// Others:
'comment': 'italic brightBlack',
'punctuation': null,
'operator': null
},
'solarized': {
// Keywords:
'control': 'green',
'keyword': 'bold',
'specialIdentifier': 'red',

// Literals:
'string': 'cyan',
'number': 'brightMagenta',
'literal': 'brightYellow',
'regexp': 'red',

// Identifiers:
'command': 'bold magenta',
'function': 'brightBlue',
'object': 'brightRed',
'variable': 'brightBlue',
'name': null,

// Others:
'comment': 'italic brightBlack',
'punctuation': null,
'operator': 'green'
},
'monokai': {
// Keywords:
'control': 'brightRed',
'keyword': 'italic brightCyan',
'specialIdentifier': 'brightMagenta',

// Literals:
'string': 'brightYellow',
'number': 'brightBlue',
'literal': 'brightBlue',
'regexp': 'underline yellow',

// Identifiers:
'command': 'bold brightGreen',
'function': 'brightGreen',
'object': 'italic brightMagenta',
'variable': null,
'name': null,

// Others:
'comment': 'brightBlack',
'punctuation': null,
'operator': 'brightRed'
}
};

Expand Down
Loading