[fpc-pascal] How to get ANSI code command return value?

Mr Bee pak.lebah at yahoo.com
Sun Dec 2 08:29:26 CET 2018

Hi all,
I'm playing around with ANSI code (ESC sequence) on the console. Sending an escape command is trivial such as changing text's color and background. However, getting a response isn't as easy as I thought. Here's a simple test program:
program ANSItest;
var  s: string;begin  // send ESC command  write('Cursor position: ');  write(#27'[6n');  read(s);  // read ESC result  write('Received string: ');  writeln(s);end.
The program sends <ESC>[6n command to query current cursor position and would return <ESC>[<line>;<column>R string. As the result is sent back to standard input, I think using the standard read() procedure would be enough. Unfortunately, that's not the case. Here are the problems:
1. read() procedure keeps waiting until the user presses return/enter key while I hope it could automatically stop reading as the input buffer gets empty. Is there a way to make read() doesn't wait for return/enter key?
2. read() procedure strangely doesn't read the response given by the console (into s variable) although it's clearly printed on the screen. It's proven by the last writeln(s) line that produces nothing or an empty text. What's wrong with it?
3. The return value of an ESC command query is printed on the console. I don't want this. I want to send the ESC command silently, fortunately the standard write() procedure is able to do just that. I also want to get the response value silently. Is there a way to temporarily redirect standard input into a variable?
– I want to avoid CRT unit as much as possible. I prefer raw pascal solution.– The solution works across platforms, at least on Mac, Linux, and Windows.
Is it possible using standard Pascal routines?
Thank you.
–Mr Bee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20181202/d60433c5/attachment.html>

More information about the fpc-pascal mailing list