@@ -9,7 +9,8 @@ export default class ShowFile extends Command {
9
9
10
10
static flags = {
11
11
help : flags . help ( { char : 'h' } ) ,
12
- file : flags . string ( { char : 'f' , description : 'formatted file to be shown' } )
12
+ file : flags . string ( { char : 'f' , description : 'formatted file to be shown' } ) ,
13
+ num : flags . string ( { char : 'n' , description : 'rows to show' } )
13
14
}
14
15
15
16
static args = [ { name : 'file' } ]
@@ -19,6 +20,9 @@ export default class ShowFile extends Command {
19
20
const { args, flags} = this . parse ( ShowFile )
20
21
21
22
args . file = this . getFilePath ( flags , args )
23
+ args . num = this . getFileLinesToShow ( flags )
24
+
25
+ // args.rowsToShow
22
26
23
27
this . checkParameters ( flags , args )
24
28
this . showFile ( args )
@@ -28,83 +32,71 @@ export default class ShowFile extends Command {
28
32
if ( args . file )
29
33
return args . file
30
34
if ( flags . file )
31
- return flags . file
35
+ return flags . file9
32
36
Logger . error ( this , 'File path not passed' )
33
37
}
34
38
39
+ private getFileLinesToShow ( flags : any ) {
40
+ return flags . num || 10
41
+ }
35
42
// tslint:disable-next-line:no-unused
36
43
private checkParameters ( flags : unknown , args : { [ p : string ] : any } ) {
37
44
if ( args . file === undefined || args . file === '' )
38
45
Logger . error ( this , 'File path is empty' )
39
46
// others already checked
40
47
}
41
48
42
-
43
49
private showFile ( args : any ) {
44
-
45
50
let data = Utilities . getStringFromFile ( this , args . file )
46
-
47
51
let rows = data . split ( '\n' )
48
-
49
52
let widthArray = [ ]
50
53
51
- let recordsToShow = 10
54
+ let recordsToShow = parseInt ( args . num , 10 ) + 1
52
55
53
- for ( let i = 0 ; i < rows [ 0 ] . length ; i ++ ) {
54
- widthArray [ i ] = 0
56
+ for ( let i = 0 ; i < rows [ 0 ] . length ; i ++ ) {
57
+ widthArray [ i ] = 0
55
58
}
56
59
57
- if ( recordsToShow > rows . length ) {
58
- recordsToShow = rows . length
60
+ if ( recordsToShow > rows . length ) {
61
+ recordsToShow = rows . length - 1
59
62
}
60
63
61
- for ( let i = 0 ; i < recordsToShow ; i ++ ) {
62
- let row = rows [ i ] . split ( "," ) ;
63
- let s = ''
64
- for ( let j = 0 ; j < row . length ; j ++ ) {
65
- s = s + row [ j ]
66
- if ( widthArray [ j ] < row [ j ] . length ) {
67
- widthArray [ j ] = row [ j ] . length
68
- }
64
+ for ( let i = 0 ; i < recordsToShow ; i ++ ) {
65
+ let row = rows [ i ] . split ( ',' )
66
+ for ( let j = 0 ; j < row . length ; j ++ ) {
67
+ if ( widthArray [ j ] < row [ j ] . length ) {
68
+ widthArray [ j ] = row [ j ] . length
69
69
}
70
+ }
70
71
}
71
72
72
- this . printRow ( rows [ 0 ] . split ( "," ) , widthArray , "+" , true )
73
- for ( let i = 0 ; i < recordsToShow - 1 ; i ++ ) {
74
- let row = rows [ i ]
75
- this . printRow ( row . split ( ',' ) , widthArray , '|' , false )
76
- this . printRow ( row . split ( ',' ) , widthArray , '+' , true )
73
+ this . printRow ( rows [ 0 ] . split ( ',' ) , widthArray , '+' , true )
74
+ for ( let i = 0 ; i < recordsToShow ; i ++ ) {
75
+ let row = rows [ i ]
76
+ this . printRow ( row . split ( ',' ) , widthArray , '|' , false )
77
+ this . printRow ( row . split ( ',' ) , widthArray , '+' , true )
77
78
}
78
-
79
+ this . log ( `showing top ${ recordsToShow } rows.` )
79
80
}
80
81
81
- private printRow ( row : any , widthArray : any , seperator : any , isLineSeparator : any ) {
82
- let output = seperator
83
- for ( let i = 0 ; i < row . length ; i ++ ) {
84
- let totalSize = widthArray [ i ]
85
-
86
- let dataLength = 0 ;
87
- let space = "-"
88
- if ( ! isLineSeparator ) {
89
- let data = row [ i ]
90
- data = data . split ( / \r / ) [ 0 ]
91
- output += data
92
- dataLength = data . length
93
- space = ' '
94
- // this.log(`The output log upto here is : ${output}`)
95
- }
96
-
97
- for ( let j = 0 ; j < totalSize - dataLength ; j ++ ) {
98
- output += space
99
- }
100
-
101
- output += seperator
102
- if ( ! isLineSeparator ) {
103
- // this.log(`The output log upto here is : ${output}`)
104
- // Logger.info(this, output)
105
- }
82
+ private printRow ( row : any , widthArray : any , seperator : any , isLineSeparator : any ) {
83
+ let output = seperator
84
+ for ( let i = 0 ; i < row . length ; i ++ ) {
85
+ let totalSize = widthArray [ i ]
86
+ let dataLength = 0
87
+ let space = '-'
88
+ if ( ! isLineSeparator ) {
89
+ let data = row [ i ]
90
+ data = data . split ( / \r / ) [ 0 ]
91
+ output += data
92
+ dataLength = data . length
93
+ space = ' '
106
94
}
107
- this . log ( '' + output )
108
- output = ''
95
+ for ( let j = 0 ; j < totalSize - dataLength ; j ++ ) {
96
+ output += space
97
+ }
98
+ output += seperator
99
+ }
100
+ this . log ( '' + output )
109
101
}
110
102
}
0 commit comments