Skip to content

Commit

Permalink
feat(pull-request): Add table formatting to pull requests list
Browse files Browse the repository at this point in the history
  • Loading branch information
hamxabaig authored and Ryan Garant committed Jan 5, 2019
1 parent 7ac9459 commit 506c1f7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 21 deletions.
28 changes: 8 additions & 20 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@
},
"dependencies": {
"async": "1.5.2",
"cli-table": "^0.3.1",
"colors": "1.3.3",
"github": "~0.2.4",
"handlebars": "4.0.12",
"inquirer": "6.2.1",
"lodash": "4.17.11",
"marked": "^0.5.2",
"marked-terminal": "^3.1.1",
"moment": "2.23.0",
"nopt": "3.0.6",
"opn": "5.4.0",
Expand All @@ -101,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 506c1f7

Please sign in to comment.