Пример #1
0
def _GetTermSizeTput():
    """Returns the terminal x and y dimemsions from tput(1)."""
    import subprocess  # pylint: disable=g-import-not-at-top
    output = encoding.Decode(
        subprocess.check_output(['tput', 'cols'], stderr=subprocess.STDOUT))
    cols = int(output)
    output = encoding.Decode(
        subprocess.check_output(['tput', 'lines'], stderr=subprocess.STDOUT))
    rows = int(output)
    return (cols, rows)
Пример #2
0
def Decode(data, encoding=None):
    """Converts the given string, bytes, or object to a text string.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.
    encoding: A suggesting encoding used to decode. If this encoding doesn't
      work, other defaults are tried. Defaults to
      GetConsoleAttr().GetEncoding().

  Returns:
    A text string representation of the data.
  """
    encoding = encoding or GetConsoleAttr().GetEncoding()
    return encoding_util.Decode(data, encoding=encoding)
Пример #3
0
def SafeText(data, encoding=None, escape=True):
    br"""Converts the data to a text string compatible with the given encoding.

  This works the same way as Decode() below except it guarantees that any
  characters in the resulting text string can be re-encoded using the given
  encoding (or GetConsoleAttr().GetEncoding() if None is given). This means
  that the string will be safe to print to sys.stdout (for example) without
  getting codec exceptions if the user's terminal doesn't support the encoding
  used by the source of the text.

  Args:
    data: Any bytes, string, or object that has str() or unicode() methods.
    encoding: The encoding name to ensure compatibility with. Defaults to
      GetConsoleAttr().GetEncoding().
    escape: Replace unencodable characters with a \uXXXX or \xXX equivalent if
      True. Otherwise replace unencodable characters with an appropriate unknown
      character, '?' for ASCII, and the unicode unknown replacement character
      \uFFFE for unicode.

  Returns:
    A text string representation of the data, but modified to remove any
    characters that would result in an encoding exception with the target
    encoding. In the worst case, with escape=False, it will contain only ?
    characters.
  """
    if data is None:
        return 'None'
    encoding = encoding or GetConsoleAttr().GetEncoding()
    string = encoding_util.Decode(data, encoding=encoding)

    try:
        # No change needed if the string encodes to the output encoding.
        string.encode(encoding)
        return string
    except UnicodeError:
        # The string does not encode to the output encoding. Encode it with error
        # handling then convert it back into a text string (which will be
        # guaranteed to only contain characters that can be encoded later.
        return (string.encode(
            encoding,
            'backslashreplace' if escape else 'replace').decode(encoding))
Пример #4
0
 def _GetKeyChar():
     return encoding.Decode(msvcrt.getch())
Пример #5
0
 def _GetKeyChar():
     return encoding.Decode(os.read(fd, 1))