Ncurses library on sololearn !
I found today that one of my favorite libraries is available on Sololearn, namely ncurses. One little hiccup though, the output shows the ansi escape sequences as plain text. Is there a way to prevent this? See what i mean here: https://code.sololearn.com/ckbvj0um9VM5/?ref=app
1/26/2021 9:46:56 PM#define CPP ChillPill
21 AnswersNew Answer
I don't believe there's a workaround: ncurses works by updating live console output, and sololearn input/output capture system cannot handle such things...
oups, less is not found. no less installed. weird cause i thought that was standard install on linux but i guess not.
ChillPill visph is correct, the i/o is handled by sololearn via plain text files only. Means even if you get the ANSI escape sequences interpreted via "less", still the final output will be piped to a plain text file only (logfile.txt which is later converted to completed.txt), making those escape sequences to again appear in final output. Check this out for more info about what SL is doing under the hood ( I didn't expect them to comment it so well ) 👇 https://code.sololearn.com/cQM7WC5FCfbA/?ref=app There are some possibe solutions I can think of to tackle this rn :- 1) strip off all the escape sequences from the output file (maybe via regex search) 2) try changing TERM variable so that ncurses doesn't send all the escape sequences with it 3) ( the Brian 's idea ) make use of SL's web code playground to interprete those escape sequences and then display it there.
imagine a ncurse interpreter that is basically a 2d array buffer. everytime you encounter a command like move cursor to y,x you get ready to write stuff at that position in the buffer. when you move up,down,left,right same thing. when you see a string you simply write it where the cursor is currently. clear screen would work too, just empty the buffer. now some commands might be missed like refresh() which is not an ansi code but a c function to shoot the buffer. but because we can only get the last frame returned to us from sololearn, doesnt matter, we can just flush the buffer once at the end of the program. ... edit as for the ansi color codes, we could just ignore them as the sololearn app can only display monochrome. same goes for many other ansi codes, we could just ignore and remove them.
https://code.sololearn.com/cpEn4JCfp4Fv/?ref=app there are a few buffer files in "usercode" one for inputs one for stdout one for stderr
actually i had another idea that would give better results. the problem is that im too lazy to do it. here s my idea. make an ansi escape sequence interpreter. so it would read the output in logfile.txt, execute every escape codes. every time ncurses refreshes, output to usercode/errors which should display in the app as well. i dont get brian idea well though. not sure how to fool the app in displaying html code?
Brian you know what, we are just brainstorming here so s thank you very much for trying find a solution. i think it is good to explore all avenues.
Martin Taylor any thoughts about a hack for this? besides making an ansi code interpreter, because im really hoping for a lazy solution before i start coding an ansi interpreter. ... edit: shoots i already started something. i guess i ll have to make this happen afterall. https://code.sololearn.com/c8wmamZE3dC7/?ref=app
ncurses is great... but only works in "normal" interactive context, not through sololearn input/output capture system ^^
Yeah seems like you re right. Still maybe there is a way to hack this and make it work in Sololearn. I m wondering.
No idea. I dont know. Does the playground return an HTML string?
I rather think that input/output are exchanged as plain text... web i/o are handled at client side... (php, html; but node.js is server sided as others languages)
oh i found something guys https://stackoverflow.com/questions/26005946/writing-string-with-ansi-escape-sequences-to-file i dont know if it s gonna work but i ll give it a try later. using "less".
ChillPill I really hope that you can figure this one out. I suppose that on SoloLearn it would have to be some kind of dynamic translator into HTML. How difficult would that be?
Redirect stdin and stdout of your C/C++ program to shared flat files or pipes that the JS can read/write during run time, and in turn update the DOM.
I also thought the same, but I thought that this is only applicable till the time we are text formatting (backspace,clearscreen etc) But could not think of a way to decode colour character and all that.
ChillPill I had naively proposed to use JS to issue a shell script to write and compile the curses-based program, run it, and redirect its i/o streams to some means of interprocess communication (IPC). From there, the JS would pick up the output and translate escape sequences into HTML directives for display on the screen. If it could work, then my own master plan would fall into place, and I could release my original 1980's interactive VT games! Alas, the IPC is the brick wall. I realized later that SoloLearn's JS runs on the client, not the server. It could not start a shell on SoloLearn's server and interact with files.
The reason ncurses does not work on SoloLearn is because stdin and stdout, or cin and cout, are not connected to the console. Remember, cin and cout are data streams not physical entities and can be redirected. The ncurses library under *nix works by using terminal specific escape codes, usually via the termcap library and database. The ms-dos ports used dos interrupts and bios calls to gain information about the screen and read the keyboard, and uses ANSI escape codes to update the display. Under Windows it uses the System.Console object to gather information and ANSI escape codes. Essentially, ncurses translates higher level abstract instructions like "clear the screen" into device specific escape codes or function calls.
There is no easy work around to this problem. Windows and Linux are fundamentally different designs. The curses library goes back decades to a time when actual physical terminals such as the vt100 were used to access servers over serial connections. Linux still uses this model to communicate with terminal windows. Programs like xterm are actual terminal emulators communicating over a virtual serial channel. You can verify this by issuing a "who" command in a Linux terminal and see which tty channel you are logged on to. The terminal window is not the shell. The terminal window is a terminal emulator that interacts with the shell. This client server model is central to the design of Unix/Linux. Even GUI applications use this model. An X Window server is used to communicate with a client. It is even possible to run an X Window program on a headless server and display the results on a different machine. MS-DOS on the other hand does not use this model. The command shell is the core of the operating system and the OS does not use a client server model for users. Windows uses a similar model. The new Windows Terminal program is reputed to support ANSI escape codes and VT terminal emulation.
Martin Taylor if I am correct, we can change the TERM environment variable before executing the script to make sure ncurses sends appropriate ANSI escape sequences for that perticular terminal. My first attempt was to change it to something very bare bones (like dumb) so that there is less to no escape sequences to work with, but it ended up removing the entire output from the console, maybe there is something I am missing here.