Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
feat(pull-request): Add table formatting to pull requests list
Browse files Browse the repository at this point in the history
close #589
  • Loading branch information
hamxabaig authored and Ryan Garant committed Feb 5, 2019
1 parent 026b657 commit 5b1f35b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
15 changes: 4 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"update-notifier": "2.5.0",
"userhome": "1.0.0",
"which": "1.3.1",
"word-wrap": "^1.2.3",
"wordwrap": "1.0.0"
},
"devDependencies": {
Expand Down
88 changes: 87 additions & 1 deletion src/cmds/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
// -- Requires -------------------------------------------------------------------------------------

import * as async from 'async'
import * as Table from 'cli-table'
import { startsWith } from 'lodash'
import marked from 'marked'
import TerminalRenderer from 'marked-terminal'
import * as openUrl from 'opn'
import wrap from 'word-wrap'
import * as base from '../base'
import * as git from '../git'
import * as hooks from '../hooks'
Expand Down Expand Up @@ -457,6 +461,84 @@ PullRequest.prototype.getPullsTemplateJson_ = function(pulls, opt_callback) {
opt_callback && opt_callback(null, json)
}

PullRequest.prototype.printPullsInfoTable_ = function(pulls) {
var options = this.options
var tableHead = ['#', 'Title', 'Author', 'Opened', 'Status']
var showDetails = options.info || options.detailed

if (showDetails) {
tableHead[1] = 'Details'
}

function getColWidths() {
var cols = process.stdout.columns
var noCol = 0.07 * cols,
titleCol = 0.53 * cols,
authorCol = 0.16 * cols,
dateCol = 0.13 * cols,
statusCol = 0.07 * cols

return [noCol, titleCol, authorCol, dateCol, statusCol].map(function(col) {
return Math.floor(col)
})
}

function getPRBody(pull, length) {
var title = wrap(pull.title, { indent: '', width: length })
var body = ''

if (showDetails) {
marked.setOptions({
renderer: new TerminalRenderer(),
})
body += logger.colors.blue('Title:')
body += '\n\n'
body += title
body += '\n\n'
body += logger.colors.blue('Body:')
body += '\n\n'
body += marked(wrap(pull.body || 'N/A', { indent: '', width: length }))
} else {
body += title
}

if (options.link || showDetails) {
body += '\n' + logger.colors.blue(pull.html_url)
}

return body
}

var tableWidths = getColWidths()
var table = new Table({
head: tableHead,
colWidths: tableWidths,
})

pulls.forEach(function(pull) {
var status = ''
var columns = []

switch (pull.combinedStatus) {
case 'success':
status = logger.colors.green(' ✓')
break
case 'failure':
status = logger.colors.red(' ✗')
break
}

columns.push('#' + pull.number)
columns.push(getPRBody(pull, tableWidths[1] - 5))
columns.push(logger.colors.magenta('@' + pull.user.login))
columns.push(logger.getDuration(pull.created_at))
columns.push(status)

table.push(columns)
})
logger.log(table.toString())
}

PullRequest.prototype.printPullInfo_ = function(pull) {
const options = this.options

Expand Down Expand Up @@ -621,7 +703,11 @@ PullRequest.prototype.list = function(user, repo, opt_callback) {
json.branches.forEach(branch => {
logger.log(`${branch.name} (${branch.total})`)

branch.pulls.forEach(instance.printPullInfo_, instance)
if (config.output === 'table') {
instance.printPullsInfoTable_(branch.pulls)
} else {
branch.pulls.forEach(instance.printPullInfo_, instance)
}
})
}

Expand Down

0 comments on commit 5b1f35b

Please sign in to comment.