0

I am trying to execute special character functionality in jQuery terminal. I was able to execute backspace functionality successfully, but I am facing a problem while executing escape functionality.

Here is the code I tried to execute. Regex used:

(^|[^\x08]|\r\n|&[^;]+\x08|[\x1B[K]

Input string:

Checking current state.\t[    ]\b\b\b\b\b-\r\u001B[KChecking current state.\t[    ]\b\b\b\b\bFAIL\n\r\u001B

Expected final output:

Checking current state.      [FAIL}

here \x1B[K is the special characters, whose functionality i would like to execute.(\x1B[K = clear line)

dom cobb
  • 25
  • 1
  • 5
  • Sorry, it is not quite clear: do you need to remove a line starting with `\x1B[K` or a line the contains it? Try http://jsfiddle.net/jwvqpaur/3/ if the former is correct, but I still can't get your expected output. – Wiktor Stribiżew Jul 09 '18 at 10:14
  • i wanted to clear the entire line when we get "\u001B[K" this sequence.("\u001B[K" this is nothing but "ESC [K", its functionality is to remove all the preceding characters in line) – dom cobb Jul 09 '18 at 10:44
  • Then again, I still don't quite get with [this code](http://jsfiddle.net/jwvqpaur/7/). – Wiktor Stribiżew Jul 09 '18 at 10:48

1 Answers1

0

Here is solution:

$.terminal.defaults.formatters.unshift([
    /(^|[^\x08]|[\r\n]{2}|&[^;]+;)\x08|[^\r\n]+\r\x1B(?:\[|[)K/g, '', {loop: true}
]);

It almost work except that last ] because regex is removing character before \b and not moving the cursor.

(?:\[|[)K is needed because command line accept first encode input with html entities, so you can't type formatting.

options are new feature for version 1.18.0 that replace in a loop until it don't match the regex.

var term = $('body').terminal();


$.terminal.defaults.formatters.unshift([
    /(^|[^\x08]|[\r\n]{2}|&[^;]+;)\x08|[^\r\n]+\r\x1B(?:\[|[)K/g, '', {loop: true}
]);
var str = 'Checking current state.\t[    ]\b\b\b\b\b-\r\u001B[KChecking current state.\t[    ]\b\b\b\b\bFAIL\n\r\u001B';
term.insert(str).echo(str);
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script src="https://rawgit.com/jcubic/leash/master/lib/wcwidth.js"></script><script src="https://unpkg.com/jquery.terminal/js/jquery.terminal.js"></script><script src="https://rawgit.com/inexorabletash/polyfill/master/keyboard.js"></script><script src="https://unpkg.com/jquery.terminal/js/unix_formatting.js"></script><script src="https://unpkg.com/prismjs@1.8.1/prism.js"></script><script src="https://unpkg.com/jquery.terminal/js/less.js"></script><script src="https://unpkg.com/jquery.terminal/js/prism.js"></script><link rel="stylesheet prefetch" href="https://unpkg.com/prismjs/themes/prism-coy.css"><link rel="stylesheet prefetch" href="https://unpkg.com/jquery.terminal/css/jquery.terminal.css">

EDIT

The fix with better overtyping is in devel branch in unix_formatting file but it require more to work to make it work with command line right now it only work with echo.

var term = $('body').terminal();

var str = 'Checking current state.\t[    ]\b\b\b\b\b-\r\u001B[KChecking current state.\t[    ]\b\b\b\b\bFAIL\r\n';
term.insert(str).echo(str);
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script src="https://rawgit.com/jcubic/leash/master/lib/wcwidth.js"></script><script src="https://rawgit.com/jcubic/jquery.terminal/devel/js/jquery.terminal.js"></script><script src="https://rawgit.com/inexorabletash/polyfill/master/keyboard.js"></script><script src="https://rawgit.com/jcubic/jquery.terminal/devel/js/unix_formatting.js"></script><script src="https://unpkg.com/prismjs@1.8.1/prism.js"></script><script src="https://unpkg.com/jquery.terminal/js/less.js"></script><script src="https://unpkg.com/jquery.terminal/js/prism.js"></script><link rel="stylesheet prefetch" href="https://unpkg.com/prismjs/themes/prism-coy.css"><link rel="stylesheet prefetch" href="https://unpkg.com/jquery.terminal/css/jquery.terminal.css">

This functions is already replacing naive version of overtyping in unix_formatting. But for fully working I'll need update the API to allow to return string and position from function formatter like with tracking_replace utility used for regex formatters that can change length of the string, without this you will not have correct position when you insert this into terminal (using copy/paste or using insert function), but it will work for echo.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • @domcobb the updated code is in devel branch on github I've needed to fix the overtyping function (string like A\x08A or A\x08_) it required one change in jQuery Terminal to not remove \r before applying formatters further API changes will also be required so it work with command line (moving cursor is not correct). Right now it only work with echo. – jcubic Jul 11 '18 at 06:42