From fdf3a922ffeccda5a5e5b63f8457d88842476219 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 7 Feb 2018 23:35:44 -0800 Subject: [PATCH] improve layout --- cmd.js | 32 ++++++++++++++++++++++++-------- package.json | 1 + 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cmd.js b/cmd.js index 70bf54c..e8ca2d5 100755 --- a/cmd.js +++ b/cmd.js @@ -11,6 +11,7 @@ const readPackageTree = require('read-package-tree') const RegistryClient = require('npm-registry-client') // TODO: use npm-registry-fetch when done const registryUrl = require('registry-url') const stripAnsi = require('strip-ansi') +const termSize = require('term-size') const textTable = require('text-table') const { promisify } = require('util') @@ -67,6 +68,7 @@ async function init () { const authorInfos = computeAuthorInfos(allPkgs, downloadCounts) // TODO: compute list of **projects** seeking donations + // TODO: show direct dependencies first in the list const donateLinks = [] @@ -77,10 +79,11 @@ async function init () { const authorPkgs = authorInfos[author] const donateLink = thanks.authors[author] donateLinks.push(donateLink) + const prettyDonateLink = donateLink.replace(/https?:\/\/(www\.)?/, '') return [ chalk.green(author), - donateLink, - `${authorPkgs.length} packages including ${authorPkgs.slice(0, 2).join(', ')}` + chalk.cyan(prettyDonateLink), + listWithMaxLen(authorPkgs, termSize().columns - 45) ] }) @@ -121,7 +124,7 @@ async function init () { } async function fetchPkg (pkgName) { - // The registry does not support fetching versions for scoped packages + // Note: The registry does not support fetching versions for scoped packages const url = isScopedPkg(pkgName) ? `${registryUrl()}${pkgName.replace('/', '%2F')}` : `${registryUrl()}${pkgName}/latest` @@ -154,12 +157,10 @@ function isScopedPkg (pkgName) { return pkgName.includes('/') } -/** - * A few notes: - * - bulk queries do not support scoped packages - * - bulk queries are limited to at most 128 packages at a time - */ async function bulkFetchDownloads (pkgNames) { + // A few notes: + // - bulk queries do not support scoped packages + // - bulk queries are limited to at most 128 packages at a time const downloads = {} const normalPkgNames = pkgNames.filter(pkgName => !isScopedPkg(pkgName)) @@ -204,3 +205,18 @@ function computeAuthorInfos (pkgs, downloadCounts) { return authorInfos } + +function listWithMaxLen (list, maxLen) { + const ELLIPSIS = chalk` {magenta + XX more}` + const ELLIPSIS_LENGTH = stripAnsi(ELLIPSIS).length + let str = '' + for (let i = 0; i < list.length; i++) { + const item = (i === 0 ? '' : ', ') + list[i] + if (stripAnsi(str).length + item.length >= maxLen - ELLIPSIS_LENGTH) { + str += ELLIPSIS.replace('XX', list.length - i) + break + } + str += item + } + return str +} diff --git a/package.json b/package.json index 29f04dc..703ea58 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "read-package-tree": "^5.1.6", "registry-url": "^3.1.0", "strip-ansi": "^4.0.0", + "term-size": "^1.2.0", "text-table": "^0.2.0" }, "devDependencies": {