Skip to content

Commit

Permalink
feat: add combined styles and inbuilt syntax highlighting themes in t…
Browse files Browse the repository at this point in the history
…he REPL

PR-URL: #2341

---------

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
Snehil-Shah committed Jun 10, 2024
1 parent 0be04fe commit 0856277
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 17 deletions.
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',
'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++ ) {
// 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

1 comment on commit 0856277

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
repl $\color{red}9192/11438$
$\color{green}+80.36\%$
$\color{red}529/718$
$\color{green}+73.68\%$
$\color{red}114/185$
$\color{green}+61.62\%$
$\color{red}9192/11438$
$\color{green}+80.36\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.