Coverage for /media/ldata/code/tendril/tendril/utils/terminal.py : 65%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # encoding: utf-8
# Copyright (C) 2015 Chintalagiri Shashank # # This file is part of tendril. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
The Terminal Utils Module (:mod:`tendril.utils.terminal`) =========================================================
This module provides utils for rendering basic UI elements on the terminal.
:class:`TendrilProgressBar can be used to produce animated progress bars on the terminal. This class (and the code related) to it is essentially a copy of pip`s progressbar implementation in pip.utils.ui.
"""
# Lots of different errors can come from this, including SystemError and # ImportError. except Exception: colorama = None
(sys.platform == 'cli' and os.name == 'nt'))
""" getTerminalSize()
- get width and height of console - works on linux,os x,windows,cygwin(windows)
Taken from https://gist.github.com/jtriley/1108174 """ tuple_xy = _get_terminal_size_windows() if tuple_xy is None: tuple_xy = _get_terminal_size_tput() # needed for window's python in cygwin's xterm! tuple_xy = (80, 25) # default value
try: 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: (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw) sizex = right - left + 1 sizey = bottom - top + 1 return sizex, sizey except: pass
# get terminal width try: cols = int(subprocess.check_call(shlex.split('tput cols'))) rows = int(subprocess.check_call(shlex.split('tput lines'))) return cols, rows except: pass
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) except: pass try: cr = (os.environ['LINES'], os.environ['COLUMNS']) except: return None
# If we don't know what encoding this file is in, then we'll just assume # that it doesn't support unicode and use the ASCII bar. return fallback
# Collect all of the possible characters we want to use with the preferred # bar. getattr(preferred, "empty_fill", six.text_type()), getattr(preferred, "fill", six.text_type()), ]
# Try to decode the characters we're using for the bar using the encoding # of the given file, if this works then we'll assume that we can use the # fancier bar and if not we'll fall back to the plaintext bar. except UnicodeEncodeError: return fallback else:
# The Windows terminal does not support the hide/show cursor ANSI # codes even with colorama. So we'll ensure that hide_cursor is False # on Windows. # This call neds to go before the super() call, so that hide_cursor # is set in time. The base progress bar class writes the "hide cursor" # code to the terminal in its init, so if we don't set this soon # enough, we get a "hide" with no corresponding "show"... self.hide_cursor = False
# Check if we are running on Windows and we have the colorama module, # if we do then wrap our file with it. self.file = colorama.AnsiToWin32(self.file) # The progress code expects to be able to call self.file.isatty() # but the colorama.AnsiToWin32() object doesn't have that, so # we'll add it. self.file.isatty = lambda: self.file.wrapped.isatty() # The progress code expects to be able to call self.file.flush() # but the colorama.AnsiToWin32() object doesn't have that, so # we'll add it. self.file.flush = lambda: self.file.wrapped.flush()
""" This class can be used from other modules to provide a consistent feel to progress bars across tendril. It adds a ``note`` keyword argument to the ``next()`` function, and renders the note after the suffixe of the progress bar.
.. rubric :: Usage
>>> from tendril.utils.terminal import TendrilProgressBar >>> pb = TendrilProgressBar(max=100) >>> for i in range(100): ... pb.next(note=i)
"""
def term_width(self):
self.clearln() if self._note is not None: line = ' '.join([line, self._note]) if len(line) > self.term_width: line = line[:self.term_width] print(line, end='', file=self.file) self.file.flush() |