22// See LICENSE in the project root for license information.
33
44import { type ITerminalProvider , TerminalProviderSeverity } from './ITerminalProvider' ;
5- import { Colorize , SgrParameterAttribute } from './Colorize' ;
5+ import { Colorize } from './Colorize' ;
66import type { ITerminal , ITerminalWriteOptions , TerminalWriteParameters } from './ITerminal' ;
77import { AnsiEscape } from './AnsiEscape' ;
88
9- /**
10- * Colors used with {@link ILegacyColorableSequence}.
11- */
12- enum ColorValue {
13- Black ,
14- Red ,
15- Green ,
16- Yellow ,
17- Blue ,
18- Magenta ,
19- Cyan ,
20- White ,
21- Gray
22- }
23-
24- /**
25- * Text styles used with {@link ILegacyColorableSequence}.
26- */
27- enum TextAttribute {
28- Bold ,
29- Dim ,
30- Underline ,
31- Blink ,
32- InvertColor ,
33- Hidden
34- }
35-
36- interface ILegacyColorableSequence {
37- text : string ;
38- isEol ?: boolean ;
39- foregroundColor ?: ColorValue ;
40- backgroundColor ?: ColorValue ;
41- textAttributes ?: TextAttribute [ ] ;
42- }
43-
449/**
4510 * This class facilitates writing to a console.
4611 *
4712 * @beta
4813 */
4914export class Terminal implements ITerminal {
50- private _providers : Set < ITerminalProvider > ;
15+ private readonly _providers : Set < ITerminalProvider > ;
5116
5217 public constructor ( provider : ITerminalProvider ) {
53- this . _providers = new Set < ITerminalProvider > ( ) ;
54- this . _providers . add ( provider ) ;
18+ this . _providers = new Set < ITerminalProvider > ( [ provider ] ) ;
5519 }
5620
5721 /**
@@ -65,9 +29,7 @@ export class Terminal implements ITerminal {
6529 * {@inheritdoc ITerminal.unregisterProvider }
6630 */
6731 public unregisterProvider ( provider : ITerminalProvider ) : void {
68- if ( this . _providers . has ( provider ) ) {
69- this . _providers . delete ( provider ) ;
70- }
32+ this . _providers . delete ( provider ) ;
7133 }
7234
7335 /**
@@ -183,30 +145,11 @@ export class Terminal implements ITerminal {
183145 }
184146
185147 private _writeSegmentsToProviders (
186- segments : ( string | ILegacyColorableSequence ) [ ] ,
148+ segments : string [ ] ,
187149 severity : TerminalProviderSeverity ,
188150 followedByEol : boolean
189151 ) : void {
190- const linesSegments : string [ ] [ ] = [ [ ] ] ;
191- let currentLineSegments : string [ ] = linesSegments [ 0 ] ;
192- for ( const segment of segments ) {
193- if ( typeof segment === 'string' ) {
194- currentLineSegments . push ( segment ) ;
195- } else {
196- if ( segment . isEol ) {
197- linesSegments . push ( [ ] ) ;
198- currentLineSegments = linesSegments [ linesSegments . length - 1 ] ;
199- } else {
200- currentLineSegments . push ( this . _serializeLegacyColorableSequence ( segment ) ) ;
201- }
202- }
203- }
204-
205- const lines : string [ ] = [ ] ;
206- for ( const lineSegments of linesSegments ) {
207- lines . push ( lineSegments . join ( '' ) ) ;
208- }
209-
152+ const lines : string [ ] = [ segments . join ( '' ) ] ;
210153 if ( followedByEol ) {
211154 lines . push ( '' ) ;
212155 }
@@ -243,179 +186,6 @@ export class Terminal implements ITerminal {
243186 }
244187 }
245188
246- private _serializeLegacyColorableSequence ( segment : ILegacyColorableSequence ) : string {
247- const startColorCodes : number [ ] = [ ] ;
248- const endColorCodes : number [ ] = [ ] ;
249- switch ( segment . foregroundColor ) {
250- case ColorValue . Black : {
251- startColorCodes . push ( SgrParameterAttribute . BlackForeground ) ;
252- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
253- break ;
254- }
255-
256- case ColorValue . Red : {
257- startColorCodes . push ( SgrParameterAttribute . RedForeground ) ;
258- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
259- break ;
260- }
261-
262- case ColorValue . Green : {
263- startColorCodes . push ( SgrParameterAttribute . GreenForeground ) ;
264- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
265- break ;
266- }
267-
268- case ColorValue . Yellow : {
269- startColorCodes . push ( SgrParameterAttribute . YellowForeground ) ;
270- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
271- break ;
272- }
273-
274- case ColorValue . Blue : {
275- startColorCodes . push ( SgrParameterAttribute . BlueForeground ) ;
276- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
277- break ;
278- }
279-
280- case ColorValue . Magenta : {
281- startColorCodes . push ( SgrParameterAttribute . MagentaForeground ) ;
282- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
283- break ;
284- }
285-
286- case ColorValue . Cyan : {
287- startColorCodes . push ( SgrParameterAttribute . CyanForeground ) ;
288- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
289- break ;
290- }
291-
292- case ColorValue . White : {
293- startColorCodes . push ( SgrParameterAttribute . WhiteForeground ) ;
294- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
295- break ;
296- }
297-
298- case ColorValue . Gray : {
299- startColorCodes . push ( SgrParameterAttribute . GrayForeground ) ;
300- endColorCodes . push ( SgrParameterAttribute . DefaultForeground ) ;
301- break ;
302- }
303- }
304-
305- switch ( segment . backgroundColor ) {
306- case ColorValue . Black : {
307- startColorCodes . push ( SgrParameterAttribute . BlackBackground ) ;
308- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
309- break ;
310- }
311-
312- case ColorValue . Red : {
313- startColorCodes . push ( SgrParameterAttribute . RedBackground ) ;
314- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
315- break ;
316- }
317-
318- case ColorValue . Green : {
319- startColorCodes . push ( SgrParameterAttribute . GreenBackground ) ;
320- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
321- break ;
322- }
323-
324- case ColorValue . Yellow : {
325- startColorCodes . push ( SgrParameterAttribute . YellowBackground ) ;
326- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
327- break ;
328- }
329-
330- case ColorValue . Blue : {
331- startColorCodes . push ( SgrParameterAttribute . BlueBackground ) ;
332- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
333- break ;
334- }
335-
336- case ColorValue . Magenta : {
337- startColorCodes . push ( SgrParameterAttribute . MagentaBackground ) ;
338- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
339- break ;
340- }
341-
342- case ColorValue . Cyan : {
343- startColorCodes . push ( SgrParameterAttribute . CyanBackground ) ;
344- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
345- break ;
346- }
347-
348- case ColorValue . White : {
349- startColorCodes . push ( SgrParameterAttribute . WhiteBackground ) ;
350- endColorCodes . push ( SgrParameterAttribute . DefaultBackground ) ;
351- break ;
352- }
353-
354- case ColorValue . Gray : {
355- startColorCodes . push ( SgrParameterAttribute . GrayBackground ) ;
356- endColorCodes . push ( 49 ) ;
357- break ;
358- }
359- }
360-
361- if ( segment . textAttributes ) {
362- for ( const textAttribute of segment . textAttributes ) {
363- switch ( textAttribute ) {
364- case TextAttribute . Bold : {
365- startColorCodes . push ( SgrParameterAttribute . Bold ) ;
366- endColorCodes . push ( SgrParameterAttribute . NormalColorOrIntensity ) ;
367- break ;
368- }
369-
370- case TextAttribute . Dim : {
371- startColorCodes . push ( SgrParameterAttribute . Dim ) ;
372- endColorCodes . push ( SgrParameterAttribute . NormalColorOrIntensity ) ;
373- break ;
374- }
375-
376- case TextAttribute . Underline : {
377- startColorCodes . push ( SgrParameterAttribute . Underline ) ;
378- endColorCodes . push ( SgrParameterAttribute . UnderlineOff ) ;
379- break ;
380- }
381-
382- case TextAttribute . Blink : {
383- startColorCodes . push ( SgrParameterAttribute . Blink ) ;
384- endColorCodes . push ( SgrParameterAttribute . BlinkOff ) ;
385- break ;
386- }
387-
388- case TextAttribute . InvertColor : {
389- startColorCodes . push ( SgrParameterAttribute . InvertColor ) ;
390- endColorCodes . push ( SgrParameterAttribute . InvertColorOff ) ;
391- break ;
392- }
393-
394- case TextAttribute . Hidden : {
395- startColorCodes . push ( SgrParameterAttribute . Hidden ) ;
396- endColorCodes . push ( SgrParameterAttribute . HiddenOff ) ;
397- break ;
398- }
399- }
400- }
401- }
402-
403- const resultSegments : string [ ] = [ ] ;
404- for ( let j : number = 0 ; j < startColorCodes . length ; j ++ ) {
405- const code : number = startColorCodes [ j ] ;
406- resultSegments . push ( AnsiEscape . getEscapeSequenceForAnsiCode ( code ) ) ;
407- }
408-
409- resultSegments . push ( segment . text ) ;
410-
411- for ( let j : number = endColorCodes . length - 1 ; j >= 0 ; j -- ) {
412- const code : number = endColorCodes [ j ] ;
413- resultSegments . push ( AnsiEscape . getEscapeSequenceForAnsiCode ( code ) ) ;
414- }
415-
416- return resultSegments . join ( '' ) ;
417- }
418-
419189 private _normalizeWriteParameters ( parameters : TerminalWriteParameters ) : {
420190 parts : string [ ] ;
421191 options : ITerminalWriteOptions ;
0 commit comments