Skip to content

Commit 1d41bc5

Browse files
#98 : csv file viewer in console changes
1 parent 6684944 commit 1d41bc5

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

src/commands/showfile.ts

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

0 commit comments

Comments
 (0)