finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
858 bytes
|
| Line | |
|---|
| 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) {
|
|---|
| 9 | return str
|
|---|
| 10 | }
|
|---|
| 11 | if (target <= 0) {
|
|---|
| 12 | return ''
|
|---|
| 13 | }
|
|---|
| 14 | if (stringWidth(str) <= target) {
|
|---|
| 15 | return str
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | // We compute the number of bytes of ansi sequences here and add
|
|---|
| 19 | // that to our initial truncation to ensure that we don't slice one
|
|---|
| 20 | // that we want to keep in half.
|
|---|
| 21 | var noAnsi = stripAnsi(str)
|
|---|
| 22 | var ansiSize = str.length + noAnsi.length
|
|---|
| 23 | var truncated = str.slice(0, target + ansiSize)
|
|---|
| 24 |
|
|---|
| 25 | // we have to shrink the result to account for our ansi sequence buffer
|
|---|
| 26 | // (if an ansi sequence was truncated) and double width characters.
|
|---|
| 27 | while (stringWidth(truncated) > target) {
|
|---|
| 28 | truncated = truncated.slice(0, -1)
|
|---|
| 29 | }
|
|---|
| 30 | return truncated
|
|---|
| 31 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.