Skip to content

Commit

Permalink
Use cli-table to return the reviewers
Browse files Browse the repository at this point in the history
  • Loading branch information
Víctor Guzmán committed Nov 30, 2022
1 parent 1cf1bb4 commit 2b02b78
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/tempo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<void>): Promise<boolean> {
Expand Down
55 changes: 55 additions & 0 deletions src/timesheets/reviewersTable.ts
Original file line number Diff line number Diff line change
@@ -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)
})
)
}

0 comments on commit 2b02b78

Please sign in to comment.