Last change
on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
828 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict'
|
---|
| 2 | var stringWidth = require('string-width')
|
---|
| 3 | var stripAnsi = require('strip-ansi')
|
---|
| 4 |
|
---|
| 5 | module.exports = wideTruncate
|
---|
| 6 |
|
---|
| 7 | function wideTruncate (str, target) {
|
---|
| 8 | if (stringWidth(str) === 0) return str
|
---|
| 9 | if (target <= 0) return ''
|
---|
| 10 | if (stringWidth(str) <= target) return str
|
---|
| 11 |
|
---|
| 12 | // We compute the number of bytes of ansi sequences here and add
|
---|
| 13 | // that to our initial truncation to ensure that we don't slice one
|
---|
| 14 | // that we want to keep in half.
|
---|
| 15 | var noAnsi = stripAnsi(str)
|
---|
| 16 | var ansiSize = str.length + noAnsi.length
|
---|
| 17 | var truncated = str.slice(0, target + ansiSize)
|
---|
| 18 |
|
---|
| 19 | // we have to shrink the result to account for our ansi sequence buffer
|
---|
| 20 | // (if an ansi sequence was truncated) and double width characters.
|
---|
| 21 | while (stringWidth(truncated) > target) {
|
---|
| 22 | truncated = truncated.slice(0, -1)
|
---|
| 23 | }
|
---|
| 24 | return truncated
|
---|
| 25 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.