'use strict'
var stringWidth = require('string-width')
var stripAnsi = require('strip-ansi')
module.exports = wideTruncate
function wideTruncate (str, target) {
if (stringWidth(str) === 0) return str
if (target <= 0) return ''
if (stringWidth(str) <= target) return str
// We compute the number of bytes of ansi sequences here and add
// that to our initial truncation to ensure that we don't slice one
// that we want to keep in half.
var noAnsi = stripAnsi(str)
var ansiSize = str.length + noAnsi.length
var truncated = str.slice(0, target + ansiSize)
// we have to shrink the result to account for our ansi sequence buffer
// (if an ansi sequence was truncated) and double width characters.
while (stringWidth(truncated) > target) {
truncated = truncated.slice(0, -1)
}
return truncated
}
| Name | Type | Size | Permission | Actions |
|---|---|---|---|---|
| node_modules | Folder | 0755 |
|
|
| CHANGELOG.md | File | 6.23 KB | 0644 |
|
| LICENSE | File | 751 B | 0644 |
|
| README.md | File | 14.72 KB | 0644 |
|
| base-theme.js | File | 395 B | 0644 |
|
| error.js | File | 616 B | 0644 |
|
| has-color.js | File | 292 B | 0644 |
|
| index.js | File | 6.83 KB | 0644 |
|
| package.json | File | 2.13 KB | 0644 |
|
| plumbing.js | File | 1.24 KB | 0644 |
|
| process.js | File | 89 B | 0644 |
|
| progress-bar.js | File | 998 B | 0644 |
|
| render-template.js | File | 5.6 KB | 0644 |
|
| set-immediate.js | File | 139 B | 0644 |
|
| set-interval.js | File | 93 B | 0644 |
|
| spin.js | File | 105 B | 0644 |
|
| template-item.js | File | 1.86 KB | 0644 |
|
| theme-set.js | File | 3.61 KB | 0644 |
|
| themes.js | File | 1.51 KB | 0644 |
|
| wide-truncate.js | File | 828 B | 0644 |
|