You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Scheme REPL written in Node.js where I implemented:
syntax highlighting
parentheses matching
auto indentation
It works perfectly, unless the lines are wrapped because they are too long.
This is an example when everything is working:
And this is an example when it gets nuts:
The thing that is messed up is \e[<N>F ANSI escape code. I move up the cursor that many times as there are lines and overwrite existing code. So it's always syntax highlighted.
This is the function:
// function accept ANSI (syntax highlighted code) and// return ANSI escapes to overwrite the code// this is needed to make sure that syntax highlighting// is always correctfunctionansi_rewrite_above(ansi_code){constlines=ansi_code.split('\n');conststdout=lines.map((line,i)=>{constprefix=i===0 ? prompt : continue_prompt;return'\x1b[K'+prefix+line;}).join('\n');constlen=lines.length;// overwrite all lines to get rid of any artifacts left my stdin// mostly because of parenthesis matchinglog_error(`\x1b[${len}F`);return`\x1b[${len}F${stdout}\n`;}
But this is more of the Terminal ANSI question than code, since the code works fine, only [<n>F escape code doesn't work when lines are wrapped. As you can see, I also use [K escape code to clear the line so you can print new text over it. ansi_code parameter receive ANSI encoded scheme code, but it's only coloring so it's making no difference (to be sure I tested by removing colors and the effect is the same).
The parentheses matching use same function, it only marks matching parenthesis with \e[7m (reverse colors).
Why there is such odd behavior when the lines are wrapped? And how to fix this? I use Xfce terminal on Fedora, but I'm not sure if this makes any difference.
I was thinking to calculate the overflow and move more lines up
Is there a way to format the output so when you resize the terminal window, it resizes the text to make it fit and don't stay wrapped?
Note that I don't have any understanding what is happening to the terminal when it wraps lines and how it affects the ANSI escape codes.
I was trying to calculate the overflow with this code:
Basically I check how many lines overflow. By adding this offset to the ansi_rewrite_above (into \e[<num>F) fixes the issue when I type longer text and press enter. But it doesn't fix the issue in above gif.
on every keystroke (I use undocumented rl._writeToOutput to hook into stdin and highlight it), this code highlights the matching parenthesis. I was trying to also add calculate_overflow for the input, but it doesn't work.
If I comment out this line, everything is working, so the issue is \e[1F executed from ansi_rewrite_above when the line is longer than the width of the terminal.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a Scheme REPL written in Node.js where I implemented:
It works perfectly, unless the lines are wrapped because they are too long.
This is an example when everything is working:
And this is an example when it gets nuts:
The thing that is messed up is
\e[<N>F
ANSI escape code. I move up the cursor that many times as there are lines and overwrite existing code. So it's always syntax highlighted.This is the function:
But this is more of the Terminal ANSI question than code, since the code works fine, only
[<n>F
escape code doesn't work when lines are wrapped. As you can see, I also use[K
escape code to clear the line so you can print new text over it.ansi_code
parameter receive ANSI encoded scheme code, but it's only coloring so it's making no difference (to be sure I tested by removing colors and the effect is the same).The parentheses matching use same function, it only marks matching parenthesis with
\e[7m
(reverse colors).Why there is such odd behavior when the lines are wrapped? And how to fix this? I use Xfce terminal on Fedora, but I'm not sure if this makes any difference.
I was thinking to calculate the overflow and move more lines up
Is there a way to format the output so when you resize the terminal window, it resizes the text to make it fit and don't stay wrapped?
Note that I don't have any understanding what is happening to the terminal when it wraps lines and how it affects the ANSI escape codes.
I was trying to calculate the overflow with this code:
Basically I check how many lines overflow. By adding this offset to the
ansi_rewrite_above
(into\e[<num>F
) fixes the issue when I type longer text and press enter. But it doesn't fix the issue in above gif.What I use in Node.js is to call :
on every keystroke (I use undocumented
rl._writeToOutput
to hook into stdin and highlight it), this code highlights the matching parenthesis. I was trying to also add calculate_overflow for the input, but it doesn't work.If I comment out this line, everything is working, so the issue is
\e[1F
executed fromansi_rewrite_above
when the line is longer than the width of the terminal.Here is a simple reproduction of the issue:
https://github.com/jcubic/node-repl
Beta Was this translation helpful? Give feedback.
All reactions