def Display(lines): text = '\n'.join(lines) + '\n' pager = console_pager.Pager(text, out=sys.stderr) try: pager.Run() except: # pylint: disable=bare-except # pager.Run() fails with termios.error(25, 'Inappropriate ioctl for device') # for outputs that don't fit on a single screen in our test environment. pass
def More(contents, out, prompt=None, check_pager=True): """Run a user specified pager or fall back to the internal pager. Args: contents: The entire contents of the text lines to page. out: The output stream. prompt: The page break prompt. check_pager: Checks the PAGER env var and uses it if True. """ if not IsInteractive(output=True): out.write(contents) return if check_pager: pager = encoding.GetEncodedValue(os.environ, 'PAGER', None) if pager == '-': # Use the fallback Pager. pager = None elif not pager: # Search for a pager that handles ANSI escapes. for command in ('less', 'pager'): if files.FindExecutableOnPath(command): pager = command break if pager: # If the pager is less(1) then instruct it to display raw ANSI escape # sequences to enable colors and font embellishments. less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None) less = '-R' + (less_orig or '') encoding.SetEncodedValue(os.environ, 'LESS', less) # Ignores SIGINT from this point on since the child process has started # and we don't want to terminate either one when the child is still alive. signal.signal(signal.SIGINT, signal.SIG_IGN) # Runs PreexecFunc before starting the child so SIGINT is ignored for the # child process as well. p = subprocess.Popen( pager, stdin=subprocess.PIPE, shell=True, preexec_fn=PreexecFunc) enc = console_attr.GetConsoleAttr().GetEncoding() p.communicate(input=contents.encode(enc)) p.wait() # Starts using default disposition for SIGINT again after the child has # exited. signal.signal(signal.SIGINT, signal.SIG_DFL) if less_orig is None: encoding.SetEncodedValue(os.environ, 'LESS', None) return # Fall back to the internal pager. console_pager.Pager(contents, out, prompt).Run()
def More(contents, out, prompt=None, check_pager=True): """Run a user specified pager or fall back to the internal pager. Args: contents: The entire contents of the text lines to page. out: The output stream. prompt: The page break prompt. check_pager: Checks the PAGER env var and uses it if True. """ if not IsInteractive(output=True): out.write(contents) return if check_pager: pager = encoding.GetEncodedValue(os.environ, "PAGER", None) if pager == "-": # Use the fallback Pager. pager = None elif not pager: # Search for a pager that handles ANSI escapes. for command in ("less", "pager"): if files.FindExecutableOnPath(command): pager = command break if pager: # If the pager is less(1) then instruct it to display raw ANSI escape # sequences to enable colors and font embellishments. less_orig = encoding.GetEncodedValue(os.environ, "LESS", None) less = "-R" + (less_orig or "") encoding.SetEncodedValue(os.environ, "LESS", less) # Ignore SIGINT while the pager is running. # We don't want to terminate the parent while the child is still alive. signal.signal(signal.SIGINT, signal.SIG_IGN) p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True) enc = console_attr.GetConsoleAttr().GetEncoding() p.communicate(input=contents.encode(enc)) p.wait() # Start using default signal handling for SIGINT again. signal.signal(signal.SIGINT, signal.SIG_DFL) if less_orig is None: encoding.SetEncodedValue(os.environ, "LESS", None) return # Fall back to the internal pager. console_pager.Pager(contents, out, prompt).Run()
def More(contents, out, prompt=None, check_pager=True): """Run a user specified pager or fall back to the internal pager. Args: contents: The entire contents of the text lines to page. out: The output stream. prompt: The page break prompt. check_pager: Checks the PAGER env var and uses it if True. """ if not IsInteractive(output=True): out.write(contents) return if check_pager: pager = encoding.GetEncodedValue(os.environ, 'PAGER', None) if pager == '-': # Use the fallback Pager. pager = None elif not pager: # Search for a pager that handles ANSI escapes. for command in ('less', 'pager'): if files.FindExecutableOnPath(command): pager = command break if pager: # If the pager is less(1) then instruct it to display raw ANSI escape # sequences to enable colors and font embellishments. less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None) less = '-R' + (less_orig or '') encoding.SetEncodedValue(os.environ, 'LESS', less) p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True) enc = console_attr.GetConsoleAttr().GetEncoding() p.communicate(input=contents.encode(enc)) p.wait() if less_orig is None: encoding.SetEncodedValue(os.environ, 'LESS', None) return # Fall back to the internal pager. console_pager.Pager(contents, out, prompt).Run()