Discussion:
[fpc-pascal] How to get ANSI code command return value?
Mr Bee via fpc-pascal
2018-12-02 07:29:26 UTC
Permalink
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?
Requirements:
– 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.
Regards,
–Mr Bee
Sven Barth via fpc-pascal
2018-12-02 08:59:01 UTC
Permalink
Am So., 2. Dez. 2018, 08:29 hat Mr Bee via fpc-pascal <
Post by Mr Bee via fpc-pascal
– I want to avoid CRT unit as much as possible. I prefer raw pascal
solution.
Um... The CRT unit is a raw Pascal solution?! Anyway, you can use that unit
to see how the CRT unit did it.

– The solution works across platforms, at least on Mac, Linux, and Windows.
The Windows console does not support ANSI codes. So you'll need to use an
abstraction API like the CRT unit anyway.

Regards,
Sven
Mr Bee via fpc-pascal
2018-12-02 14:19:22 UTC
Permalink
Pada tanggal Min, 2 Des 2018 pukul 15.59 Sven Barth <
Post by Sven Barth via fpc-pascal
Um... The CRT unit is a raw Pascal solution?! Anyway, you can use that
unit to see how the CRT unit did it.
Nope, I don't think so. CRT unit is Turbo Pascal's legacy unit. There are
some serious caveats with CRT unit, as explained here:
https://www.freepascal.org/docs-html/rtl/crt/index.html

The Windows console does not support ANSI codes. So you'll need to use an
Post by Sven Barth via fpc-pascal
abstraction API like the CRT unit anyway.
Windows now supports ANSI codes emulation, as explained here:
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

Regards,

–Mr Bee

Loading...