8

How can the amount of available lines in a terminal be found?

Preferably in a cross-platform manner but any suggestions (even OS-specific) are welcome.

The height and length of a terminal can be found using the os module however this does not take into account the amount of lines that may already have been used.

To clarify things here is an example:

In this example the height of the terminal here is 33 however since 3 lines have been used, only 30 lines are available.

Xantium
  • 11,201
  • 10
  • 62
  • 89

2 Answers2

8

Determining by that screen shot, you are on Windows

This is from http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/

from ctypes import windll, create_string_buffer

# stdin handle is -10
# stdout handle is -11
# stderr handle is -12

h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)

if res:
    import struct
    (bufx, bufy, curx, cury, wattr,
     left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
    sizex = right - left + 1
    sizey = bottom - top + 1
else:
    sizex, sizey = 80, 25 # can't determine actual size - return default values

print sizex, sizey, curx, cury

That will give you screen size, and the cursor position.

cury is the line, so you can calculate the number of lines left.

However, you may want to re-check the console window size as you progress, as the user may resize the window at any time.

Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
  • 1
    doesn't curx and cury allow right away for the calculation OP wants? – progmatico Jan 31 '18 at 19:47
  • @progmatico ... oh that is right... well spotted. cury will give you the line. Answer updated. Thanks !! – Edwin van Mierlo Jan 31 '18 at 19:52
  • Thank you for your answer. Just one problem. I am running Python 3 (and I don't know Python 2). If I add the `()` to print statement I get the error:`NameError: name 'curx' is not defined`. – Xantium Jan 31 '18 at 23:57
  • The only thing to change is ( ) for the print statement on the last line. The problem error you are getting is because you are running it on an IDE and not an actual console... try running in an actual console. – Edwin van Mierlo Jan 31 '18 at 23:59
  • **Just what I was after thank you**. However just a note. It counts text and not new lines, so if I used 30 new lines it would still say 33 are available, for this reason I will wait a day or so until I accept this answer. In any case this is worthy of an up-vote. Thank you once again for the quick reply. – Xantium Feb 01 '18 at 00:56
  • @Simon `sizey - cury` is giving me a consistent "lines left" up to the point you have used more lines than available; which means `cury > sizey`, you are using the console buffer and the console window starts scrolling. Using Python3 on Win10, tested on 3 machines consistently.... not sure why you get different results. – Edwin van Mierlo Feb 01 '18 at 08:58
4

To determine the terminal size (for any OS platform) -

import os
size = os.get_terminal_size() 
print(size)

The output generated will be : os.terminal_size(columns=80, lines=24) Here columns is the width of the terminal window and lines is the height (80 and 24 is just the reference value). You can use these as print("-"*size.columns) or anything else. I hope this helps!

  • This way you can get the terminal size but not the available lines in a terminal even if we have the number of total lines in a terminal we still doesn't have the record of lines used in a terminal. Your approach might help if someone decides to keep track of the number of lines printed already on the terminal by increasing a counter every time someone prints a `'\n'`. – Ayush Jul 15 '21 at 11:19