Skip to content

Commit 217bb45

Browse files
authored
Merge pull request #100 from codingtools/feature/csv_show_tool
Feature/csv show tool,
2 parents 7164bcb + 7edf5af commit 217bb45

File tree

5 files changed

+1074
-10
lines changed

5 files changed

+1074
-10
lines changed

src/commands/view.ts

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

test/commands/avro.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ describe('avro', () => {
4949
expect(ctx.stdout).to.contain('Schema file is not provided')
5050
})
5151

52-
53-
// TODO: BUG this is just skipping test , find a way to implement wait() etc.
54-
test
55-
.stdout()
56-
.command(['avro', '-f', 'test/resources/avro/person.avro', 'get_schema'])
57-
.it('if get_schema outputs to console', ctx => {
58-
setTimeout(() =>
59-
expect(ctx.stdout).to.contain('success')
60-
, 5000) // wait for it to write stuff on console
61-
})
52+
//
53+
// // TODO: this was is just skipping test , find a way to implement wait() etc.
54+
// test
55+
// .stdout()
56+
// .command(['avro', '-f', 'test/resources/avro/person.avro', 'get_schema'])
57+
// .it('if get_schema outputs to console', ctx => {
58+
// setTimeout(() =>
59+
// expect(ctx.stdout).to.contain('success')
60+
// , 9000) // wait for it to write stuff on console
61+
// })
6262

6363
test
6464
.stdout()

test/commands/view.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {expect, test} from '@oclif/test'
2+
3+
describe('view', () => {
4+
test
5+
.stdout()
6+
.command(['view'])
7+
.exit(0)
8+
.it('File path not passed', ctx => {
9+
expect(ctx.stdout).to.contain('File path not passed')
10+
})
11+
12+
test
13+
.stdout()
14+
.command(['view', 'test/resources/csv/test_view.csv'])
15+
.it('check 10 lines are found', ctx => {
16+
expect(ctx.stdout).to.contain('|1 |2 |1 |14.0 |30.0708|1 |0 |')
17+
})
18+
test
19+
.stdout()
20+
.command(['view', 'test/resources/csv/test_view.csv', '-n', '5'])
21+
.it('check 5th line is found', ctx => {
22+
expect(ctx.stdout).to.contain('|0 |3 |0 |35.0|8.05 |0 |0 |')
23+
})
24+
25+
test
26+
.stdout()
27+
.command(['view', 'test/resources/csv/test_view.csv', '-n', '900'])
28+
.it('check if the given number is greater than total lines', ctx => {
29+
expect(ctx.stdout).to.contain('|0 |3 |0 |32.0 |7.75 |0 |0 |')
30+
})
31+
32+
test
33+
.stdout()
34+
.command(['view', 'test/resources/csv/test_view.csv', '-n', '900'])
35+
.it('check if 900 count is used, it shows only total present rows', ctx => {
36+
expect(ctx.stdout).to.contain('showing top 891 records.')
37+
})
38+
39+
test
40+
.stdout()
41+
.command(['view', 'test/resources/csv/test_view.csv', '-n', '0'])
42+
.it('check if the given number is invalid then show default 10 lines', ctx => {
43+
expect(ctx.stdout).to.contain('|1 |2 |1 |14.0 |30.0708|1 |0 |')
44+
})
45+
})

0 commit comments

Comments
 (0)