I use QT to call external programs under Ubuntu and put the output on textbrowser. However, because the external programs I want to call have colors in the shell output, the output of textbrowser is mixed with color codes. How can I remove them?
enter image description here
enter image description here
Normally programs perform check if output is tty (terminal) and only output text with colors if it is. Do you have escape codes when you redirect output to file?
To strip color codes from terminal you can use regular expression. Check answers to this question: Removing colors from output
In SWI-Prolog Qt console I wrote this class for matching ANSI coloring sequences that must be cleaned. I replace them with the appropriate QTextCharFormat commands, you can work out something similar, replacing instead with empty strings or the CSS styling as required by your application.
Inside the source (both ansi_esc_seq.h and ansi_esc_seq.cpp) you also find a bit of documentation about the ANSI escape sequences behaviour.
Related
I`ve codet a simple cli app. This app uses ansi codes like \x1B[48;2;190;190;0m for color. In addition it uses ascii full block (█) to display qr-codes.
When I run my application in the normal linux bash it all works just fine, but if I run it into a gnu screen no colors are showing up and instead of an qr-code I only see many blank lines.
What am I missing ?
Python 3.7, on Windows print does not work as expected for ANSI color codes until shell=True once in subprocess.call().
In the below links it appears to imply that the ANSI color codes should work using the "print" command out of the box.
How to print colour/color in python?
Print in terminal with colors using Python?
the second one mentions VT100 emulation... not sure what exactly that means. I am able to write a batch file that outputs the color fine so I would think (naively) that it should work the same way in Python.
However I am not able to use the ANSI color codes as it seems that the ESC character is being "commented out"(?) because for instance when I
print(u"\u001b[31mHelloWorld")
I am not able to see the colored output, as the ESC character seems to be necessary in Windows and prints in the python shell as "[?]" (a box with a question mark)
Is there something I am missing here?
I found myself an answer. As often happens I just did not look far enough.
the Colorama module can be installed with
py -m pip install colorama
and comes with a method definition at the root of the module called init
colorama.init()
This is a cross platform function in that it is only useful on windows (it saves the active Terminal state for reversal and writes the Terminal to preprocess ANSI codes), it does nothing for other operating systems.
I am thinking about implementing an even more lightweight solution using ctypes and setting the Interpret flags on the active terminal myself.
If you are interested in more information, see here:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
Output Sequences
The following terminal sequences are intercepted by the console host when written into the output stream, if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. Note that the DISABLE_NEWLINE_AUTO_RETURN flag may also be useful in emulating the cursor positioning and scrolling behavior of other terminal emulators in relation to characters written to the final column in any row.
Emphasis mine.
Do you think there is a way of changing a specific word's color on the Linux terminal or on Putty?
I'm using the "make" command though terminal (and putty sometimes), and I just thought of how nice it would be if among all the warnings, the printed line containing the "error:" would be in red/marked somehow.
It doesn't really matter, because I guess the solution would be somehow terminal recognizing a specific word related, but just for clarafication - I'm compiling a makefile of C/Assembly code.
There is off course the solution of not printing the warnings at all, but I want to see the warnings.
Thanks,
Barak.
See the ANSI terminal escape sequences. Most terminals implement them.
For example, to display a word in red:
The word at the end is in <Esc>[31m red <Esc>[30m .
I have inserted some spaces for clarity, which should be removed.
Unfortunately, this assumes that the foreground color is initially black. If the text is originally white on a black background, this will make subsequent text seem to disappear. (I say seem because it is there and can be viewed by selecting the text.)
The make files generated by CMake have a colorful output for improved readability.
Is it possible to change the color? I have a green terminal font so I kind of loose the color for the messages indicating that objects are built.
I assume you are talking about the cmake added messages from the make output (and not the normal make output itself) in which case it looks like those lines are hard-coded into the generated makefiles with arguments to the cmake cmake_echo_color command that include literal color names as arguments. So there don't appear to be any variables to override or anything of the sort that would let you change that.
So, no, it looks like you cannot control the colors that cmake uses for its various messages. At all.
It doesn't even get the values it uses to control the colors from your terminal's termcap/terminfo it has hard-coded escape sequences in the source code from what I can tell.
The only think you can do is change your terminal's color definitions.
Edit:
While discussing this with a colleague we came up with a fairly obvious and unsophisticated workaround for this. Create a cmake wrapper script, assign the path to it to the $(CMAKE) variable (the only variable in use on that generated make line) and then your wrapper needs to scan for the -E cmake_echo_color command sequence and replace whatever color the command was going to be using with the appropriate argument for the color you want to use.
The only problem with this approach is that there is nothing in the command line that indicates any category or any of the sort other than the message itself so to do this in anything resembling a coherent fashion is going to require a filtering on (potentially random) message texts.
As of 2022, it still isn't possible to change the colors.
I've filed two bugs about this with Kitware:
https://gitlab.kitware.com/cmake/cmake/-/issues/23277
https://gitlab.kitware.com/cmake/cmake/-/issues/23279
So maybe things will improve like they have for ccmake.
I would like to print a file containing text with ANSI escapes.
Here is file content (generated with bash script):
\033[1m bold message example \033[0m
normal font message
When printing file to screen in terminal, it works nice:
cat example.txt
shows:
bold message example
normal font message
But my problem when I try to send it to a printer:
lp example.txt
prints:
1mbold message example2m
normal font message
Is there a way to print this file correctly ? Maybe with groff (can be used to print a styled man page), but I did not manage to get anything efficient with it...
Maybe a2ps might be able to handle that (but I am not sure, you should try).
And I would rather suggest changing the way you get such a file with ANSI escapes (that is, also provide some alternative output format).
I mean that the program producing such a file (or such an output) could instead produce a more printable output (perhaps by generating some intermediate form, e.g. LaTeX, or Lout, or groff or HTML format, then forking the appropriate command to print it. That program could also generate directly PDF thru libharu or poppler, etc....)
Also, it might depend upon your printer, and the driver.