"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/gauge/wide-truncate.js" (8 Mar 2017, 828 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 }