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 15, 2019
1 parent 999146d commit 1a78aeb
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 13 deletions.
73 changes: 72 additions & 1 deletion lib/cmds/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
Object.defineProperty(exports, "__esModule", { value: true });
// -- Requires -------------------------------------------------------------------------------------
const async = require("async");
const Table = require("cli-table");
const lodash_1 = require("lodash");
const marked_1 = require("marked");
const marked_terminal_1 = require("marked-terminal");
const openUrl = require("opn");
const word_wrap_1 = require("word-wrap");
const base = require("../base");
const git = require("../git");
const hooks = require("../hooks");
Expand Down Expand Up @@ -386,6 +390,68 @@ 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 = word_wrap_1.default(pull.title, { indent: '', width: length });
var body = '';
if (showDetails) {
marked_1.default.setOptions({
renderer: new marked_terminal_1.default(),
});
body += logger.colors.blue('Title:');
body += '\n\n';
body += title;
body += '\n\n';
body += logger.colors.blue('Body:');
body += '\n\n';
body += marked_1.default(word_wrap_1.default(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;
let status = '';
Expand Down Expand Up @@ -521,7 +587,12 @@ PullRequest.prototype.list = function (user, repo, opt_callback) {
logger.log(logger.colors.yellow(`${user}/${repo}`));
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);
}
});
}
opt_callback && opt_callback(err);
Expand Down
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 1a78aeb

Please sign in to comment.