|
| 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 | +} |
0 commit comments