예제 #1
0
    def testGetRawKeyFunctionPosix(self):
        expected = [
            'A',
            '<UP-ARROW>',
            '<DOWN-ARROW>',
            '<LEFT-ARROW>',
            '<RIGHT-ARROW>',
            '<PAGE-UP>',
            '<PAGE-DOWN>',
            '<HOME>',
            '<END>',
            '<DOWN-ARROW>',
            '<PAGE-UP>',
            '<PAGE-DOWN>',
            '\x1b',
            None,
            None,
            '/',
        ]

        self.imports.SetImport('tty', self.MockTtyModule)
        getrawkey = console_attr_os.GetRawKeyFunction()
        actual = []
        for _ in range(len(expected)):
            actual.append(getrawkey())
        self.assertEqual(expected, actual)
    def __init__(self, encoding=None, out=None):
        """Constructor.

    Args:
      encoding: Encoding override.
        ascii -- ASCII art. This is the default.
        utf8 -- UTF-8 unicode.
        win -- Windows code page 437.
      out: The console output file stream, log.out if None.
    """
        if not out:
            try:
                from googlecloudsdk.core import log  # pylint: disable=g-import-not-at-top, avoid config import loop
                out = log.out
            except ImportError:
                out = sys.stdout
        self._out = out
        # Normalize the encoding name.
        console_encoding = None
        if not encoding:
            if hasattr(self._out, 'encoding') and self._out.encoding:
                console_encoding = self._out.encoding.lower()
                if 'utf-8' in console_encoding:
                    encoding = 'utf8'
                elif 'cp437' in console_encoding:
                    encoding = 'cp437'
        elif encoding == 'win':
            encoding = 'cp437'
        self._encoding = encoding or 'ascii'
        self._term = os.getenv('TERM', '').lower()

        # ANSI "standard" attributes.
        if self._encoding != 'ascii' and ('screen' in self._term
                                          or 'xterm' in self._term):
            # Select Graphic Rendition paramaters from
            # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
            # Italic '3' would be nice here but its not widely supported.
            self._csi = '\x1b['
            self._font_bold = '1'
            self._font_italic = '4'
        else:
            self._csi = None
            self._font_bold = ''
            self._font_italic = ''

        # Encoded character attributes.
        if self._encoding == 'utf8':
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_UNICODE
        elif self._encoding == 'cp437':
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_WINDOWS
        else:
            self._box_line_characters = BoxLineCharactersAscii()
            self._bullets = self._BULLETS_ASCII

        # OS specific attributes.
        self._get_raw_key = [console_attr_os.GetRawKeyFunction()]
        self._term_size = console_attr_os.GetTermSize()
예제 #3
0
    def __init__(self, encoding=None, suppress_output=False):
        """Constructor.

    Args:
      encoding: Encoding override.
        ascii -- ASCII art. This is the default.
        utf8 -- UTF-8 unicode.
        win -- Windows code page 437.
      suppress_output: True to create a ConsoleAttr that doesn't want to output
        anything.
    """
        # Normalize the encoding name.
        if not encoding:
            encoding = self._GetConsoleEncoding()
        elif encoding == 'win':
            encoding = 'cp437'
        self._encoding = encoding or 'ascii'
        self._term = '' if suppress_output else encoding_util.GetEncodedValue(
            os.environ, 'TERM', '').lower()

        # ANSI "standard" attributes.
        if self.SupportsAnsi():
            # Select Graphic Rendition paramaters from
            # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
            # Italic '3' would be nice here but its not widely supported.
            self._csi = '\x1b['
            self._font_bold = '1'
            self._font_italic = '4'
        else:
            self._csi = None
            self._font_bold = ''
            self._font_italic = ''

        # Encoded character attributes.
        is_screen_reader = properties.VALUES.accessibility.screen_reader.GetBool(
        )
        if self._encoding == 'utf-8' and not is_screen_reader:
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_UNICODE
            self._progress_tracker_symbols = ProgressTrackerSymbolsUnicode()
        elif self._encoding == 'cp437' and not is_screen_reader:
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_WINDOWS
            # Windows does not suport the unicode characters used for the spinner.
            self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()
        else:
            self._box_line_characters = BoxLineCharactersAscii()
            if is_screen_reader:
                self._box_line_characters = BoxLineCharactersScreenReader()
            self._bullets = self._BULLETS_ASCII
            self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()

        # OS specific attributes.
        self._get_raw_key = [console_attr_os.GetRawKeyFunction()]
        self._term_size = ((0, 0) if suppress_output else
                           console_attr_os.GetTermSize())

        self._display_width_cache = {}
예제 #4
0
    def __init__(self, encoding=None):
        """Constructor.

    Args:
      encoding: Encoding override.
        ascii -- ASCII art. This is the default.
        utf8 -- UTF-8 unicode.
        win -- Windows code page 437.
    """
        # Normalize the encoding name.
        if not encoding:
            encoding = self._GetConsoleEncoding()
        elif encoding == 'win':
            encoding = 'cp437'
        self._encoding = encoding or 'ascii'
        self._term = os.getenv('TERM', '').lower()

        # ANSI "standard" attributes.
        if self._encoding != 'ascii' and ('screen' in self._term
                                          or 'xterm' in self._term):
            # Select Graphic Rendition paramaters from
            # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
            # Italic '3' would be nice here but its not widely supported.
            self._csi = '\x1b['
            self._font_bold = '1'
            self._font_italic = '4'
        else:
            self._csi = None
            self._font_bold = ''
            self._font_italic = ''

        # Encoded character attributes.
        if self._encoding == 'utf8':
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_UNICODE
            self._progress_tracker_symbols = ProgressTrackerSymbolsUnicode()
        elif self._encoding == 'cp437':
            self._box_line_characters = BoxLineCharactersUnicode()
            self._bullets = self._BULLETS_WINDOWS
            # Windows does not suport the unicode characters used for the spinner.
            self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()
        else:
            self._box_line_characters = BoxLineCharactersAscii()
            self._bullets = self._BULLETS_ASCII
            self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()

        # OS specific attributes.
        self._get_raw_key = [console_attr_os.GetRawKeyFunction()]
        self._term_size = console_attr_os.GetTermSize()

        self._display_width_cache = {}
예제 #5
0
    def testGetRawKeyFunctionWindows(self):
        expected = [
            'A',
            '<UP-ARROW>',
            '<DOWN-ARROW>',
            '<LEFT-ARROW>',
            '<RIGHT-ARROW>',
            '<PAGE-UP>',
            '<PAGE-DOWN>',
            '<HOME>',
            '<END>',
            '\x1b',
            None,
            None,
            '/',
        ]

        getrawkey = console_attr_os.GetRawKeyFunction()
        actual = []
        for _ in range(len(expected)):
            actual.append(getrawkey())
        self.assertEqual(expected, actual)
예제 #6
0
 def testGetRawKeyFunctionFallBack(self):
     getrawkey = console_attr_os.GetRawKeyFunction()
     c = getrawkey()
     self.assertEqual(None, c)
예제 #7
0
 def testGetRawKeyFunctionPosixIOError(self):
     self.imports.SetImport('tty', self.MockTtyModuleIOError)
     getrawkey = console_attr_os.GetRawKeyFunction()
     c = getrawkey()
     self.assertEqual(None, c)