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.
Related
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.
I am looking to implement a remote client in golang which connects to Linux through nc and starts bash. So I need to tell bash what features I can parse from the stdout that it sends to me, and how I am going to send keycodes and other stuff to its stdin, so that it could parse them too.
This is done with TERM=something environment variable, which I need to set to some value. If I don't set it, then various programs start to complain:
$ mc
The TERM environment variable is unset!
I found that I can set TERM to dumb to say that my client is really limited. And still it seems that I am missing something.
$ export TERM=dumb
$ mc
Your terminal lacks the ability to clear the screen or position the cursor.
From here it looks like dumb terminal don't have these two abilities, but what abilities it is still expected to have? Is there a specification or some de-facto standard about it?
Going to the source can help. The terminal database has comments. Here is a slice from that:
#### Specials
#
# Special "terminals". These are used to label tty lines when you don't
# know what kind of terminal is on it. The characteristics of an unknown
# terminal are the lowest common denominator - they look about like a ti 700.
#
dumb|80-column dumb tty,
am,
cols#80,
bel=^G, cr=^M, cud1=^J, ind=^J,
unknown|unknown terminal type,
gn, use=dumb,
The "dumb" and "unknown" terminal types are assumed, but rarely used:
"dumb" has automargins (text "wraps" at the right margin), is assumed to have 80 columns, and an ASCII BEL and carriage return. For lack of something better, cud1 (cursor down) is an ASCII line-feed. The ind (index) value is the same, implying that text scrolls up when you reach the bottom of the screen.
There is no cursor-addressing (cup) nor alternates (such as moving along a row or column arbitrarily).
"unknown" adds the "generic" flag, which marks it as unsuitable for use by curses applications. Think of it as a printer.
As for minimum requirements, that actually depends upon the individual application. ncurses can manage to move around the screen without actually having cup. It works with a half-dozen strategies. If you read the source for mvcur, you can get an idea of what it needs.
However, applications such as mc do not simply rely upon ncurses to decide if it works, since (in this case) it may link with slang (which doesn't check that closely). So mc does its own checks, which may add restrictions.
In practice, unless you choose a limited terminal description such as "dumb", most of the terminals you are likely to encounter will work.
Further reading:
terminfo - terminal capability data base
curses interfaces to terminfo database (including mvcur)
ncurses/tty/lib_mvcur.c
Your best source of information will be the terminfo entry, easily viewed with the infocmp tool:
infocmp dumb
# Reconstructed via infocmp from file: /lib/terminfo/d/dumb
dumb|80-column dumb tty,
am,
cols#80,
bel=^G, cr=^M, cud1=^J, ind=^J,
which makes it pretty clear that the dumb terminal is quite limited ...
I am programming in Linux using ncurses. When I use attron() to switch on attributes, it works for all attributes except A_DIM (which is meant to yield half bright characters). A_BOLD, A_ITALIC, A_BLINK, A_UNDERLINE, A_STANDOUT - all of these have effect, but A_DIM has no any visible effect at all, no matter if I use console or xterm. What might be wrong with that?
It depends on the actual terminal, and on the terminal description used. For the latter, you can see immediately if it is provided by looking at the output of infocmp, e.g.,
# Reconstructed via infocmp from file: /usr/local/ncurses/share/terminfo/x/xterm-new
xterm-new|modern xterm terminal emulator,
am, bce, km, mc5i, mir, msgr, npc, xenl,
colors#8, cols#80, it#8, lines#24, pairs#64,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=^M,
csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
cvvis=\E[?12;25h, dch=\E[%p1%dP, dch1=\E[P, dim=\E[2m,
XTerm provides all of the video attributes (though "dim" and "italics" are relatively recent):
Linux console provides some attributes (dim for example), but not underline or italics (it uses color). The usual Linux console terminal description does not include "dim", since that has some caveats:
These screenshots use the main ncurses test-program, named ncurses (see webpage and source).
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.)
Is there a way to use ANSI escape codes inside mvwprintw?
mvwprintw(window, 0, 0,"%c[%dmCOLORED_TEXT!\n", 0x1B, 32);//doesn't work
even though:
printf("%c[%dmCOLORED_TEXT\n", 0x1B, 32); //works
This would be for cases where using wattron/wattroff is not convenient; for example, when redirecting output from stdout of a process that outputs such escape codes.
No. The only way to make that work would be to parse the string yourself, turning escape codes back into the appropriate curses commands, to issue along with your output.
One thing you should realize is that those codes, although widely implemented, are not universal. One of the major purposes of curses is to translate its standard commands into series of terminal-specific control codes. So, passing through codes that may or may not correspond to the current terminal type doesn't really fit the curses model. Even more fundamentally, the codes would change the terminal state in a way that curses wouldn't be able to keep track of, so that the contents of its window structures no longer matched what was on screen.