File: Pager.Rd

package info (click to toggle)
r-cran-diffobj 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,432 kB
  • sloc: ansic: 455; javascript: 96; sh: 32; makefile: 8
file content (273 lines) | stat: -rwxr-xr-x 11,698 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pager.R
\docType{class}
\name{Pager}
\alias{Pager}
\alias{PagerOff,}
\alias{PagerSystem,}
\alias{PagerSystemLess,}
\alias{PagerBrowser}
\alias{PagerOff-class}
\alias{PagerOff}
\alias{PagerSystem-class}
\alias{PagerSystem}
\alias{PagerSystemLess-class}
\alias{PagerSystemLess}
\title{Objects for Specifying Pager Settings}
\usage{
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{
\item{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.
\code{PagerSystem} and \code{PagerSystemLess} use \code{\link{file.show}}
by default, and \code{PagerBrowser} uses
\code{\link{view_or_browse}} for HTML output.  For asynchronous pagers such
as \code{view_or_browse} it is important to make the pager function
blocking by setting the \code{make.blocking} parameter to TRUE, or to
specify a pager file path explicitly with \code{file.path}.}

\item{file.ext}{character(1L) an extension to append to file path passed to
\code{pager}, \emph{without} the period.  For example, \code{PagerBrowser}
uses \dQuote{html} to cause \code{\link{browseURL}} to launch the web
browser.  This parameter will be overridden if \code{file.path} is used.}

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

\item{ansi}{TRUE or FALSE, whether the pager supports ANSI CSI SGR sequences.}

\item{file.path}{character(1L), if not NA the diff will be written to this
location, ignoring the value of \code{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.}

\item{make.blocking}{TRUE, FALSE, or NA. Whether to wrap \code{pager} with
\code{\link{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 \code{PagerBrowser} where it defaults to NA,
which resolves to \code{is.na(file.path)} (i.e. it is TRUE if the diff is
being written to a temporary file, and FALSE otherwise).}

\item{...}{additional arguments to pass on to \code{new} that are passed on
to parent classes.}

\item{flags}{character(1L), only for \code{PagerSystemLess}, what flags to
set with the \code{LESS} system environment variable.  By default the
\dQuote{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
\dQuote{RX}.  You should only provide the flag letters (e.g. \dQuote{"RX"},
not \code{"-RX"}).  The system variable is only modified for the duration
of the evaluation and is reset / unset afterwards. \emph{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.}
}
\description{
Initializers for pager configuration objects that modify pager behavior.
These objects can be used as the \code{pager} argument to the
\code{\link[=diffPrint]{diff*}} methods, or as the \code{pager} slot for
\code{\link{Style}} objects.  In this documentation we use the \dQuote{pager}
term loosely and intend it to refer to any device other than the terminal
that can be used to render output.
}
\section{Default Output Behavior}{


\code{\link[=diffPrint]{diff*}} methods use \dQuote{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 \code{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
\code{\link{console_lines}}) it is sent to the pager.

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

\section{Pagers and Styles}{


There is a close relationship between pagers and \code{\link{Style}}.  The
\code{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 \code{\link{Style}} objects come with a
\code{Pager} configuration object pre-assigned so the output can render
correctly.  The exact \code{Pager} configuration object depends on the
\code{\link{Style}} as well as the system configuration.

In any call to the \code{\link[=diffPrint]{diff*}} methods you can always
specify both the \code{\link{Style}} and \code{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
\code{options(diffobj.pager=..., diffobj.style=...)} so that you do not need
to specify them each time you use \code{diffobj}.
}

\section{Pager Configuration Objects}{


The \code{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:
\itemize{
  \item \code{Pager}: Generic pager just outputs directly to terminal; not
    useful unless the default parameters are modified.
  \item \code{PagerOff}: Turn off pager
  \item \code{PagerSystem}: Use the system pager as invoked by
     \code{\link{file.show}}
  \item \code{PagerSystemLess}: Like \code{PagerSystem}, but provides
     additional configuration options if the system pager is \code{less}.
     Note this object does not change the system pager; it only allows you to
     configure it via the \code{$LESS} environment variable which will have
     no effect unless the system pager is set to be \code{less}.
  \item \code{PagerBrowser}: Use \code{getOption("viewer")} if defined, or
    \code{\link{browseURL}} if not
}
The default configuration for \code{PagerSystem} and \code{PagerSystemLess}
leads to output being sent to the pager if it exceeds the estimated window
size, whereas \code{PagerBrowser} always sends output to the pager.  This
behavior can be configured via the \code{threshold} parameter.

\code{PagerSystemLess}'s primary role is to correctly configure the
\code{$LESS} system variable so that \code{less} renders the ANSI escape
sequences as intended.  On OS X \code{more} is a faux-alias to \code{less},
except it does not appear to read the \code{$LESS} system variable.
Should you configure your system pager to be the \code{more} version of
\code{less}, \code{\link{pager_is_less}} will be tricked into thinking you
are using a \dQuote{normal} version of \code{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.
}

\section{Custom Pager Configurations}{


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

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

If you wish to define your own pager object you should do so by extending the
any of the \code{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 \code{make.blocking} parameter to
TRUE to pause execution prior to deleting the temporary file that contains
the diff.
}

\examples{
## We `dontrun` these examples as they involve pagers that should only be run
## in interactive mode
\dontrun{
## 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)
}
}
\seealso{
\code{\link{Style}}, \code{\link{pager_is_less}}
}