@@ -12,34 +12,34 @@ export interface AnsiState {
1212}
1313
1414// Color mapping for standard ANSI colors
15- const foregroundColors : { [ key : number ] : string } = {
16- 30 : '#000000' , // black
17- 31 : '#f44336' , // red
18- 32 : '#4caf50' , // green
19- 33 : '#ffeb3b' , // yellow
20- 34 : '#2196f3' , // blue
21- 35 : '#e91e63' , // magenta
22- 36 : '#00bcd4' , // cyan
23- 37 : '#ffffff' , // white
24- 90 : '#9e9e9e' , // gray
25- 91 : '#ff5252' , // lightred
26- 92 : '#8bc34a' , // lightgreen
27- 93 : '#ffc107' , // lightyellow
28- 94 : '#03a9f4' , // lightblue
29- 95 : '#ec407a' , // lightmagenta
30- 96 : '#26c6da' , // lightcyan
31- 97 : '#fafafa' , // white
15+ const foregroundColors : { [ key : number ] : string } = {
16+ 30 : '#000000' , // black
17+ 31 : '#f44336' , // red
18+ 32 : '#4caf50' , // green
19+ 33 : '#ffeb3b' , // yellow
20+ 34 : '#2196f3' , // blue
21+ 35 : '#e91e63' , // magenta
22+ 36 : '#00bcd4' , // cyan
23+ 37 : '#ffffff' , // white
24+ 90 : '#9e9e9e' , // gray
25+ 91 : '#ff5252' , // lightred
26+ 92 : '#8bc34a' , // lightgreen
27+ 93 : '#ffc107' , // lightyellow
28+ 94 : '#03a9f4' , // lightblue
29+ 95 : '#ec407a' , // lightmagenta
30+ 96 : '#26c6da' , // lightcyan
31+ 97 : '#fafafa' , // white
3232} ;
3333
34- const backgroundColors : { [ key : number ] : string } = {
35- 40 : '#000000' , // black
36- 41 : '#f44336' , // red
37- 42 : '#4caf50' , // green
38- 43 : '#ffeb3b' , // yellow
39- 44 : '#2196f3' , // blue
40- 45 : '#e91e63' , // magenta
41- 46 : '#00bcd4' , // cyan
42- 47 : '#ffffff' , // white
34+ const backgroundColors : { [ key : number ] : string } = {
35+ 40 : '#000000' , // black
36+ 41 : '#f44336' , // red
37+ 42 : '#4caf50' , // green
38+ 43 : '#ffeb3b' , // yellow
39+ 44 : '#2196f3' , // blue
40+ 45 : '#e91e63' , // magenta
41+ 46 : '#00bcd4' , // cyan
42+ 47 : '#ffffff' , // white
4343 100 : '#9e9e9e' , // gray
4444 101 : '#ff5252' , // lightred
4545 102 : '#8bc34a' , // lightgreen
@@ -59,28 +59,28 @@ export interface AnsiSegment {
5959export function parseAnsiString ( text : string ) : AnsiSegment [ ] {
6060 const result : AnsiSegment [ ] = [ ] ;
6161 const regex = / \u001b \[ ( (?: \d + ; ) * \d * ) m / g;
62-
62+
6363 let lastIndex = 0 ;
6464 let match : RegExpExecArray | null ;
6565 let currentState : AnsiState = { } ;
66-
66+
6767 while ( ( match = regex . exec ( text ) ) !== null ) {
6868 // Add text before the escape sequence with current state
6969 const segment = text . substring ( lastIndex , match . index ) ;
7070 if ( segment ) {
7171 result . push ( {
7272 text : segment ,
73- state : { ...currentState }
73+ state : { ...currentState } ,
7474 } ) ;
7575 }
76-
76+
7777 // Update state based on escape sequence
78- const codes = match [ 1 ] . split ( ';' ) . map ( num => parseInt ( num || '0' , 10 ) ) ;
79-
78+ const codes = match [ 1 ] . split ( ';' ) . map ( ( num ) => parseInt ( num || '0' , 10 ) ) ;
79+
8080 // Process codes
8181 for ( let i = 0 ; i < codes . length ; i ++ ) {
8282 const code = codes [ i ] ;
83-
83+
8484 // Process reset and text styles
8585 if ( code === 0 ) {
8686 // Reset all attributes
@@ -108,16 +108,16 @@ export function parseAnsiString(text: string): AnsiSegment[] {
108108 delete currentState . foreground ;
109109 } else if ( code === 49 ) {
110110 delete currentState . background ;
111- }
111+ }
112112 // Standard colors
113113 else if ( ( code >= 30 && code <= 37 ) || ( code >= 90 && code <= 97 ) ) {
114114 currentState . foreground = foregroundColors [ code ] ;
115115 } else if ( ( code >= 40 && code <= 47 ) || ( code >= 100 && code <= 107 ) ) {
116116 currentState . background = backgroundColors [ code ] ;
117117 }
118118 // 8-bit color support (256 colors)
119- else if ( code === 38 && i + 2 < codes . length && codes [ i + 1 ] === 5 ) {
120- const colorCode = codes [ i + 2 ] ;
119+ else if ( code === 38 && i + 2 < codes . length && codes [ i + 1 ] === 5 ) {
120+ const colorCode = codes [ i + 2 ] ;
121121 // Generate 8-bit color
122122 if ( colorCode < 8 ) {
123123 // Standard colors (0-7)
@@ -137,9 +137,8 @@ export function parseAnsiString(text: string): AnsiSegment[] {
137137 currentState . foreground = `rgb(${ gray } , ${ gray } , ${ gray } )` ;
138138 }
139139 i += 2 ; // Skip the next two parameters
140- }
141- else if ( code === 48 && i + 2 < codes . length && codes [ i + 1 ] === 5 ) {
142- const colorCode = codes [ i + 2 ] ;
140+ } else if ( code === 48 && i + 2 < codes . length && codes [ i + 1 ] === 5 ) {
141+ const colorCode = codes [ i + 2 ] ;
143142 // Same logic for background colors
144143 if ( colorCode < 8 ) {
145144 currentState . background = backgroundColors [ colorCode + 40 ] ;
@@ -157,34 +156,33 @@ export function parseAnsiString(text: string): AnsiSegment[] {
157156 i += 2 ; // Skip the next two parameters
158157 }
159158 // 24-bit color support (RGB)
160- else if ( code === 38 && i + 4 < codes . length && codes [ i + 1 ] === 2 ) {
161- const r = codes [ i + 2 ] ;
162- const g = codes [ i + 3 ] ;
163- const b = codes [ i + 4 ] ;
159+ else if ( code === 38 && i + 4 < codes . length && codes [ i + 1 ] === 2 ) {
160+ const r = codes [ i + 2 ] ;
161+ const g = codes [ i + 3 ] ;
162+ const b = codes [ i + 4 ] ;
164163 currentState . foreground = `rgb(${ r } , ${ g } , ${ b } )` ;
165164 i += 4 ; // Skip the next four parameters
166- }
167- else if ( code === 48 && i + 4 < codes . length && codes [ i + 1 ] === 2 ) {
168- const r = codes [ i + 2 ] ;
169- const g = codes [ i + 3 ] ;
170- const b = codes [ i + 4 ] ;
165+ } else if ( code === 48 && i + 4 < codes . length && codes [ i + 1 ] === 2 ) {
166+ const r = codes [ i + 2 ] ;
167+ const g = codes [ i + 3 ] ;
168+ const b = codes [ i + 4 ] ;
171169 currentState . background = `rgb(${ r } , ${ g } , ${ b } )` ;
172170 i += 4 ; // Skip the next four parameters
173171 }
174172 }
175-
173+
176174 lastIndex = match . index + match [ 0 ] . length ;
177175 }
178-
176+
179177 // Add the remaining text with current state
180178 const remainingText = text . substring ( lastIndex ) ;
181179 if ( remainingText ) {
182180 result . push ( {
183181 text : remainingText ,
184- state : { ...currentState }
182+ state : { ...currentState } ,
185183 } ) ;
186184 }
187-
185+
188186 return result ;
189187}
190188
0 commit comments