Package: autopage
autopageAutopageis a Python library to
automatically display terminal output from a program in a pager (likeless)
whenever you need it, and never when you don't. And it only takes one line of
code.You know how some CLI programs likegit(and a handful of others, includingmanandsystemctl) automatically pipe their output toless? Except not if
there's less than one screen's worth of data. And if you redirect the output to
a file or a pipe, it does the right thing instead. Colours are preserved. Don't
you wish all programs worked like that? Now at least all of your Python
programs can.License© 2020-2022 by Zane BitterOpen Source licensed under the terms of the Apache Software License, version
2.0.InstallationAutopage is available from PyPI. The
easiest way to install (preferably in avirtualenvvirtual environment) is
withpip:$ pip install autopageOn Fedora and CentOS/RHELAutopage ispackaged for
Fedora35 and later. To install:# dnf install python3-autopageCopr repositories are available for older versions of Fedora and EPEL. Before
attempting to install on those versions, first enable the copr repository:# dnf copr enable zaneb/autopageOn Ubuntu and DebianAutopage ispackaged for
Ubuntujammy and later, andfor Debianbookworm. To install:# apt-get install python3-autopageA PPA is available for older versions of Ubuntu. Before attempting to install
on those versions, first enable the PPA:# add-apt-repository ppa:zaneb/autopage
# apt-get updateOn GentooAutopage ispackaged for
Gentoo. To install:# emerge dev-python/autopageBasic UseTheAutoPagerclass provides a context manager that furnishes the output
stream to write to. Here is a basic example that reads from stdin and outputs
to a pager connected to stdout:importsysimportautopagewithautopage.AutoPager()asout:forlinsys.stdin:out.write(l)If you are explicitly passing a stream to write to (rather than directly
referencing a global variable such assys.stdoutthen you may be able to add
automatic paging support with only a single line of code.Paging help outputIf your program uses theargparsemodule from the standard library, you can
ensure that the help output is automatically paged when possible by changing
the import statement to:fromautopageimportargparseIf you don't control the module that importsargparse, you can instead callautopage.argparse.monkey_patch()to patch the module directly. This function
can also be used as a context manager.EnvironmentThe default pager command (autopage.command.DefaultPager()) allows the end
user to override the pager command by setting thePAGERenvironment variable.
To disable this behaviour, passpager_command=autopage.command.PlatformPager()to use the default pager for
the current platform, or pass a specific pager fromautopage.command. The
default pager command islesson most platforms. On AIX the default pager
command ismore, and on Windowsmore.com.The end user can also override the settings forlessby setting theLESSenvironment variable. If not specified, the settings are determined by theallow_colorandline_bufferingoptions. By default ANSI control characters
for setting colours are respected and the pager will not run if there is less
than a full screen of text to display.Line bufferingNormally output streams are buffered so that data is written to the output file
only when the buffer becomes full. This is efficient and generally works fine
as long as the data is being produced as fast as it can be consumed. However,
when the data is streaming at a slower rate than it could be displayed (e.g.
log output from something liketail -f) this results in a large delay between
data being produced and consumed. If you have ever tried to grep a streaming
log and pipe the output to a pager then you are familiar with how
unsatisfactory this is.The solution is to flush the output buffer after each line is written, which is
known asline
buffering. TheAutoPagerclass supports aline_bufferingargument to enable or disable
line buffering. The default is to use the line buffering mode already
configured for the output stream (which is usually to disable line buffering).When reading from an input stream (which may be a file, pipe, or the console)
and optionally processing the data before outputting it again, the convenience
functionline_buffer_from_input()returns the optimal line buffering setting
for a given input stream (sys.stdinby default).importsysimportautopagewithautopage.AutoPager(line_buffering=autopage.line_buffer_from_input())aso:forlinsys.stdin:o.write(l)Terminal resetBy default, when the pager exits it will leave the latest displayed output on
screen in the terminal. This can be changed by passingTruefor thereset_on_exitargument to theAutoPagerclass. If this option is set, the
terminal will be cleared when the pager exits, returning to its position prior
to starting the pager (as is the case by default when runninglessmanually
from the command line).Exit codePrograms may wish to return a different exit code if they are interrupted by
the user (either with Ctrl-C or by closing the pager) than if they ran to
completion. The exceptions generated when the pager is closed prematurely are
suppressed, so theAutoPagerclass offers theexit_code()method to provide
a suitable exit code for the program. This also takes into account other
exceptions that bubble up through the context manager.Complete Exampleimportsysimportautopagedefprocess(input_stream,output_stream):pager=autopage.AutoPager(output_stream,line_buffering=autopage.line_buffer_from_input(input_stream),allow_color=True,reset_on_exit=True,errors=autopage.ErrorStrategy.REPLACE,)try:withpagerasout:forlininput_stream:out.write(l)exceptExceptionasexc:sys.stderr.write(f'{str(exc)}\n')exceptKeyboardInterrupt:passreturnpager.exit_code()sys.exit(process(sys.stdin,sys.stdout))