@@ -49,9 +49,11 @@ const codesANSI = {
49
49
} ,
50
50
} ;
51
51
52
- function getCellText ( content , colWidth , cellAttrs ) {
52
+ function getCellText ( content , colWidth , cellAttrs , rowAttrs , colAttrs ) {
53
53
var attrs ;
54
54
if ( typeof cellAttrs === "object" ) attrs = cellAttrs ;
55
+ else if ( typeof rowAttrs === "object" ) attrs = rowAttrs ;
56
+ else if ( typeof colAttrs === "object" ) attrs = colAttrs ;
55
57
else attrs = { } ;
56
58
57
59
var columnStr = "" ;
@@ -264,7 +266,7 @@ class Table {
264
266
if ( table . length > 0 ) {
265
267
stringTable += "\n" ;
266
268
for ( var i = 0 ; i < table [ 0 ] . length ; i ++ ) {
267
- stringTable += this . borders . sep + " " . repeat ( this . leftPadding ) + getCellText ( table [ 0 ] [ i ] , columnsLengths [ i ] , this . attrs [ `cell:${ 0 } ,${ i } ` ] ) + " " . repeat ( this . rightPadding ) ;
269
+ stringTable += this . borders . sep + " " . repeat ( this . leftPadding ) + getCellText ( table [ 0 ] [ i ] , columnsLengths [ i ] , this . attrs [ `cell:${ 0 } ,${ i } ` ] , this . attrs [ `row: ${ 0 } ` ] , this . attrs [ `col: ${ i } ` ] ) + " " . repeat ( this . rightPadding ) ;
268
270
}
269
271
stringTable += this . borders . sep ;
270
272
}
@@ -281,7 +283,7 @@ class Table {
281
283
if ( table [ i ] !== "HORIZONTAL LINE" ) {
282
284
stringTable += "\n" ;
283
285
for ( var j = 0 ; j < table [ i ] . length ; j ++ ) {
284
- stringTable += this . borders . sep + " " . repeat ( this . leftPadding ) + getCellText ( table [ i ] [ j ] , columnsLengths [ j ] , this . attrs [ `cell:${ i } ,${ j } ` ] ) + " " . repeat ( this . rightPadding ) ;
286
+ stringTable += this . borders . sep + " " . repeat ( this . leftPadding ) + getCellText ( table [ i ] [ j ] , columnsLengths [ j ] , this . attrs [ `cell:${ i } ,${ j } ` ] , this . attrs [ `row: ${ i } ` ] , this . attrs [ `col: ${ j } ` ] ) + " " . repeat ( this . rightPadding ) ;
285
287
}
286
288
stringTable += this . borders . sep ;
287
289
if ( this . horizontalLines && i < table . length - 1 ) {
@@ -349,6 +351,38 @@ class Table {
349
351
this . attrs [ `cell:${ row } ,${ col } ` ] = attrs ;
350
352
} else throw TypeError ( "attrs must be an object or undefined" ) ;
351
353
}
354
+
355
+ setRowAttrs ( row , attrs ) {
356
+ if ( row >= this . rows . length ) throw RangeError ( "row number out of range" ) ;
357
+ if ( this . rows [ row ] === "HORIZONTAL LINE" ) throw SyntaxError ( "this row is horizontal line" ) ;
358
+ if ( typeof attrs === "undefined" ) delete this . attrs [ `row:${ row } ` ] ;
359
+ else if ( typeof attrs === "object" ) {
360
+ Object . keys ( attrs ) . forEach ( ( key ) => { if ( ! [ "color" , "bgColor" , "decorations" , "align" ] . includes ( key ) ) throw SyntaxError ( `invalid attribute "${ key } "` ) ; } ) ;
361
+ if ( typeof attrs . color !== "string" && typeof attrs . color !== "undefined" ) throw TypeError ( "attrs.color must be a string or undefined" ) ;
362
+ if ( typeof attrs . bgColor !== "string" && typeof attrs . bgColor !== "undefined" ) throw TypeError ( "attrs.bgColor must be a string or undefined" ) ;
363
+ if ( ! Array . isArray ( attrs . decorations ) && typeof attrs . decorations !== "undefined" ) throw TypeError ( "attrs.decorations must be an array or undefined" ) ;
364
+ if ( typeof attrs . color === "string" && ! codesANSI . foreground . hasOwnProperty ( attrs . color ) ) throw SyntaxError ( "attrs.color is invalid" ) ;
365
+ if ( typeof attrs . bgColor === "string" && ! codesANSI . background . hasOwnProperty ( attrs . bgColor ) ) throw SyntaxError ( "attrs.bgColor is invalid" ) ;
366
+ if ( Array . isArray ( attrs . decorations ) ) attrs . decorations . forEach ( ( name ) => { if ( ! Object . keys ( codesANSI . decorations ) . includes ( name ) ) throw SyntaxError ( `invalid decoration "${ name } "` ) ; } ) ;
367
+ if ( typeof attrs . align !== "undefined" && attrs . align !== "left" && attrs . align !== "center" && attrs . align !== "right" ) throw SyntaxError ( 'attrs.align must be "left", "center", "right" or undefined' ) ;
368
+ this . attrs [ `row:${ row } ` ] = attrs ;
369
+ } else throw TypeError ( "attrs must be an object or undefined" ) ;
370
+ }
371
+
372
+ setColAttrs ( col , attrs ) {
373
+ if ( typeof attrs === "undefined" ) delete this . attrs [ `col:${ col } ` ] ;
374
+ else if ( typeof attrs === "object" ) {
375
+ Object . keys ( attrs ) . forEach ( ( key ) => { if ( ! [ "color" , "bgColor" , "decorations" , "align" ] . includes ( key ) ) throw SyntaxError ( `invalid attribute "${ key } "` ) ; } ) ;
376
+ if ( typeof attrs . color !== "string" && typeof attrs . color !== "undefined" ) throw TypeError ( "attrs.color must be a string or undefined" ) ;
377
+ if ( typeof attrs . bgColor !== "string" && typeof attrs . bgColor !== "undefined" ) throw TypeError ( "attrs.bgColor must be a string or undefined" ) ;
378
+ if ( ! Array . isArray ( attrs . decorations ) && typeof attrs . decorations !== "undefined" ) throw TypeError ( "attrs.decorations must be an array or undefined" ) ;
379
+ if ( typeof attrs . color === "string" && ! codesANSI . foreground . hasOwnProperty ( attrs . color ) ) throw SyntaxError ( "attrs.color is invalid" ) ;
380
+ if ( typeof attrs . bgColor === "string" && ! codesANSI . background . hasOwnProperty ( attrs . bgColor ) ) throw SyntaxError ( "attrs.bgColor is invalid" ) ;
381
+ if ( Array . isArray ( attrs . decorations ) ) attrs . decorations . forEach ( ( name ) => { if ( ! Object . keys ( codesANSI . decorations ) . includes ( name ) ) throw SyntaxError ( `invalid decoration "${ name } "` ) ; } ) ;
382
+ if ( typeof attrs . align !== "undefined" && attrs . align !== "left" && attrs . align !== "center" && attrs . align !== "right" ) throw SyntaxError ( 'attrs.align must be "left", "center", "right" or undefined' ) ;
383
+ this . attrs [ `col:${ col } ` ] = attrs ;
384
+ } else throw TypeError ( "attrs must be an object or undefined" ) ;
385
+ }
352
386
}
353
387
354
388
module . exports = Table ;
0 commit comments