Skip to content

Commit 0856277

Browse files
authored
feat: add combined styles and inbuilt syntax highlighting themes in the 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>
1 parent 0be04fe commit 0856277

File tree

3 files changed

+195
-17
lines changed

3 files changed

+195
-17
lines changed

lib/node_modules/@stdlib/repl/lib/ansi_colors.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ var ANSI = {
4848
'brightCyan': '\u001b[96m',
4949
'brightWhite': '\u001b[97m',
5050

51+
// Background colors:
52+
'bgBlack': '\u001b[40m',
53+
'bgRed': '\u001b[41m',
54+
'bgGreen': '\u001b[42m',
55+
'bgYellow': '\u001b[43m',
56+
'bgBlue': '\u001b[44m',
57+
'bgMagenta': '\u001b[45m',
58+
'bgCyan': '\u001b[46m',
59+
'bgWhite': '\u001b[47m',
60+
61+
// Bright background colors:
62+
'bgBrightBlack': '\u001b[100m',
63+
'bgBrightRed': '\u001b[101m',
64+
'bgBrightGreen': '\u001b[102m',
65+
'bgBrightYellow': '\u001b[103m',
66+
'bgBrightBlue': '\u001b[104m',
67+
'bgBrightMagenta': '\u001b[105m',
68+
'bgBrightCyan': '\u001b[106m',
69+
'bgBrightWhite': '\u001b[107m',
70+
71+
// Styles:
72+
'bold': '\u001b[1m',
73+
'underline': '\u001b[4m',
74+
'reversed': '\u001b[7m',
75+
'italic': '\u001b[3m',
76+
'strikethrough': '\u001b[9m',
77+
5178
// Reset colors:
5279
'reset': '\u001b[0m'
5380
};

lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,35 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, '_highlightLine', functio
115115
var highlightedLine = '';
116116
var resetCode = ANSI[ 'reset' ];
117117
var colorCode;
118-
var colors = this._themes[ this._theme ];
119118
var offset = 0;
119+
var color;
120+
var theme = this._themes[ this._theme ];
120121
var token;
121122
var i;
123+
var j;
122124

123125
// Sort and traverse the tokens...
124126
tokens.sort( tokenComparator );
125127
for ( i = 0; i < tokens.length; i++ ) {
126128
token = tokens[ i ];
127-
colorCode = ANSI[ colors[ token.type ] ];
128-
129-
// Highlight token if it's color exists in the theme...
130-
if ( colorCode ) {
131-
highlightedLine += line.slice( offset, token.start ); // add text before token
132-
highlightedLine += colorCode; // insert colorCode
133-
highlightedLine += line.slice( token.start, token.end ); // add token
134-
highlightedLine += resetCode; // reset color
135-
offset = token.end;
129+
color = theme[ token.type ];
130+
if ( !color ) {
131+
continue; // no color defined for the token type
136132
}
133+
color = color.split( ' ' ); // allow combination of styles. eg: `bold magenta`
134+
highlightedLine += line.slice( offset, token.start ); // add text before token
135+
for ( j = 0; j < color.length; j++ ) {
136+
// Highlight token if it's color is supported...
137+
colorCode = ANSI[ color[ j ] ];
138+
if ( colorCode ) {
139+
highlightedLine += colorCode; // insert colorCode
140+
}
141+
}
142+
highlightedLine += line.slice( token.start, token.end ); // add token
143+
highlightedLine += resetCode; // reset color
144+
offset = token.end;
137145
}
138146
highlightedLine += line.slice( offset ); // add remaining text
139-
140147
return highlightedLine;
141148
});
142149

lib/node_modules/@stdlib/repl/lib/themes.js

Lines changed: 150 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,172 @@
2929
*/
3030
var THEMES = {
3131
'stdlib-ansi-basic': {
32+
// Keywords:
33+
'control': 'brightMagenta',
34+
'keyword': 'bold brightBlue',
35+
'specialIdentifier': 'cyan',
36+
37+
// Literals:
38+
'string': 'green',
39+
'number': 'yellow',
40+
'literal': 'italic brightCyan',
41+
'regexp': 'underline brightRed',
42+
43+
// Identifiers:
44+
'command': 'bold magenta',
45+
'function': 'bold yellow',
46+
'object': 'cyan',
47+
'variable': null,
48+
'name': null,
49+
50+
// Others:
51+
'comment': 'brightBlack',
52+
'punctuation': null,
53+
'operator': null
54+
},
55+
'stdlib-ansi-dark': {
56+
// Keywords:
57+
'control': 'bold brightMagenta',
58+
'keyword': 'green',
59+
'specialIdentifier': 'cyan',
60+
61+
// Literals:
62+
'string': 'yellow',
63+
'number': 'brightBlue',
64+
'literal': 'italic brightCyan',
65+
'regexp': 'bold red',
66+
67+
// Identifiers:
68+
'command': 'brightGreen',
69+
'function': 'bold magenta',
70+
'object': 'cyan',
71+
'variable': null,
72+
'name': null,
73+
74+
// Others:
75+
'comment': 'bold brightBlack',
76+
'punctuation': null,
77+
'operator': null
78+
},
79+
'stdlib-ansi-light': {
3280
// Keywords:
3381
'control': 'magenta',
3482
'keyword': 'blue',
35-
'specialIdentifier': 'cyan',
83+
'specialIdentifier': 'bold cyan',
3684

3785
// Literals:
38-
'string': 'brightYellow',
39-
'number': 'brightGreen',
40-
'literal': 'brightBlue',
41-
'regexp': 'red',
86+
'string': 'yellow',
87+
'number': 'green',
88+
'literal': 'italic brightBlue',
89+
'regexp': 'underline red',
4290

4391
// Identifiers:
4492
'command': 'brightMagenta',
4593
'function': 'yellow',
46-
'object': 'brightCyan',
94+
'object': 'bold',
4795
'variable': null,
4896
'name': null,
4997

5098
// Others:
5199
'comment': 'brightBlack',
52100
'punctuation': null,
53101
'operator': null
102+
},
103+
'stdlib-ansi-strong': {
104+
// Keywords:
105+
'control': 'bold magenta',
106+
'keyword': 'bold blue',
107+
'specialIdentifier': 'italic cyan',
108+
109+
// Literals:
110+
'string': 'bold brightGreen',
111+
'number': 'bold brightYellow',
112+
'literal': 'italic brightBlue',
113+
'regexp': 'underline red',
114+
115+
// Identifiers:
116+
'command': 'underline brightBlue',
117+
'function': 'bold yellow',
118+
'object': 'italic cyan',
119+
'variable': 'green',
120+
'name': null,
121+
122+
// Others:
123+
'comment': 'italic brightBlack',
124+
'punctuation': null,
125+
'operator': null
126+
},
127+
'minimalist': {
128+
// Keywords:
129+
'control': 'underline',
130+
'keyword': 'bold',
131+
'specialIdentifier': 'italic',
132+
133+
// Literals:
134+
'string': 'underline',
135+
'number': 'bold',
136+
'literal': 'italic',
137+
'regexp': 'bold underline',
138+
139+
// Identifiers:
140+
'command': 'underline bold',
141+
'function': 'italic bold',
142+
'object': 'italic',
143+
'variable': null,
144+
'name': null,
145+
146+
// Others:
147+
'comment': 'italic brightBlack',
148+
'punctuation': null,
149+
'operator': null
150+
},
151+
'solarized': {
152+
// Keywords:
153+
'control': 'green',
154+
'keyword': 'bold',
155+
'specialIdentifier': 'red',
156+
157+
// Literals:
158+
'string': 'cyan',
159+
'number': 'brightMagenta',
160+
'literal': 'brightYellow',
161+
'regexp': 'red',
162+
163+
// Identifiers:
164+
'command': 'bold magenta',
165+
'function': 'brightBlue',
166+
'object': 'brightRed',
167+
'variable': 'brightBlue',
168+
'name': null,
169+
170+
// Others:
171+
'comment': 'italic brightBlack',
172+
'punctuation': null,
173+
'operator': 'green'
174+
},
175+
'monokai': {
176+
// Keywords:
177+
'control': 'brightRed',
178+
'keyword': 'italic brightCyan',
179+
'specialIdentifier': 'brightMagenta',
180+
181+
// Literals:
182+
'string': 'brightYellow',
183+
'number': 'brightBlue',
184+
'literal': 'brightBlue',
185+
'regexp': 'underline yellow',
186+
187+
// Identifiers:
188+
'command': 'bold brightGreen',
189+
'function': 'brightGreen',
190+
'object': 'italic brightMagenta',
191+
'variable': null,
192+
'name': null,
193+
194+
// Others:
195+
'comment': 'brightBlack',
196+
'punctuation': null,
197+
'operator': 'brightRed'
54198
}
55199
};
56200

0 commit comments

Comments
 (0)