r/Python•
CBruce

Speeding up terminal output?

[removed]

Posted Jan 25, 2019, 2:34 PM UTC

27
K900_•

Don't redraw the whole screen every time, figure out what parts changed and then redraw those.

5
rcfox•
2
CBruceOP•

I looked into those early on and of those, only asthmatics is cross-platform. curses and blessing are Linux only I believe..

Part of this for me is learning lower-level operations and not just feeding parameters into a library of screen/widget makers. (I do that all day long with PyQt)

Sure, it's re-inventing the wheel, but that's part of the fun for me.

1
CBruceOP•

I may have found a solution:

https://stackoverflow.com/questions/7392779/is-it-possible-to-print-a-string-at-a-certain-screen-position-inside-idle

​

Instead of clearing the screen and drawing it again (which is what's causing the visible flickering, I can use escape characters to position the cursor at the top-left of the window and then draw over the existing screen.

Implemented in python 3.7, it looks like this:

def draw(self, clear_first=False):
    """ Prints current screen to the output window """
    if clear_first:
        self.clear()

    stdout.write(f"\x1b7\x1b[0;0f{self.array_to_string(self.screen)}\x1b8")
    stdout.flush()

It's already quite a bit better, and could probably be improved even more by only drawing over the elements that need to be updated,

1
wikwikwik•

Wow - this what happens ehen you ask an applications programmer instead of a game developer. This type of question works better on the gamedev stack exchange.

Use libtcod.

http://roguecentral.org/doryen/data/libtcod/doc/1.5.1/index2.html

https://github.com/libtcod/python-tcod

2
LightShadow3.8-dev in Prod•

You could use a faster terminal.

alacritty is a GPU accelerated terminal, https://github.com/jwilm/alacritty

1
CBruceOP•

Thanks. I'll look into this in the future.

2