@@ -27,8 +27,6 @@ abstract class BaseSerializeHandler {
2727 for ( let row = startRow ; row < endRow ; row ++ ) {
2828 const line = this . _buffer . getLine ( row ) ;
2929
30- this . _lineStart ( row ) ;
31-
3230 if ( line ) {
3331 for ( let col = 0 ; col < line . length ; col ++ ) {
3432 const newCell = line . getCell ( col , oldCell === cell1 ? cell2 : cell1 ) ;
@@ -37,20 +35,14 @@ abstract class BaseSerializeHandler {
3735 console . warn ( `Can't get cell at row=${ row } , col=${ col } ` ) ;
3836 continue ;
3937 }
40- if ( ! newCell . equalFg ( oldCell ) || ! newCell . equalBg ( oldCell ) ) {
41- this . _cellFgBgChanged ( newCell , oldCell , row , col ) ;
42- }
43- if ( ! newCell . equalFlags ( oldCell ) ) {
44- this . _cellFlagsChanged ( newCell , oldCell , row , col ) ;
45- }
4638
4739 this . _nextCell ( newCell , oldCell , row , col ) ;
4840
4941 oldCell = newCell ;
5042 }
5143 }
5244
53- this . _lineEnd ( row ) ;
45+ this . _nextRow ( row ) ;
5446 }
5547
5648 this . _serializeEnd ( ) ;
@@ -60,13 +52,7 @@ abstract class BaseSerializeHandler {
6052
6153 protected _nextCell ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void { }
6254
63- protected _cellFlagsChanged ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void { }
64-
65- protected _cellFgBgChanged ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void { }
66-
67- protected _lineStart ( row : number ) : void { }
68-
69- protected _lineEnd ( row : number ) : void { }
55+ protected _nextRow ( row : number ) : void { }
7056
7157 protected _serializeStart ( rows : number ) : void { }
7258
@@ -80,7 +66,6 @@ class StringSerializeHandler extends BaseSerializeHandler {
8066 private _allRows : string [ ] = new Array < string > ( ) ;
8167 private _currentRow : string = '' ;
8268 private _nullCellCount : number = 0 ;
83- private _sgrSeq : number [ ] = [ ] ;
8469
8570 constructor ( buffer : IBuffer ) {
8671 super ( buffer ) ;
@@ -90,51 +75,52 @@ class StringSerializeHandler extends BaseSerializeHandler {
9075 this . _allRows = new Array < string > ( rows ) ;
9176 }
9277
93- protected _lineEnd ( row : number ) : void {
78+ protected _nextRow ( row : number ) : void {
9479 this . _allRows [ this . _rowIndex ++ ] = this . _currentRow ;
9580 this . _currentRow = '' ;
9681 this . _nullCellCount = 0 ;
9782 }
9883
99- protected _cellFlagsChanged ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void {
100- const sgrSeq = this . _sgrSeq ;
101-
102- // skip if it's default color style, we will use \x1b[0m to clear every color style later
103- if ( cell . isAttributeDefault ( ) || cell . equalFlags ( oldCell ) ) { return ; }
104-
105- if ( cell . isInverse ( ) !== oldCell . isInverse ( ) ) { sgrSeq . push ( cell . isInverse ( ) ? 7 : 27 ) ; }
106- if ( cell . isBold ( ) !== oldCell . isBold ( ) ) { sgrSeq . push ( cell . isBold ( ) ? 1 : 22 ) ; }
107- if ( cell . isUnderline ( ) !== oldCell . isUnderline ( ) ) { sgrSeq . push ( cell . isUnderline ( ) ? 4 : 24 ) ; }
108- if ( cell . isBlink ( ) !== oldCell . isBlink ( ) ) { sgrSeq . push ( cell . isBlink ( ) ? 5 : 25 ) ; }
109- if ( cell . isInvisible ( ) !== oldCell . isInvisible ( ) ) { sgrSeq . push ( cell . isInvisible ( ) ? 8 : 28 ) ; }
110- if ( cell . isItalic ( ) !== oldCell . isItalic ( ) ) { sgrSeq . push ( cell . isItalic ( ) ? 3 : 23 ) ; }
111- if ( cell . isDim ( ) !== oldCell . isDim ( ) ) { sgrSeq . push ( cell . isDim ( ) ? 2 : 22 ) ; }
112- }
113-
114- protected _cellFgBgChanged ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void {
115- const sgrSeq = this . _sgrSeq ;
116-
117- // skip if it's default color style, we will use \x1b[0m to clear every color style later
118- if ( cell . isAttributeDefault ( ) ) { return ; }
84+ protected _nextCell ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void {
85+ const sgrSeq : number [ ] = [ ] ;
86+ const fgChanged = ! cell . equalFg ( oldCell ) ;
87+ const bgChanged = ! cell . equalBg ( oldCell ) ;
88+ const flagsChanged = ! cell . equalFlags ( oldCell ) ;
11989
120- if ( ! cell . equalFg ( oldCell ) ) {
121- const color = cell . getFgColor ( ) ;
122- if ( cell . isFgRGB ( ) ) { sgrSeq . push ( 38 , 2 , ( color >>> 16 ) & 0xFF , ( color >>> 8 ) & 0xFF , color & 0xFF ) ; }
123- else if ( cell . isFgPalette256 ( ) ) { sgrSeq . push ( 38 , 5 , color ) ; }
124- else if ( cell . isFgPalette16 ( ) ) { sgrSeq . push ( color & 8 ? 90 + ( color & 7 ) : 30 + ( color & 7 ) ) ; }
125- else { sgrSeq . push ( 39 ) ; }
90+ if ( fgChanged || bgChanged || flagsChanged ) {
91+ if ( cell . isAttributeDefault ( ) ) {
92+ this . _currentRow += '\x1b[0m' ;
93+ } else {
94+ if ( fgChanged ) {
95+ const color = cell . getFgColor ( ) ;
96+ if ( cell . isFgRGB ( ) ) { sgrSeq . push ( 38 , 2 , ( color >>> 16 ) & 0xFF , ( color >>> 8 ) & 0xFF , color & 0xFF ) ; }
97+ else if ( cell . isFgPalette256 ( ) ) { sgrSeq . push ( 38 , 5 , color ) ; }
98+ else if ( cell . isFgPalette16 ( ) ) { sgrSeq . push ( color & 8 ? 90 + ( color & 7 ) : 30 + ( color & 7 ) ) ; }
99+ else { sgrSeq . push ( 39 ) ; }
100+ }
101+ if ( bgChanged ) {
102+ const color = cell . getBgColor ( ) ;
103+ if ( cell . isBgRGB ( ) ) { sgrSeq . push ( 48 , 2 , ( color >>> 16 ) & 0xFF , ( color >>> 8 ) & 0xFF , color & 0xFF ) ; }
104+ else if ( cell . isBgPalette256 ( ) ) { sgrSeq . push ( 48 , 5 , color ) ; }
105+ else if ( cell . isBgPalette16 ( ) ) { sgrSeq . push ( color & 8 ? 100 + ( color & 7 ) : 40 + ( color & 7 ) ) ; }
106+ else { sgrSeq . push ( 49 ) ; }
107+ }
108+ if ( flagsChanged ) {
109+ if ( cell . isInverse ( ) !== oldCell . isInverse ( ) ) { sgrSeq . push ( cell . isInverse ( ) ? 7 : 27 ) ; }
110+ if ( cell . isBold ( ) !== oldCell . isBold ( ) ) { sgrSeq . push ( cell . isBold ( ) ? 1 : 22 ) ; }
111+ if ( cell . isUnderline ( ) !== oldCell . isUnderline ( ) ) { sgrSeq . push ( cell . isUnderline ( ) ? 4 : 24 ) ; }
112+ if ( cell . isBlink ( ) !== oldCell . isBlink ( ) ) { sgrSeq . push ( cell . isBlink ( ) ? 5 : 25 ) ; }
113+ if ( cell . isInvisible ( ) !== oldCell . isInvisible ( ) ) { sgrSeq . push ( cell . isInvisible ( ) ? 8 : 28 ) ; }
114+ if ( cell . isItalic ( ) !== oldCell . isItalic ( ) ) { sgrSeq . push ( cell . isItalic ( ) ? 3 : 23 ) ; }
115+ if ( cell . isDim ( ) !== oldCell . isDim ( ) ) { sgrSeq . push ( cell . isDim ( ) ? 2 : 22 ) ; }
116+ }
117+ }
126118 }
127119
128- if ( ! cell . equalBg ( oldCell ) ) {
129- const color = cell . getBgColor ( ) ;
130- if ( cell . isBgRGB ( ) ) { sgrSeq . push ( 48 , 2 , ( color >>> 16 ) & 0xFF , ( color >>> 8 ) & 0xFF , color & 0xFF ) ; }
131- else if ( cell . isBgPalette256 ( ) ) { sgrSeq . push ( 48 , 5 , color ) ; }
132- else if ( cell . isBgPalette16 ( ) ) { sgrSeq . push ( color & 8 ? 100 + ( color & 7 ) : 40 + ( color & 7 ) ) ; }
133- else { sgrSeq . push ( 49 ) ; }
120+ if ( sgrSeq . length ) {
121+ this . _currentRow += `\x1b[${ sgrSeq . join ( ';' ) } m` ;
134122 }
135- }
136123
137- protected _nextCell ( cell : IBufferCell , oldCell : IBufferCell , row : number , col : number ) : void {
138124 // Count number of null cells encountered after the last non-null cell and move the cursor
139125 // if a non-null cell is found (eg. \t or cursor move)
140126 if ( cell . char === '' ) {
@@ -144,19 +130,6 @@ class StringSerializeHandler extends BaseSerializeHandler {
144130 this . _nullCellCount = 0 ;
145131 }
146132
147- const fgChanged = ! cell . equalFg ( oldCell ) ;
148- const bgChanged = ! cell . equalBg ( oldCell ) ;
149- const flagsChanged = ! cell . equalFlags ( oldCell ) ;
150-
151- if ( cell . isAttributeDefault ( ) && ( fgChanged || bgChanged || flagsChanged ) ) {
152- this . _currentRow += '\x1b[0m' ;
153- }
154-
155- if ( this . _sgrSeq . length ) {
156- this . _currentRow += `\x1b[${ this . _sgrSeq . join ( ';' ) } m` ;
157- this . _sgrSeq = [ ] ;
158- }
159-
160133 this . _currentRow += cell . char ;
161134 }
162135
0 commit comments