Search code examples
pythonpython-3.xcmdcursespython-curses

Center input in python cmd?


I'm working on a classic text-adventure port in python.

I'm wondering is it possible to center a users input? I found out that using str.center() method, I can center the printed text, however, I can't center the python input() function. Using str.center() just gives me this: As you can see, the input starts in the middle but strafts rigth

As you can see, the input starts in the middle but then just continues to the right

What I'd like to do is that whatever the user inputs, it goes in the middle like this: Mock (also if you're wondering, I didn't solve my own problem, this is just a mock using a print instead of an input)

So I'm wondering how could I do that? Using the str.center() or Curses or something else altogether?


Solution

  • You could do this with curses. The input() and str.center() will not be that helpful since they do not know in advance how wide the screen is (and how to center the input string as you enter it).

    With curses, you could use the filter function to set aside the current line of the screen (rather than erasing the whole screen...), create a window in that line using newwin and use mvwin to adjust the window's position as entered (or alternatively just move within the original stdscr). There are several ways to accomplish this with curses—but more complicated than input().