6

I am writing a Python script to print out displayable user interface. The problem is every Linux user would have their own unique terminal size. This will cause the hard-coded user interface to go out of format.

(If there is a lot of example below, the terminal looks Crazy!!!).

Example, in the script. I have print out:

print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

Format should goes well in my terminal: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

When the terminal is smaller, the print out format will run out. Format become: ++++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++

So I am thinking:

  1. When the user run the script, can I auto change the Linux terminal size to my declare size
  2. Can I get the Width and Length of the user terminal size using Python, so the terminal display can be flexible
  3. I would like to hear any better solution around the world to solve the terminal display problem!

I would strongly prefer recommendation in Python

4
  • You might want to have a look at this question. Commented Oct 3, 2011 at 15:26
  • That question, get the terminal size. What would be most recommended? (Most probably, for future programming purposes) Get the terminal size or resize it? Commented Oct 3, 2011 at 15:28
  • Get the size. You can't resize terminals. (well, maybe some types, but even then users wouldn't like it) Commented Oct 3, 2011 at 15:37
  • *Like* @Petr Viktorin Comment Commented Oct 3, 2011 at 15:39

3 Answers 3

8

I'd highly suggest using something like the Python Standard Library's curses module to do this.

Don't reinvent the wheel - using an existing library will both help you avoid corner cases and also save you time. Plus, the curses interface is a familiar one to *nix users, which will make them like you more.

Sign up to request clarification or add additional context in comments.

6 Comments

Hm.. Interesting! So does the API auto change the terminal size or does it get the terminal size?
curses is designed to work with whatever the user's existing terminal size is. (Most people do not like you forcing their terminals to resize, and some terminals may not even allow you to resize them.)
True, true! Some terminal can't be resize!
Sadly, no (at least by default). As stated at the top of the manual page, it's "Platforms: Unix".
Tsk! Thought I could kill two bird with a stone! =P
|
1

As Amber suggested, you should use a library like curses.

Still, you could get the width of the terminal using something like this:

import subprocess
int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).stdout.read())

Comments

0

Based on the comments in Amber's solution, there is some desire to see a solution that works on Windows too. A simple cross platform solution is to use asciimatics. For example:

from asciimatics.screen import Screen

def demo(screen):
    screen.print_at('+' * screen.width, 0, 0)
    screen.refresh()
    sleep(10)

Screen.wrapper(demo)

This package also provides a whole load of higher level widgets to make full screen text UIs easier. See the contact list demo for an example.

Full disclosure: yes - I am the author of that package and so might be a little biased. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.