2
2
3
3
const { Math, ObjectPrototype } = primordials ;
4
4
5
- const { Buffer } = require ( 'buffer' ) ;
6
- const { removeColors } = require ( 'internal/util' ) ;
5
+ const { getStringWidth } = require ( 'internal/readline/utils' ) ;
7
6
8
7
// The use of Unicode characters below is the only non-comment use of non-ASCII
9
8
// Unicode characters in Node.js built-in modules. If they are ever removed or
@@ -29,16 +28,11 @@ const tableChars = {
29
28
/* eslint-enable node-core/non-ascii-character */
30
29
} ;
31
30
32
- const countSymbols = ( string ) => {
33
- const normalized = removeColors ( string ) . normalize ( 'NFC' ) ;
34
- return Buffer . from ( normalized , 'UCS-2' ) . byteLength / 2 ;
35
- } ;
36
-
37
31
const renderRow = ( row , columnWidths ) => {
38
32
let out = tableChars . left ;
39
33
for ( var i = 0 ; i < row . length ; i ++ ) {
40
34
const cell = row [ i ] ;
41
- const len = countSymbols ( cell ) ;
35
+ const len = getStringWidth ( cell ) ;
42
36
const needed = ( columnWidths [ i ] - len ) / 2 ;
43
37
// round(needed) + ceil(needed) will always add up to the amount
44
38
// of spaces we need while also left justifying the output.
@@ -52,7 +46,7 @@ const renderRow = (row, columnWidths) => {
52
46
53
47
const table = ( head , columns ) => {
54
48
const rows = [ ] ;
55
- const columnWidths = head . map ( ( h ) => countSymbols ( h ) ) ;
49
+ const columnWidths = head . map ( ( h ) => getStringWidth ( h ) ) ;
56
50
const longestColumn = columns . reduce ( ( n , a ) => Math . max ( n , a . length ) , 0 ) ;
57
51
58
52
for ( var i = 0 ; i < head . length ; i ++ ) {
@@ -63,7 +57,7 @@ const table = (head, columns) => {
63
57
const value = rows [ j ] [ i ] =
64
58
ObjectPrototype . hasOwnProperty ( column , j ) ? column [ j ] : '' ;
65
59
const width = columnWidths [ i ] || 0 ;
66
- const counted = countSymbols ( value ) ;
60
+ const counted = getStringWidth ( value ) ;
67
61
columnWidths [ i ] = Math . max ( width , counted ) ;
68
62
}
69
63
}
0 commit comments