Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Unicode characters #373

Open
effectivecui opened this issue Oct 21, 2022 · 0 comments
Open

Support Unicode characters #373

effectivecui opened this issue Oct 21, 2022 · 0 comments

Comments

@effectivecui
Copy link

effectivecui commented Oct 21, 2022

When input unicode characters,the number of input line is not correct. see screenshot below for more details.

image

Please use string-width@5.1.2 to strip characters instead of the simple strip function in the source code below to compute the width of characters

The path of source code below is lib/util/strip.js

'use strict';

module.exports = str => {
  const pattern = [
    '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
    '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
  ].join('|');

  const RGX = new RegExp(pattern, 'g');
  return typeof str === 'string' ? str.replace(RGX, '') : str;
};

The changes maybe like this

The path of source code below is lib/util/clear.js

'use strict';

//const strip = require('./strip');
const { erase, cursor } = require('sisteransi');
const stringWidth = require('string-width');

//const width = str => [...strip(str)].length;

/**
 * @param {string} prompt
 * @param {number} perLine
 */
module.exports = function(prompt, perLine) {
  if (!perLine) return erase.line + cursor.to(0);

  let rows = 0;
  const lines = prompt.split(/\r?\n/);
  for (let line of lines) {
    rows += 1 + Math.floor(Math.max(stringWidth(line) - 1, 0) / perLine);
  }

  return erase.lines(rows);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant