Git Product home page Git Product logo

Comments (2)

tecfu avatar tecfu commented on July 19, 2024

Options:

  1. Embed a static array of emojis within the smartwrap module, and then refuse to wrap them.
  2. Create option to set an array of character combinations that cannot be split, then pass that array to smartwrap.

Preferred option:
2.

from tty-table.

mscalora avatar mscalora commented on July 19, 2024

Doesn't the spread op [..."ccc 😀😃😄😁 eee"] idom correctly show where you can't split?

I implemented a string truncate using it that appears to always split chars in the right place.

// eslint-disable-next-line no-control-regex
const ansiMatcher = new RegExp("([^\u001B\u009B]*)([\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))|$)", "g")

/**
 * split a unicode string into code points
 * @param {string} s - string with unicode
 * @return {Array<string>} - array of code points
 */
const splitUnicode = s => [...s]

/**
 * truncate a unicode string that may contain ansi and/or multi-column unicode characters
 * @param {string} s - string with unicode and/or multi-column unicode characters
 * @param {number} length - width in terminal char column
 * @return {string}
 */
Format.truncate = (s, length) => {
  let total = 0
  return s.replace(ansiMatcher, function (match, content, ansi) {
    if (total === length || 0) {
      // skip all visible content
      return ansi
    }
    let contentLength = Wcwidth(content)
    if (contentLength <= length - total) {
      // pass all visible content
      total += contentLength
      return content + ansi
    }
    // partial chunk
    let chars = splitUnicode(content)
    while (Wcwidth(chars.join("")) > length - total) {
      chars.pop()
    }
    content = chars.join("")
    // set total so the rest of content is skipped
    total = length
    return content + ansi
  })
}

-Mike

from tty-table.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.