From 2b02b7824a00dcf30ac21f18c973aab832f0ace8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Guzm=C3=A1n?= Date: Fri, 18 Nov 2022 19:47:37 +0100 Subject: [PATCH] Use cli-table to return the reviewers --- src/tempo.ts | 14 +++----- src/timesheets/reviewersTable.ts | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/timesheets/reviewersTable.ts diff --git a/src/tempo.ts b/src/tempo.ts index ad28336..524686e 100644 --- a/src/tempo.ts +++ b/src/tempo.ts @@ -3,6 +3,7 @@ import worklogs, { AddWorklogInput } from './worklogs/worklogs' import timesheets, { SubmitTimesheetInput } from './timesheets/timesheets' import prompts from './config/prompts' import * as worklogsTable from './worklogs/worklogsTable' +import * as reviewersTable from './timesheets/reviewersTable' import chalk from 'chalk' import { appName } from './appName' import { trimIndent } from './trimIndent' @@ -206,16 +207,11 @@ export default { return execute(async () => { cli.action.start('Getting reviewers') const reviewers = await timesheets.getReviewers() - cli.action.stop(`Done.Reviewers for the current user:`) - console.log(`Reviewers for the current user:`) - console.log(`AccountId\t\t\t|\tDisplay Name`) - console.log(`=========\t\t\t|\t============`) - reviewers.forEach(reviewer => { - console.log(`${reviewer.accountId}\t|\t${reviewer.displayName}`) - }); - + cli.action.stop('Done.') + const table = await reviewersTable.render(reviewers) + console.log(table.toString()) }) - }, + } } async function execute(action: () => Promise): Promise { diff --git a/src/timesheets/reviewersTable.ts b/src/timesheets/reviewersTable.ts new file mode 100644 index 0000000..de2781f --- /dev/null +++ b/src/timesheets/reviewersTable.ts @@ -0,0 +1,55 @@ +import Table, { HorizontalTable, Cell } from 'cli-table3' +import chalk from 'chalk' +import { UserEntity } from '../api/api' + +export async function render(reviewers: UserEntity[]) { + const { reviewerHeaders, columnsNumber } = generateReviewerHeaders() + const infoHeaders = generateInfoHeaders(columnsNumber) + const content = await generateContent(reviewers, columnsNumber) + const table = new Table() as HorizontalTable + table.push( + ...infoHeaders, + reviewerHeaders, + ...content + ) + return table +} + +function generateReviewerHeaders() { + const headers = [ + { content: chalk.bold.greenBright('Display Name'), hAlign: 'left' }, + { content: chalk.bold.greenBright('AccountId'), hAlign: 'right' } + ] + return { + reviewerHeaders: headers.map((r) => r as Cell), + columnsNumber: headers.length + } +} + +function generateInfoHeaders(colSpan: number) { + return [ + [{ colSpan: colSpan, hAlign: 'center', content: chalk.bold('Reviewers for the current user:') }] + ].map((r) => r as Cell[]) +} + +async function generateContent(reviewers: UserEntity[], colSpan: number) { + let content = await generateUserContent(reviewers) + if (content.length === 0) { + content = [ + [{ colSpan: colSpan, content: chalk.redBright('No reviewers'), hAlign: 'center' }] + ] + } + return content.map((r) => r as Cell[]) +} + +async function generateUserContent(reviewers: UserEntity[]) { + return Promise.all( + reviewers.map(async (reviewer) => { + const tableContent = { + displayName: { colSpan: 1, content: chalk.yellow(reviewer.displayName), hAlign: 'left' }, + accountId: { colSpan: 1, content: reviewer.accountId, hAlign: 'left' } + } + return Object.values(tableContent) + }) + ) +}