Recent

Author Topic: strip color from TProcess output [solved]  (Read 1231 times)

Michael Collier

  • Sr. Member
  • ****
  • Posts: 318
strip color from TProcess output [solved]
« on: June 11, 2024, 10:35:03 pm »
Hi

I use TProcess to execute nodejs application, which in turn outputs characters which I'm guessing are for colour control.

The colours don't show up in lazarus memo, I'll try pasting below but may not paste properly, I guess I could scan the output and strip characters within certain range, has anyone done this before? Or suggest different component to display console output?

'TEXT_HERE'

« Last Edit: July 08, 2024, 06:03:19 pm by Michael Collier »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11655
  • FPC developer.
Re: strip color from TProcess output
« Reply #1 on: June 11, 2024, 11:13:50 pm »
Those are probably so called ansi escape codes. Google and find out....

Roland57

  • Sr. Member
  • ****
  • Posts: 447
    • msegui.net
Re: strip color from TProcess output
« Reply #2 on: June 11, 2024, 11:47:14 pm »
Hi.

If I were you, I would execute one time the application with redirection of the output to a file, so that I have a good sample of the output. After that I would write a function for cleaning the output, maybe using regular expressions.

Or I would check if the application hasn't an option to disable colored output.
My projects are on Gitlab and on Codeberg.

Michael Collier

  • Sr. Member
  • ****
  • Posts: 318
Re: strip color from TProcess output
« Reply #3 on: June 11, 2024, 11:58:30 pm »
I guess I'll have to strip the codes out. I've tried using online package manager to install rich edit controls  to display the colours (rather than stripping them out). But in each case the IDE reports that a file cannot be found (I'm on linux at the moment) - despite including the relevant package and being able to drop the control on a form at design time.

packages:
LazRichView
RichMemo


Roland57

  • Sr. Member
  • ****
  • Posts: 447
    • msegui.net
Re: strip color from TProcess output
« Reply #4 on: June 12, 2024, 08:16:46 am »
I don't believe that these components (RichMemo, LazRichView) can be useful in that case.
My projects are on Gitlab and on Codeberg.

cdbc

  • Hero Member
  • *****
  • Posts: 1361
    • http://www.cdbc.dk
Re: strip color from TProcess output
« Reply #5 on: June 12, 2024, 08:24:08 am »
Hi
What marcov said, they look like 'ansi escape codes', the units: crt, video, keybord amo, uses them, you can too, have a look here: https://github.com/Warfley/LazTermUtils if you need to parse them to show the colors...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Edson

  • Hero Member
  • *****
  • Posts: 1306
Re: strip color from TProcess output
« Reply #6 on: June 13, 2024, 12:47:56 am »
Those characters are not only coloring escaping sequences but screen control sequences too.

You can use https://github.com/t-edson/UnTerminal. It's a wrapper for TProcess and recognizes most of the VT100 sequences screen controls, ignoring color sequences so you can show it in a simple Textbox.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

MarkMLl

  • Hero Member
  • *****
  • Posts: 7122
Re: strip color from TProcess output
« Reply #7 on: June 13, 2024, 03:28:50 pm »
Also a possible xref to https://forum.lazarus.freepascal.org/index.php/topic,67337.msg519191.html#msg519191 which I've just "knocked together" to use a TRichMemo to display coloured output in a logging (i.e. non-interactive) context.

Note the format of those escape sequences: <Esc>[ <params> <operator> i.e. postfix like Forth etc. That implies that they are ANSI AKA (historically) VT100, but are distinct from the older e.g. VT52. In practice on Linux they're likely to be determined by the content of the TERM shell/environment variable, via termcap/terminfo etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Michael Collier

  • Sr. Member
  • ****
  • Posts: 318
Re: strip color from TProcess output
« Reply #8 on: July 08, 2024, 06:04:20 pm »
You can use https://github.com/t-edson/UnTerminal. It's a wrapper for TProcess and recognizes most of the VT100 sequences screen controls, ignoring color sequences so you can show it in a simple Textbox.

This solved the problem for me, thanks :)

 

TinyPortal © 2005-2018