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

Boxx layout is incorrect when text have ansi escape characters on it #3

Closed
syrusakbary opened this issue Feb 18, 2020 · 2 comments · Fixed by #4
Closed

Boxx layout is incorrect when text have ansi escape characters on it #3

syrusakbary opened this issue Feb 18, 2020 · 2 comments · Fixed by #4

Comments

@syrusakbary
Copy link

Sometimes you want to put some color on the text inside of the Box.

Right now, if you do it Boxx will improperly calculate the length of the line where the colors are used.
See attached screenshot:

Screen Shot 2020-02-18 at 10 39 11 AM

Some code that can trigger this is:

let message = format!(
        "There's a new version of the software! {} → {}\nUpdate with {}",
       "11".red(),
        "12".green(),
        "software self-update".green().bold()
    );
Boxx::builder().border_style(BorderStyle::Round).build().display(&message);

I think this can be easily solved by using a "cleaning" function over the provided text to Boxx, so ansi characters are cleaned out, similar to the following implementation in JS:

/**
  This function removes the ansi escape characters
  (normally used for printing colors and so)
  Inspired by: https://github.com/chalk/ansi-regex/blob/master/index.js
  MIT License Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
 */
const cleanStdout = (stdout) => {
  const pattern = [
    "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
    "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))"
  ].join("|");

  const regexPattern = new RegExp(pattern, "g");
  return stdout.replace(regexPattern, "");
};

More context on the issue here: wasmerio/wapm-cli#169

@syrusakbary syrusakbary changed the title Boxx layout is incorrect when text have ansi characters on it Boxx layout is incorrect when text have ansi escape characters on it Feb 18, 2020
@MarkMcCaskey
Copy link

Posted on the Wapm issue, but I'll post it here too, https://crates.io/crates/strip-ansi-escapes might be what you need here!

@EverlastingBugstopper
Copy link
Owner

Thanks for the reports and repros y'all - this should be fixed in 0.0.2-beta, please let me know if you run into this again or any other issues 😄

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

Successfully merging a pull request may close this issue.

3 participants