summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-02-08 22:28:36 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-02-08 22:28:36 (GMT)
commitbcf2b59fb5f18c09a26da3e9b60a37367f2a28ba (patch)
tree47232d9eb97758190b44700163d2706665224d7c /Doc/library
parent4195b5caea0fe1446160e78d69420732ead7e78b (diff)
downloadcpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.zip
cpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.tar.gz
cpython-bcf2b59fb5f18c09a26da3e9b60a37367f2a28ba.tar.bz2
Issue #13609: Add two functions to query the terminal size:
os.get_terminal_size (low level) and shutil.get_terminal_size (high level). Patch by Zbigniew Jędrzejewski-Szmek.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/os.rst37
-rw-r--r--Doc/library/shutil.rst33
2 files changed, 70 insertions, 0 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 06f1452..c3dfb3d 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -1411,6 +1411,43 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
.. versionadded:: 3.3
+.. _terminal-size:
+
+Querying the size of a terminal
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. versionadded:: 3.3
+
+.. function:: get_terminal_size(fd=STDOUT_FILENO)
+
+ Return the size of the terminal window as ``(columns, lines)``,
+ tuple of type :class:`terminal_size`.
+
+ The optional argument ``fd`` (default ``STDOUT_FILENO``, or standard
+ output) specifies which file descriptor should be queried.
+
+ If the file descriptor is not connected to a terminal, an :exc:`OSError`
+ is thrown.
+
+ :func:`shutil.get_terminal_size` is the high-level function which
+ should normally be used, ``os.get_terminal_size`` is the low-level
+ implementation.
+
+ Availability: Unix, Windows.
+
+.. class:: terminal_size(tuple)
+
+ A tuple of ``(columns, lines)`` for holding terminal window size.
+
+ .. attribute:: columns
+
+ Width of the terminal window in characters.
+
+ .. attribute:: lines
+
+ Height of the terminal window in characters.
+
+
.. _os-file-dir:
Files and Directories
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index 77ce6b0..bae7db8 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -459,3 +459,36 @@ The resulting archive contains::
-rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
-rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
-rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
+
+
+Querying the size of the output terminal
+----------------------------------------
+
+.. versionadded:: 3.3
+
+.. function:: get_terminal_size(fallback=(columns, lines))
+
+ Get the size of the terminal window.
+
+ For each of the two dimensions, the environment variable, ``COLUMNS``
+ and ``LINES`` respectively, is checked. If the variable is defined and
+ the value is a positive integer, it is used.
+
+ When ``COLUMNS`` or ``LINES`` is not defined, which is the common case,
+ the terminal connected to :data:`sys.__stdout__` is queried
+ by invoking :func:`os.get_terminal_size`.
+
+ If the terminal size cannot be successfully queried, either because
+ the system doesn't support querying, or because we are not
+ connected to a terminal, the value given in ``fallback`` parameter
+ is used. ``fallback`` defaults to ``(80, 24)`` which is the default
+ size used by many terminal emulators.
+
+ The value returned is a named tuple of type :class:`os.terminal_size`.
+
+ See also: The Single UNIX Specification, Version 2,
+ `Other Environment Variables`_.
+
+.. _`Other Environment Variables`:
+ http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003
+