I know I can use either print \b to replace a single character or sys.stdout.write(\rSomething) to replace an entire line of text (like in this example), but is there a way replace the top line in the console window using Python while letting text continue to print below?
For example:
[---- ] <-- a progress bar that updates at the top...
Ongoing info <-- while other stuff prints down below
foo
More ongoing info
It's more complex that just using characters, as you need more powerful control over terminal.
This may sources for help:
Curses Programming with Python
curses — Terminal handling for character-cell displays
If you're really want to go hard way and do it using RAW control, you have to learn about ANSI control code (well known as Escape sequences). However - you have been warned - that way is not portable as different terminal types use in fact slightly different sequences to control what and how is displayed in terminal and some terminal doesn't support some codes and then certain operations are handled different way. Using curses solve these problems for you.
Related
I am creating CLI app for Unix terminal using click module. So I see two ways how I can display data:
print(data) and click.echo(data)
What is difference between them and what should I use?
Please, read at least quickstart of library before using it. The answer is in the third part of quickstart.
If you use click click.echo() is preferred because:
Click attempts to support both Python 2 and Python 3 the same way and to be very robust even when the environment is misconfigured. Click wants to be functional at least on a basic level even if everything is completely broken.
What this means is that the echo() function applies some error correction in case the terminal is misconfigured instead of dying with an UnicodeError.
As an added benefit, starting with Click 2.0, the echo function also has good support for ANSI colors. It will automatically strip ANSI codes if the output stream is a file and if colorama is supported, ANSI colors will also work on Windows. See ANSI Colors for more information.
If you don’t need this, you can also use the print() construct / function.
I'm writing a curses program in Python. I'm a beginner of curses but I've used terminal control sequences for colored output.
Now there's some code snippets to print inside the window, I'd like them be syntax highlighted, and it's better done with libraries like pygments, which outputs highlighted code with control sequences.
Initially I feed pygments output directly to window.addstr(), but it is turned out that the control sequences is escaped and the whole highlighted string is printed on the screen (just like this: https://too-young.me/web/repos/curses-highlight.png). How can I display it directly with curses, just like cat?
There's the "culour" python module which does exactly that.
Install it using pip install culour, and then you can use it to print pre-colored strings:
import culour
culour.addstr(window, colored_string)
This will print the string colored in your window.
This has been asked several times, with the same answer: you could write a parser to do this. For related discussion:
How to use ANSI escape codes inside mvwprintw in ncurses?
Comment on Parsing ANSI color escape sequences
Handle escape sequences with ncurses? Does printf handle escape sequences?
It is not suitable as an extension to ncurses for example because:
curses produces escape sequences, but for a wide variety of devices (which may not be "ANSI color escapes").
ncurses (see the FAQ Why aren't my bugs being fixed?) does not provide it as an extension because a parser of this type would not rely upon any of ncurses' internals.
On GitHub there is a free to use, study, modify and re-distribute High Level GUI library, at "https://github.com/rigordo959/tsWxGTUI_PyVx_Repository".
It is implemented in Python 2x & 3x using the "curses" Low Level GUI package. The Linux nCurses implementation has typically replaced the original Unix Curses implementation.
Your application programs can be programmed using a character-mode subset of the pixel-mode "wxPython" High Level GUI API. It supports displays with keyboard and mouse input and various terminal emulators including the color xterms (8-color with 64-color pairs and 16-color with 256-color pairs) and non-color vt100/vt220.
Curses enables you to colorize text strings by inserting an attribute (for color, underline, bold, reverse etc.) token before the text and one to restore the previous attribute after the text. For example:
sampleWindow.attron(curses.A_REVERSE |
curses.color_pair(color_pair_number))
sampleWindow.addstr(begin_y + 3,
begin_x + 48,
' ')
sampleWindow.attroff(curses.A_REVERSE |
curses.color_pair(color_pair_number))
I'm using the pipe-pane of tmux to record what I was doing, and I want to parse and take a account of the commands I typed etc. But when I got the xxx.log file, and opened it with vim, I found that there are many special word like ^H,^M,^G etc,see the sample:
administrator#ubuntu:~$ echo "hello,world[K[K[K[K[K[K[K[K[K[K"
h
administrator#ubuntu:~$ ^G^G^G^G
administrator#ubuntu:~$
I know that tmux records everything I typed, but some keys with Ctrl/alt prefix can not be displayed, and I want to ignore them. How can I archive that with perl/python or C, any tips?
In the general case, this is hard, because some programs make extensive use of display control codes to draw dialogs etc. If you just want to drop any individual unprintable characters, that's easy with tr, but if you want to remove display control codes, too, that's significantly more complex, and basically requires knowledge about which terminal emulator you are working with. A plausible target would be xterm but I'm not aware of any off-the-shelf solutions for stripping xterm display codes.
What would be the easiest way to parse some of hg log's output (with some extra options) and present it in a differently sorted way with colour on windows cmd?
I know of graphlog and hgview (which I've never been able to get running on windows) and alike, but they all have their little quirks and in the way, I never get what I want with them.
For example, I want logs to look something like (the upper part). It would be preferable if it could be done with python, since I would like to avoid installing perl as well, but if it can't ... :/
Anyone?
Without any script, you can find a way to use graphlog with colors to achieve a result similar to your illustration:
See Short Graphlog:
It is based on an alias defined in here, however it uses special code for coloring a bash (Windows) shell. So you might need to adapt those special codes with some adapted for a cmd DOS shell, using ANSI escape codes for Windows.
I have a simple Python script that uses ANSI escape sequences to have colored output on the terminal.
And this works great, but when the output is being used elsewhere (for example in VIM) all the ANSI sequences show up and it makes it really unreadable.
For example, to display RED I do something like:
^[[91m Errors:
-------^[[0m
Which is perfectly readable in the terminal.
I could add a flag to my Python command line tool to avoid displaying these characters when I need to deal with output, but I was wondering if there is a way of having colored output without messing up output.
The solution would have to use the Python Stdlib or would have to avoid installing a third party library.
However, I am perfectly OK if the approach doesn't work on Windows :)
You can use os.isatty with the stdout file descriptor, which can be retrieved by calling fileno on sys.stdout.
Edit: It looks like files also have an isatty method; therefore, you can avoid using the os module and just using sys.stdout.isatty().
Look at this answer it covers exactly what you are asking for
How do I detect whether sys.stdout is attached to terminal or not?