| 138 | } |
| 139 | |
| 140 | formatResults (args, results) { |
| 141 | const cols = Math.min(process.stdout.columns || Infinity, 80) + 1 |
| 142 | |
| 143 | const formattedOutput = results.map(res => { |
| 144 | const out = [res.cmd] |
| 145 | const r = Object.keys(res.hits) |
| 146 | .map(k => `${k}:${res.hits[k]}`) |
| 147 | .sort((a, b) => a > b ? 1 : -1) |
| 148 | .join(' ') |
| 149 | |
| 150 | out.push(' '.repeat((Math.max(1, cols - out.join(' ').length - r.length - 1)))) |
| 151 | out.push(r) |
| 152 | |
| 153 | if (!this.npm.config.get('long')) { |
| 154 | return out.join('') |
| 155 | } |
| 156 | |
| 157 | out.unshift('\n\n') |
| 158 | out.push('\n') |
| 159 | out.push('-'.repeat(cols - 1) + '\n') |
| 160 | res.lines.forEach((line, i) => { |
| 161 | if (line === null || i > 3) { |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | const highlightLine = [] |
| 166 | for (const arg of args) { |
| 167 | const finder = line.toLowerCase().split(arg.toLowerCase()) |
| 168 | let p = 0 |
| 169 | for (const f of finder) { |
| 170 | highlightLine.push(line.slice(p, p + f.length)) |
| 171 | const word = line.slice(p + f.length, p + f.length + arg.length) |
| 172 | highlightLine.push(this.npm.chalk.blue(word)) |
| 173 | p += f.length + arg.length |
| 174 | } |
| 175 | } |
| 176 | out.push(highlightLine.join('') + '\n') |
| 177 | }) |
| 178 | |
| 179 | return out.join('') |
| 180 | }).join('\n') |
| 181 | |
| 182 | const finalOut = results.length && !this.npm.config.get('long') |
| 183 | ? 'Top hits for ' + (args.map(JSON.stringify).join(' ')) + '\n' + |
| 184 | '—'.repeat(cols - 1) + '\n' + |
| 185 | formattedOutput + '\n' + |
| 186 | '—'.repeat(cols - 1) + '\n' + |
| 187 | '(run with -l or --long to see more context)' |
| 188 | : formattedOutput |
| 189 | |
| 190 | return finalOut.trim() |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | module.exports = HelpSearch |