@@ -318,102 +318,24 @@ export class ImageAddon implements ITerminalAddon {
318
318
/**
319
319
* demo hack for complex terminal buffer serialization
320
320
*/
321
- private _parts : string [ ] = [ '' ] ;
322
321
323
- // example for text/FG/BG serializer
324
- private _serText ( num : number ) : number [ ] {
325
- // FIXME: no FG/BG attributes atm
326
- const buffer = this . _terminal ! . _core . buffer ;
327
- const line = buffer . lines . get ( num ) ;
328
- if ( ! line ) return [ ] ;
329
- const cell = this . _terminal ?. buffer . active . getNullCell ( ) ! ;
330
- const cols = this . _terminal ! . cols ;
331
- const res : number [ ] = [ ] ;
332
- let partPos = 0 ;
333
- let content = '' ;
334
- for ( let col = 0 ; col < cols ; ++ col ) {
335
- line ?. loadCell ( col , cell as any ) ;
336
- if ( ! cell . getChars ( ) ) {
337
- partPos = 0 ;
338
- if ( content ) this . _parts . push ( content ) ;
339
- content = '' ;
340
- } else {
341
- partPos = this . _parts . length ;
342
- content += cell . getChars ( ) ;
343
- }
344
- res . push ( partPos ) ;
345
- }
346
- if ( content ) this . _parts . push ( content ) ;
347
- return res ;
348
- }
349
-
350
- private _encodeTile ( canvas : HTMLCanvasElement ) : string {
351
- // repack cell tile into a proper cell covering canvas if it is too small
352
- const cw = this . _renderer ! . dimensions ?. css . cell . width || CELL_SIZE_DEFAULT . width ;
353
- const ch = this . _renderer ! . dimensions ?. css . cell . height || CELL_SIZE_DEFAULT . height ;
354
- if ( canvas . width < cw || canvas . height < ch ) {
355
- const newCanvas = ImageRenderer . createCanvas ( window , Math . ceil ( cw ) , Math . ceil ( ch ) ) ;
356
- newCanvas . getContext ( '2d' ) ?. drawImage ( canvas , 0 , 0 ) ;
357
- canvas = newCanvas ;
358
- }
322
+ public serializeLine ( num : number ) : string {
323
+ const canvas = this . _storage ! . extractLineCanvas ( num ) ;
324
+ if ( ! canvas ) return '' ;
325
+ const w = this . _terminal ! . cols ;
359
326
const data = canvas . toDataURL ( 'image/png' ) . slice ( 22 ) ;
360
- const iipSeq = `\x1b]1337;File=inline=1;width=1;height=1;preserveAspectRatio=0;size=${ atob ( data ) . length } :${ data } ` ;
361
- return iipSeq + '\x1b[C' ; // + cursor advance by one
362
- }
363
-
364
- // example for image serializer
365
- private _serImages ( num : number ) : number [ ] {
366
- const res : number [ ] = [ ] ;
367
- const cols = this . _terminal ! . cols ;
368
- const buffer = this . _terminal ! . _core . buffer ;
369
- const line = buffer . lines . get ( num ) ;
370
- if ( ! line ) return [ ] ;
371
-
372
- for ( let col = 0 ; col < cols ; ++ col ) {
373
- // for simplicity only single cell tile encoding atm
374
- const canvas = this . extractTileAtBufferCell ( col , num ) ;
375
- if ( ! canvas || ! canvas . width || ! canvas . height ) {
376
- res . push ( 0 ) ;
377
- continue ;
378
- }
379
- res . push ( this . _parts . length ) ;
380
- this . _parts . push ( this . _encodeTile ( canvas ) ) ;
381
- }
382
- return res ;
327
+ const iipSeq = `\x1b]1337;File=inline=1;width=${ w } ;height=1;preserveAspectRatio=0;size=${ atob ( data ) . length } :${ data } ` ;
328
+ return '\r' + iipSeq + `\x1b[${ w } C` ; // CR + IIP sequence + cursor advance by line width
383
329
}
384
330
385
331
public serialize ( start : number , end : number ) : string [ ] {
386
332
const lines : string [ ] = [ ] ;
387
- const cols = this . _terminal ! . cols ;
333
+ const buffer = this . _terminal ! . _core . buffer ;
388
334
for ( let row = start ; row < end ; ++ row ) {
389
- const indices : number [ ] [ ] = [ ] ;
390
- // FIXME: turn next 2 invocations into registered event handlers
391
- indices . push ( this . _serText ( row ) ) ;
392
- indices . push ( this . _serImages ( row ) ) ;
393
-
394
- // fuse logic
395
- const entries : string [ ] = [ ] ;
396
- let cursorAdjust = 0 ;
397
- for ( let i = 0 ; i < cols ; ++ i ) {
398
- let entry = '' ;
399
- let handled = 0 ;
400
- for ( let k = 0 ; k < indices . length ; ++ k ) {
401
- handled |= indices [ k ] [ i ] ;
402
- if ( this . _parts [ indices [ k ] [ i ] ] ) {
403
- entry += this . _parts [ indices [ k ] [ i ] ] ;
404
- this . _parts [ indices [ k ] [ i ] ] = '' ;
405
- }
406
- }
407
- if ( handled && cursorAdjust ) {
408
- entries . push ( `\x1b[${ cursorAdjust } C` ) ;
409
- cursorAdjust = 0 ;
410
- } else if ( ! handled ) {
411
- cursorAdjust ++ ;
412
- }
413
- entries . push ( entry ) ;
414
- }
415
- lines . push ( entries . join ( '' ) ) ;
416
- this . _parts . length = 1 ;
335
+ const line = buffer . lines . get ( row ) ;
336
+ if ( ! line ) break ;
337
+ // FIXME: hook into serialize addon instead of translateToString
338
+ lines . push ( line . translateToString ( true ) + this . serializeLine ( row ) ) ;
417
339
}
418
340
return lines ;
419
341
}
0 commit comments