def print_with_pager(output):
    """Print the output to `stdout` using less when in a tty."""
    if sys.stdout.isatty():
        try:
            pager = subprocess.Popen(
                ['less', '-F', '-r', '-S', '-X', '-K'],
                stdin=subprocess.PIPE, stdout=sys.stdout)
        except subprocess.CalledProcessError:
            # Don't use the pager since starting it has failed.
            print(output)
            return
        else:
            pager.stdin.write(output.encode('utf-8'))
            pager.stdin.close()
            pager.wait()
    else:
        # Output directly to stdout since not in tty.
        print(output)
def print_with_pager ( output ) : if sys . stdout . isatty ( ) : try : pager = subprocess . Popen ( [ 'less' , '-F' , '-r' , '-S' , '-X' , '-K' ] , stdin = subprocess . PIPE , stdout = sys . stdout ) except subprocess . CalledProcessError : print ( output ) return else : pager . stdin . write ( output . encode ( 'utf-8' ) ) pager . stdin . close ( ) pager . wait ( ) else : print ( output )
Print the output to `stdout` using less when in a tty.