-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bin): Improve output #42
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,20 +15,38 @@ var argv = require('yargs') | |
.alias(options) | ||
.argv | ||
|
||
var size = require('window-size') | ||
var availableWidth = size.width || /* istanbul ignore next */ 80 | ||
var ui = require('cliui')({width: availableWidth}) | ||
|
||
var processExitCode = argv.u && !argv.n ? 1 : 0 | ||
var getRuleFinder = require('../lib/rule-finder') | ||
var specifiedFile = argv._[0] | ||
|
||
var ruleFinder = getRuleFinder(specifiedFile) | ||
Object.keys(options).forEach(function findRules(option) { | ||
var rules | ||
var rules, outputRules, outputRuleCellMapper | ||
var outputPadding = ' ' | ||
var outputMaxWidth = 0 | ||
var outputMaxCols = 0 | ||
var ruleFinderMethod = ruleFinder[option] | ||
if (argv[option] && ruleFinderMethod) { | ||
rules = ruleFinderMethod() | ||
/* istanbul ignore next */ | ||
if (rules.length) { | ||
console.log('\n' + options[option][0], 'rules\n') // eslint-disable-line no-console | ||
console.log(rules.join(', ')) // eslint-disable-line no-console | ||
rules = rules.map(function columnSpecification(rule) { | ||
rule = rule + outputPadding | ||
outputMaxWidth = Math.max(rule.length, outputMaxWidth) | ||
return rule | ||
}) | ||
outputMaxCols = Math.floor(availableWidth / outputMaxWidth) | ||
outputRuleCellMapper = getOutputRuleCellMapper(Math.floor(availableWidth / outputMaxCols)) | ||
while (rules.length) { | ||
outputRules = rules.splice(0, outputMaxCols).map(outputRuleCellMapper) | ||
ui.div.apply(ui, outputRules) | ||
} | ||
console.log(ui.toString()) // eslint-disable-line no-console | ||
} else if (option === 'getUnusedRules') { | ||
processExitCode = 0 | ||
} | ||
|
@@ -38,3 +56,9 @@ Object.keys(options).forEach(function findRules(option) { | |
if (processExitCode) { | ||
process.exit(processExitCode) | ||
} | ||
|
||
function getOutputRuleCellMapper(width) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be refactoring the function (duplicate, exists in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible it will be great to see it along with this PR. Other wise just create an issue for cleaning up dups. |
||
return function curriedOutputRuleCellMapper(rule) { | ||
return {text: rule, width: width} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var fs = require('fs') | |
var eslint = require('eslint') | ||
var isAbsolute = require('path-is-absolute') | ||
var difference = require('./array-diff') | ||
var sortRules = require('./sort-rules') | ||
|
||
function _getConfigFile(specifiedFile) { | ||
if (specifiedFile) { | ||
|
@@ -71,21 +72,21 @@ function RuleFinder(specifiedFile) { | |
|
||
// get all the current rules instead of referring the extended files or documentation | ||
this.getCurrentRules = function getCurrentRules() { | ||
return currentRules | ||
return sortRules(currentRules) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have named |
||
} | ||
|
||
// get all the plugin rules instead of referring the extended files or documentation | ||
this.getPluginRules = function getPluginRules() { | ||
return pluginRules | ||
return sortRules(pluginRules) | ||
} | ||
|
||
// get all the available rules instead of referring eslint and plugin packages or documentation | ||
this.getAllAvailableRules = function getAllAvailableRules() { | ||
return allRules | ||
return sortRules(allRules) | ||
} | ||
|
||
this.getUnusedRules = function getUnusedRules() { | ||
return unusedRules | ||
return sortRules(unusedRules) | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
function sortRules(rules) { | ||
return rules.sort(function sort(a, b) { | ||
return a > b ? 1 : -1 | ||
}) | ||
} | ||
|
||
module.exports = sortRules |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var assert = require('assert') | ||
var sortRules = require('../../src/lib/sort-rules') | ||
|
||
describe('sort-rules', function() { | ||
it('should return sorted rules', function() { | ||
assert.deepEqual( | ||
sortRules(['a', 'b', 'c']), | ||
['a', 'b', 'c'] | ||
) | ||
assert.deepEqual( | ||
sortRules(['c', 'b', 'a']), | ||
['a', 'b', 'c'] | ||
) | ||
assert.deepEqual( | ||
sortRules(['aa', 'a', 'ab', 'b', 'c']), | ||
['a', 'aa', 'ab', 'b', 'c'] | ||
) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Available width for tabular display is calculated with respect to individual terminal (window) settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome!! responsive ui 👍