From f0c351b0308fcb8dddc8fcd06bf32f693478dec3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 10 Jun 2024 21:39:01 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 4 +- lib/ansi_colors.js | 27 +++++++ lib/syntax_highlighter.js | 29 ++++--- lib/themes.js | 156 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 198 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a981549..1090c29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-06-09) +## Unreleased (2024-06-10)
@@ -20,6 +20,7 @@ ##### Features +- [`0856277`](https://github.com/stdlib-js/stdlib/commit/0856277523259bf111501ff87d54104b361b0fa3) - add combined styles and inbuilt syntax highlighting themes in the REPL - [`b4c12b7`](https://github.com/stdlib-js/stdlib/commit/b4c12b7c4a76cfa71164d1b01fcbfca0426abbb3) - add APIs, commands, and tests for REPL syntax-highlighting [(#2291)](https://github.com/stdlib-js/stdlib/pull/2291) - [`24f4a8f`](https://github.com/stdlib-js/stdlib/commit/24f4a8f24c08dd25686afc4cfb78be2e0045e844) - add syntax highlighting in the REPL - [`0f9acd1`](https://github.com/stdlib-js/stdlib/commit/0f9acd17de012dfe755c98b602d6bb3dbe1e8117) - add `BooleanArray` to namespace @@ -257,6 +258,7 @@ A total of 10 people contributed to this release. Thank you to the following con
+- [`0856277`](https://github.com/stdlib-js/stdlib/commit/0856277523259bf111501ff87d54104b361b0fa3) - **feat:** add combined styles and inbuilt syntax highlighting themes in the REPL _(by Snehil Shah)_ - [`9f3dcaf`](https://github.com/stdlib-js/stdlib/commit/9f3dcaf4d19fde9e7066f7dc12a49cf87e6fd0f7) - **fix:** update error message _(by Athan Reines)_ - [`b4c12b7`](https://github.com/stdlib-js/stdlib/commit/b4c12b7c4a76cfa71164d1b01fcbfca0426abbb3) - **feat:** add APIs, commands, and tests for REPL syntax-highlighting [(#2291)](https://github.com/stdlib-js/stdlib/pull/2291) _(by Snehil Shah, Athan Reines)_ - [`f85ed2a`](https://github.com/stdlib-js/stdlib/commit/f85ed2aafc393cfbac360ad14b97af0ee28d450b) - **docs:** update REPL namespace documentation [(#2313)](https://github.com/stdlib-js/stdlib/pull/2313) _(by stdlib-bot, Athan Reines)_ diff --git a/lib/ansi_colors.js b/lib/ansi_colors.js index 1df33d1..ac490f9 100644 --- a/lib/ansi_colors.js +++ b/lib/ansi_colors.js @@ -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' }; diff --git a/lib/syntax_highlighter.js b/lib/syntax_highlighter.js index f055c88..f314d49 100644 --- a/lib/syntax_highlighter.js +++ b/lib/syntax_highlighter.js @@ -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; }); diff --git a/lib/themes.js b/lib/themes.js index e49e4f1..63edd47 100644 --- a/lib/themes.js +++ b/lib/themes.js @@ -29,21 +29,69 @@ */ 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, @@ -51,6 +99,102 @@ var THEMES = { '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' } };