"Fossies" - the Fresh Open Source Software Archive 
Member "polysh-polysh-0.13/polysh/terminal_size.py" (11 May 2020, 2592 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "terminal_size.py" see the
Fossies "Dox" file reference documentation.
1 """Polysh - Buffered Dispatcher Class
2
3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
4 Copyright (c) 2018 InnoGames GmbH
5 """
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # The code is taken from http://pdos.csail.mit.edu/~cblake/cls/cls.py
20 # license in http://pdos.csail.mit.edu/~cblake/cls/cls_py_LICENSE reproduced
21 # thereafter:
22 # I, Charles Blake, hereby relinquish all rights to the functions
23 # terminal_size() and ioctl_GWINSZ() in file cls.py, located in this
24 # same code directory to the maximum extent applicable by this notice.
25 #
26 # These functions are provided "as is" and without any expressed or implied
27 # warranties, including, without limitation, the implied warranties of
28 # merchantibility and fitness for a particular purpose.
29 #
30 # It would be nice (but not necessary) to give me an artistic license credit
31 # somewhere in the licensing materials of any derivative product.
32
33
34 import os
35 from typing import Tuple, Optional
36
37
38 def _ioctl_GWINSZ(fd: int) -> Optional[Tuple[int, int]]:
39 try: # Discover terminal width
40 import fcntl
41 import termios
42 import struct
43 cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, b'1234'))
44 except BaseException:
45 return None
46 return int(cr[0]), int(cr[1])
47
48
49 def terminal_size() -> Tuple[int, int]: # decide on *some* terminal size
50 """Return (lines, columns)."""
51 cr = _ioctl_GWINSZ(0) or _ioctl_GWINSZ(
52 1) or _ioctl_GWINSZ(2) # try open fds
53 if not cr: # ...then ctty
54 try:
55 fd = os.open(os.ctermid(), os.O_RDONLY)
56 cr = _ioctl_GWINSZ(fd)
57 os.close(fd)
58 except BaseException:
59 pass
60 if not cr: # env vars or finally defaults
61 try:
62 cr = int(os.environ['LINES']), int(os.environ['COLUMNS'])
63 except BaseException:
64 cr = 25, 80
65 return cr[1], cr[0] # reverse rows, cols