Pager: Objects for Specifying Pager Settings

Description Usage Arguments Default Output Behavior Pagers and Styles Pager Configuration Objects Custom Pager Configurations See Also Examples

View source: R/pager.R

Description

Initializers for pager configuration objects that modify pager behavior. These objects can be used as the pager argument to the diff* methods, or as the pager slot for Style objects. In this documentation we use the “pager” term loosely and intend it to refer to any device other than the terminal that can be used to render output.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Pager(
  pager = function(x) writeLines(readLines(x)),
  file.ext = "",
  threshold = 0L,
  ansi = FALSE,
  file.path = NA_character_,
  make.blocking = FALSE
)

PagerOff(...)

PagerSystem(pager = file.show, threshold = -1L, file.ext = "", ...)

PagerSystemLess(
  pager = file.show,
  threshold = -1L,
  flags = "R",
  file.ext = "",
  ansi = TRUE,
  ...
)

PagerBrowser(
  pager = view_or_browse,
  threshold = 0L,
  file.ext = "html",
  make.blocking = NA,
  ...
)

Arguments

pager

a function that accepts at least one parameter and does not require a parameter other than the first parameter. This function will be called with a file path passed as the first argument. The referenced file will contain the text of the diff. By default this is a temporary file that will be deleted as soon as the pager function completes evaluation. PagerSystem and PagerSystemLess use file.show by default, and PagerBrowser uses view_or_browse for HTML output. For asynchronous pagers such as view_or_browse it is important to make the pager function blocking by setting the make.blocking parameter to TRUE, or to specify a pager file path explicitly with file.path.

file.ext

character(1L) an extension to append to file path passed to pager, without the period. For example, PagerBrowser uses “html” to cause browseURL to launch the web browser. This parameter will be overridden if file.path is used.

threshold

integer(1L) number of lines of output that triggers the use of the pager; negative values lead to using console_lines + 1, and zero leads to always using the pager irrespective of how many lines the output has.

ansi

TRUE or FALSE, whether the pager supports ANSI CSI SGR sequences.

file.path

character(1L), if not NA the diff will be written to this location, ignoring the value of file.ext. If NA_character_ (default), a temporary file is used and removed after the pager function completes evaluation. If not NA, the file is preserved. Beware that the file will be overwritten if it already exists.

make.blocking

TRUE, FALSE, or NA. Whether to wrap pager with make_blocking prior to calling it. This suspends R code execution until there is user input so that temporary diff files are not deleted before the pager has a chance to read them. This typically defaults to FALSE, except for PagerBrowser where it defaults to NA, which resolves to is.na(file.path) (i.e. it is TRUE if the diff is being written to a temporary file, and FALSE otherwise).

...

additional arguments to pass on to new that are passed on to parent classes.

flags

character(1L), only for PagerSystemLess, what flags to set with the LESS system environment variable. By default the “R” flag is set to ensure ANSI escape sequences are interpreted if it appears your terminal supports ANSI escape sequences. If you want to leave the output on the screen after you exit the pager you can use “RX”. You should only provide the flag letters (e.g. “"RX"”, not "-RX"). The system variable is only modified for the duration of the evaluation and is reset / unset afterwards. Note: you must specify this slot via the constructor as in the example. If you set the slot directly it will not have any effect.

Default Output Behavior

diff* methods use “pagers” to help manage large outputs and also to provide an alternative colored diff when the terminal does not support them directly.

For OS X and *nix systems where less is the pager and the terminal supports ANSI escape sequences, output is colored with ANSI escape sequences. If the output exceeds one screen height in size (as estimated by console_lines) it is sent to the pager.

If the terminal does not support ANSI escape sequences, or if the system pager is not less as detected by pager_is_less, then the output is rendered in HTML and sent to the IDE viewer (getOption("viewer")) if defined, or to the browser with browseURL if not. This behavior may seem sub-optimal for systems that have ANSI aware terminals and ANSI aware pagers other than less, but these should be rare and it is possible to configure diffobj to produce the correct output for them (see examples).

Pagers and Styles

There is a close relationship between pagers and Style. The Style objects control whether the output is raw text, formatted with ANSI escape sequences, or marked up with HTML. In order for these different types of outputs to render properly, they need to be sent to the right device. For this reason Style objects come with a Pager configuration object pre-assigned so the output can render correctly. The exact Pager configuration object depends on the Style as well as the system configuration.

In any call to the diff* methods you can always specify both the Style and Pager configuration object directly for full control of output formatting and rendering. We have tried to set-up sensible defaults for most likely use cases, but given the complex interactions involved it is possible you may need to configure things explicitly. Should you need to define explicit configurations you can save them as option values with options(diffobj.pager=..., diffobj.style=...) so that you do not need to specify them each time you use diffobj.

Pager Configuration Objects

The Pager configuration objects allow you to specify what device to use as the pager and under what circumstances the pager should be used. Several pre-defined pager configuration objects are available via constructor functions:

The default configuration for PagerSystem and PagerSystemLess leads to output being sent to the pager if it exceeds the estimated window size, whereas PagerBrowser always sends output to the pager. This behavior can be configured via the threshold parameter.

PagerSystemLess's primary role is to correctly configure the $LESS system variable so that less renders the ANSI escape sequences as intended. On OS X more is a faux-alias to less, except it does not appear to read the $LESS system variable. Should you configure your system pager to be the more version of less, pager_is_less will be tricked into thinking you are using a “normal” version of less and you will likely end up seeing gibberish in the pager. If this is your use case you will need to set-up a custom pager configuration object that sets the correct system variables.

Custom Pager Configurations

In most cases the simplest way to generate new pager configurations is to use a list specification in the diff* call. Alternatively you can start with an existing Pager object and change the defaults. Both these cases are covered in the examples.

You can change what system pager is used by PagerSystem by changing it with options(pager=...) or by changing the $PAGER environment variable. You can also explicitly set a function to act as the pager when you instantiate the Pager configuration object (see examples).

If you wish to define your own pager object you should do so by extending the any of the Pager classes. If the function you use to handle the actual paging is non-blocking (i.e. allows R code evaluation to continue after it is spawned, you should set the make.blocking parameter to TRUE to pause execution prior to deleting the temporary file that contains the diff.

See Also

Style, pager_is_less

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
## We `dontrun` these examples as they involve pagers that should only be run
## in interactive mode
## Not run: 
## Specify Pager parameters via list; this lets the `diff*` functions pick
## their preferred pager based on format and other output parameters, but
## allows you to modify the pager behavior.

f <- tempfile()
diffChr(1:200, 180:300, format='html', pager=list(file.path=f))
head(readLines(f))  # html output
unlink(f)

## Assuming system pager is `less` and terminal supports ANSI ESC sequences
## Equivalent to running `less -RFX`

diffChr(1:200, 180:300, pager=PagerSystemLess(flags="RFX"))

## If the auto-selected pager would be the system pager, we could
## equivalently use:

diffChr(1:200, 180:300, pager=list(flags="RFX"))

## System pager is not less, but it supports ANSI escape sequences

diffChr(1:200, 180:300, pager=PagerSystem(ansi=TRUE))

## Use a custom pager, in this case we make up a trivial one and configure it
## always page (`threshold=0L`)

page.fun <- function(x) cat(paste0("| ", readLines(x)), sep="\n")
page.conf <- PagerSystem(pager=page.fun, threshold=0L)
diffChr(1:200, 180:300, pager=page.conf, disp.width=getOption("width") - 2)

## Set-up the custom pager as the default pager

options(diffobj.pager=page.conf)
diffChr(1:200, 180:300)

## A blocking pager (this is effectively very similar to what `PagerBrowser`
## does); need to block b/c otherwise temp file with diff could be deleted
## before the device has a chance to read it since `browseURL` is not
## blocking itself.  On OS X we need to specify the extension so the correct
## program opens it (in this case `TextEdit`):

page.conf <- Pager(pager=browseURL, file.ext="txt", make.blocking=TRUE)
diffChr(1:200, 180:300, pager=page.conf, format='raw')

## An alternative to a blocking pager is to disable the
## auto-file deletion; here we also specify a file location
## explicitly so we can recover the diff text.

f <- paste0(tempfile(), ".html")  # must specify .html
diffChr(1:5, 2:6, format='html', pager=list(file.path=f))
tail(readLines(f))
unlink(f)

## End(Not run)

diffobj documentation built on Oct. 5, 2021, 9:07 a.m.