@@ -534,6 +534,18 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
534534 # quote matches that are completed in a delimited fashion
535535 self .matches_delimited = False
536536
537+ # Set the pager(s) for use with the ppaged() method for displaying output using a pager
538+ if sys .platform .startswith ('win' ):
539+ self .pager = self .pager_chop = 'more'
540+ else :
541+ # Here is the meaning of the various flags we are using with the less command:
542+ # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
543+ # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
544+ # -X disables sending the termcap initialization and deinitialization strings to the terminal
545+ # -F causes less to automatically exit if the entire file can be displayed on the first screen
546+ self .pager = 'less -RXF'
547+ self .pager_chop = 'less -SRXF'
548+
537549 # ----- Methods related to presenting output to the user -----
538550
539551 @property
@@ -608,14 +620,20 @@ def pfeedback(self, msg: str) -> None:
608620 else :
609621 sys .stderr .write ("{}\n " .format (msg ))
610622
611- def ppaged (self , msg : str , end : str = '\n ' ) -> None :
623+ def ppaged (self , msg : str , end : str = '\n ' , chop : bool = False ) -> None :
612624 """Print output using a pager if it would go off screen and stdout isn't currently being redirected.
613625
614626 Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
615627 stdout or stdin are not a fully functional terminal.
616628
617- :param msg: str - message to print to current stdout - anything convertible to a str with '{}'.format() is OK
618- :param end: str - string appended after the end of the message if not already present, default a newline
629+ :param msg: message to print to current stdout - anything convertible to a str with '{}'.format() is OK
630+ :param end: string appended after the end of the message if not already present, default a newline
631+ :param chop: True -> causes lines longer than the screen width to be chopped (truncated) rather than wrapped
632+ - truncated text is still accessible by scrolling with the right & left arrow keys
633+ - chopping is ideal for displaying wide tabular data as is done in utilities like pgcli
634+ False -> causes lines longer than the screen width to wrap to the next line
635+ - wrapping is ideal when you want to avoid users having to use horizontal scrolling
636+ WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
619637 """
620638 import subprocess
621639 if msg is not None and msg != '' :
@@ -635,17 +653,10 @@ def ppaged(self, msg: str, end: str='\n') -> None:
635653 # Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
636654 # Also only attempt to use a pager if actually running in a real fully functional terminal
637655 if functional_terminal and not self .redirecting and not self ._in_py and not self ._script_dir :
638-
639- if sys .platform .startswith ('win' ):
640- pager_cmd = 'more'
641- else :
642- # Here is the meaning of the various flags we are using with the less command:
643- # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
644- # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
645- # -X disables sending the termcap initialization and deinitialization strings to the terminal
646- # -F causes less to automatically exit if the entire file can be displayed on the first screen
647- pager_cmd = 'less -SRXF'
648- self .pipe_proc = subprocess .Popen (pager_cmd , shell = True , stdin = subprocess .PIPE )
656+ pager = self .pager
657+ if chop :
658+ pager = self .pager_chop
659+ self .pipe_proc = subprocess .Popen (pager , shell = True , stdin = subprocess .PIPE )
649660 try :
650661 self .pipe_proc .stdin .write (msg_str .encode ('utf-8' , 'replace' ))
651662 self .pipe_proc .stdin .close ()
0 commit comments