node subroutine return data encoding process with ANSI

node child returns data encoded with ANSI

What is ANSI?

Have described here under the action of the terminal in the ANSI, ANSI escape sequence with an escape sequence is a standard internal signal for controlling the cursor position on the videotex terminal, color, and other options. Determining the sequence of bytes embedded in the text, and most escape character ESC to "[" character starts, the terminal will be interpreted as a sequence of bytes corresponding to these instructions, instead of the ordinary character encoding.

That is output to the node supposed to our string displayed on the terminal, and this is formatted, for example, standard red error, line screen is cleared, and the like. Here is the ANSI string band:

In fact, only useful information here
File "test.py", Line 2
pythonEditor.vue aa11:? 652
SyntaxError: invalid syntax
, there are some strange characters · [2 and so on, these are control characters ASNI the

point here is the meaning of ESC which I find is a ESC] strange characters, where the brackets and other anti-matter, and found through the terminal ESC] back to the characters between the characters are under the control command is ignored. Is "C: \ Users \ admin \ AppData \ Local \ Programs \ codesprite-test \ resources \ app \ dist \ electron \ static \ Python3 \ python.exe" this is not required.
Knowing this we can deal with, first find the ANSI get regular.

const ansiRegex = ({onlyFirst = false} = {}) => {
  const pattern = [
    '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
    '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
  ].join('|');
  return new RegExp(pattern, onlyFirst ? undefined : 'g');
}

By regular ANSI can all characters in character matches out, then delete it, and then special treatment ESC] string behind

function filter (text) {
  const arr = text.match(ansiRegex())
  arr.forEach((ele, idx) => {
    if (ele.includes(']')) {
      const _idx = text.indexOf(ele)
      if (idx !== arr.length - 1) {
        const next = arr[idx + 1]
        const _idx2 = text.indexOf(next) + next.length
        const as=text.slice(_idx, _idx2)
        text = text.replace(as, '')
      } else {
        const as = text.slice(_idx, text.length - 1)
        text = text.replace(as, '')
      }
    }
    text = text.replace(ele, '')
  })
  return text
}

This method is a simple process ansi characters.

Guess you like

Origin www.cnblogs.com/suyuanli/p/12442534.html