1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/styles.R
3\docType{class}
4\name{Style-class}
5\alias{Style-class}
6\alias{Style}
7\alias{StyleRaw-class}
8\alias{StyleRaw}
9\alias{StyleAnsi-class}
10\alias{StyleAnsi}
11\alias{StyleAnsi8NeutralRgb-class}
12\alias{StyleAnsi8NeutralRgb}
13\alias{StyleAnsi8NeutralYb-class}
14\alias{StyleAnsi8NeutralYb}
15\alias{StyleAnsi256LightRgb-class}
16\alias{StyleAnsi256LightRgb}
17\alias{StyleAnsi256LightYb-class}
18\alias{StyleAnsi256LightYb}
19\alias{StyleAnsi256DarkRgb-class}
20\alias{StyleAnsi256DarkRgb}
21\alias{StyleAnsi256DarkYb-class}
22\alias{StyleAnsi256DarkYb}
23\alias{StyleHtml-class}
24\alias{StyleHtml}
25\alias{StyleHtmlLightRgb-class}
26\alias{StyleHtmlLightRgb}
27\alias{StyleHtmlLightYb-class}
28\alias{StyleHtmlLightYb}
29\title{Customize Appearance of Diff}
30\arguments{
31\item{funs}{a \code{\link{StyleFuns}} object that contains all the functions
32represented above}
33
34\item{text}{a \code{\link{StyleText}} object that contains the non-content
35text used by the diff (e.g. \code{gutter.insert.txt})}
36
37\item{summary}{a \code{\link{StyleSummary}} object that contains formatting
38functions and other meta data for rendering summaries}
39
40\item{pad}{TRUE or FALSE, whether text should be right padded}
41
42\item{pager}{what type of \code{\link{Pager}} to use}
43
44\item{nchar.fun}{function to use to count characters; intended mostly for
45internal use (used only for gutters as of version 0.2.0).}
46
47\item{wrap}{TRUE or FALSE, whether text should be hard wrapped at
48\code{disp.width}}
49
50\item{na.sub}{what character value to substitute for NA elements; NA elements
51are generated when lining up side by side diffs by adding padding rows; by
52default the text styles replace these with a blank character string, and
53the HTML styles leave them as NA for the HTML formatting functions to deal
54with}
55
56\item{blank}{sub what character value to replace blanks with; needed in
57particular for HTML rendering (uses \code{" "}) to prevent lines from
58collapsing}
59
60\item{disp.width}{how many columns the text representation of the objects to
61diff is allowed to take up before it is hard wrapped (assuming \code{wrap}
62is TRUE).  See param \code{disp.width} for \code{\link{diffPrint}}.}
63
64\item{finalizer}{function that accepts at least two parameters and requires
65no more than two parameters, will receive as the first parameter the
66the object to render (either a \code{Diff} or a \code{DiffSummary}
67object), and the text representation of that object as the second
68argument.  This allows final modifications to the character output so that
69it is displayed correctly by the pager.  For example, \code{StyleHtml}
70objects use it to generate HTML headers if the \code{Diff} is destined to
71be displayed in a browser.  The object themselves are passed along to
72provide information about the paging device and other contextual data to
73the function.}
74
75\item{html.output}{(\code{StyleHtml} objects only) one of:
76\itemize{
77  \item \dQuote{page}: Include all HTML/CSS/JS required to create a
78    stand-alone web page with the diff; in this mode the diff string will
79    be re-encoded with \code{\link{enc2utf8}} and the HTML page encoding
80    will be declared as UTF-8.
81  \item \dQuote{diff.w.style}: The CSS and HTML, but without any of the
82    outer tags that would make it a proper HTML page (i.e. no
83    \code{<html>/<head>} tags or the like) and without the JS; note that
84    technically this is illegal HTML since we have \code{<style>} tags
85    floating outside of \code{<head>} tags, but it seems to work in most
86    browsers.
87  \item \dQuote{diff.only}: Like \dQuote{diff.w.style}, but without the CSS
88  \item \dQuote{auto}: Pick one of the above based on \code{Pager}, will
89     chose \dQuote{page} if the pager is of type \code{PagerBrowser} (as in
90     that case the output is destined to be displayed in a browser like
91     device), or \dQuote{diff.only} if it is not.
92}}
93
94\item{escape.html.entities}{(\code{StyleHtml} objects only) TRUE (default)
95or FALSE, whether to escape HTML entities in the input}
96
97\item{scale}{(\code{StyleHtml} objects only) TRUE (default) or FALSE,
98whether to scale HTML output to fit to the viewport}
99
100\item{css}{(\code{StyleHtml} objects only) path to file containing CSS styles
101to style HTML output with}
102
103\item{js}{(\code{StyleHtml} objects only) path to file containing Javascript
104used for scaling output to viewports.}
105}
106\value{
107Style S4 object
108}
109\description{
110S4 objects that expose the formatting controls for \code{Diff}
111objects.  Many predefined formats are defined as classes that extend the
112base \code{Style} class.  You may fine tune styles by either extending
113the pre-defined classes, or modifying an instance thereof.
114}
115\section{Pre-defined Classes}{
116
117
118Pre-defined classes are used to populate the \code{\link{PaletteOfStyles}}
119object, which in turn allows the \code{diff*} methods to pick the
120appropriate \code{Style} for each combination of the \code{format},
121\code{color.mode}, and \code{brightness} parameters when the \code{style}
122parameter is set to \dQuote{auto}.  The following classes are pre-defined:
123
124\itemize{
125  \item \code{StyleRaw}: No styles applied
126  \item \code{StyleAnsi8NeutralRgb}
127  \item \code{StyleAnsi8NeutralYb}
128  \item \code{StyleAnsi256LightRgb}
129  \item \code{StyleAnsi256LightYb}
130  \item \code{StyleAnsi256DarkRgb}
131  \item \code{StyleAnsi256DarkYb}
132  \item \code{StyleHtmlLightRgb}
133  \item \code{StyleHtmlLightYb}
134}
135Each of these classes has an associated constructor function with the
136same name (see examples).  Objects instantiated from these classes
137may also be used directly as the value for the \code{style} parameter to the
138\code{diff*} methods. This will override the automatic selection process
139that uses \code{\link{PaletteOfStyles}}.  If you wish to tweak an
140auto-selected style rather than explicitly specify one, pass a parameter
141list instead of a \code{Style} objects as the \code{style} parameter to the
142\code{diff*} methods (see examples).
143
144There are predefined classes for most combinations of
145\code{format/color.mode/brightness}, but not all.  For example, there are
146only \dQuote{light} \code{brightness} defined for the \dQuote{html}
147\code{format}, and those classes are re-used for all possible
148\code{brightness} values, and the 8 color ANSI neutral classes are used
149for the 256 color neutral selections as well.
150
151To get a preview of what a style looks like just instantiate
152an object; the \code{show} method will output a trivial diff to screen with
153styles applied.  Note that for ANSI styles of the dark and light variety
154the show method colors the terminal background and foregrounds in compatible
155colors.  In normal usage the terminal background and foreground colors are
156left untouched so you should not expect light styles to look good on dark
157background and vice versa even if they render correctly when showing the
158style object.
159}
160
161\section{Style Structure}{
162
163
164Most of the customization is done by specifying functions that operate on
165character vectors and return a modified character vector of the same length.
166The intended use case is to pass \code{crayon} functions such as
167\code{crayon::red}, although you may pass any function of your liking
168that behaves as described.  Formatting functions are expected to return their
169inputs formatted in such a way that their \emph{display} width is unchanged.
170If your formatting functions change display width output may not render
171properly, particularly when using \code{mode="sidebyside"}.
172
173The visual representation of the diff has many nested components.  The
174functions you specify here will be applied starting with the innermost ones.
175A schematic of the various component that represent an inserted line follows
176(note \dQuote{insert} abbreviated to \dQuote{ins}, and \dQuote{gutter}
177abbreviated to \dQuote{gtr}):
178\preformatted{+- line ---------------------------------------------------+
179|+- line.ins ---------------------------------------------+|
180||+- gtr ------------------------++- text ---------------+||
181|||+- gtr.ins ---++- gtr.pad ---+||+- text.ins ---------+|||
182||||             ||             ||||      +- word.ins -+||||
183|||| gtr.ins.txt || gtr.pad.txt |||| DIFF | TEXT HERE  |||||
184||||             ||             ||||      +------------+||||
185|||+-------------++-------------+||+--------------------+|||
186||+------------------------------++----------------------+||
187|+--------------------------------------------------------+|
188+----------------------------------------------------------+
189}
190A similar model applies to deleted and matching lines.  The boxes represent
191functions.  \code{gutter.insert.txt} represents the text to use in the gutter
192and is not a function. \code{DIFF TEXT HERE} is text from the objects being
193diffed, with the portion that has different words inside the
194\code{word.insert}.  \code{gutter.pad} and \code{gutter.pad.txt} are used to
195separate the gutter from the text and usually end up resolving to a space.
196
197Most of the functions defined here default to \code{\link{identity}}, but
198you are given the flexibility to fully format the diff.  See
199\code{\link{StyleFuns}} and \code{\link{StyleText}} for a full listing of
200the adjustable elements.
201
202In side-by-side mode there are two \dQuote{lines} per screen line, each with
203the structure described here.
204
205The structure described here may change in the future.
206}
207
208\section{HTML Styles}{
209
210
211If you use a \code{Style} that inherits from \code{StyleHtml} the
212diff will be wrapped in HTML tags, styled with CSS, and output to
213\code{getOption("viewer")} if your IDE supports it (e.g. Rstudio), or
214directly to the browser otherwise, assuming that the default
215\code{\link{Pager}} or a correctly configured pager that inherits from
216\code{\link{PagerBrowser}} is in effect.  Otherwise, the raw HTML will be
217output to your terminal.
218
219By default HTML output sent to the viewer/browser is a full stand-alone
220webpage with CSS styles to format and color the diff, and JS code to
221handle scaling.  The CSS and JS is read from the
222\link[=webfiles]{default files} and injected into the HTML to simplify
223packaging of the output.  You can customize the CSS and JS by using the
224\code{css} and \code{js} arguments respectively, but read the rest of this
225documentation section if you plan on doing so.
226
227Should you want to capture the HTML output for use elsewhere, you can do
228so by using \code{as.character} on the return value of the \code{diff*}
229methods.  If you want the raw HTML without any of the headers, CSS, and
230JS use \code{html.ouput="diff.only"} when you instantiate the
231\code{StyleHtml} object (see examples), or disable the \code{\link{Pager}}.
232Another option is \code{html.output="diff.w.style"} which will add
233\code{<style>} tags with the CSS, but without wrapping those in \code{<head>}
234tags. This last option results in illegal HTML with a \code{<style>} block
235outside of the \code{<head>} block, but appears to work and is useful if you
236want to embed HTML someplace but do not have access to the headers.
237
238If you wish to modify the CSS styles you should do so cautiously.  The
239HTML and CSS work well together out of the box, but may not take to kindly
240to modifications.  The safest changes you can make are to the colors of the
241scheme.  You also probably should not modify the functions in the
242\code{@funs} slot of the \code{StyleHtml} object.  If you want to provide
243your own custom styles make a copy of the file at the location returned by
244\code{diffobj_css()}, modify it to your liking, and pass the location of your
245modified sheet back via the \code{css} argument (see examples).
246
247The javascript controls the scaling of the output such that its width fits
248in the viewport.  If you wish to turn of this behavior you can do so via the
249\code{scale} argument.  You may need to modify the javascript if you modify
250the \code{@funs} functions, but otherwise you are probably best off leaving
251the javascript untouched.  You can provide the location of a modified
252javascript file via the \code{js} argument.
253
254Both the CSS and JS files can be specified via options,
255\dQuote{diffobj.html.css}, and \dQuote{diffobj.html.js} respectively.
256
257If you define your own custom \code{StyleHtml} object you may want to modify
258the slot \code{@funs@container}.  This slot contains a function that is
259applied to the entire diff output.  For example, \code{StyleHtmlLightRgb}
260uses \code{@funs@container <- cont_f("light", "rgb")}.  \code{cont_f} returns
261a function that accepts a character vector as an argument and returns
262that value wrapped in a \code{DIV} block with class
263\dQuote{"diffobj-container light rgb"}.  This allows the CSS style sheet to
264target the \code{Diff} elements with the correct styles.
265}
266
267\section{Modifying Style Parameters Directly}{
268
269
270Often you will want to specify some of the style parameters (e.g.
271\code{scale} for html styles) while still relying on the default style
272selection to pick the specific style.  You can do so by passing a list to the
273\code{style} parameter of the \code{\link[=diffPrint]{diff*}} methods.
274See examples.
275}
276
277\section{New Classes}{
278
279
280You can in theory create entirely new classes that extent \code{Style}.  For
281example you could generate a class that renders the diff in \code{grid}
282graphics.  Note however that we have not tested such extensions and it is
283possible there is some embedded code that will misbehave with such a new
284class.
285}
286
287\examples{
288\dontrun{
289## Create a new style based on existing style by changing
290## gutter symbols and guide color; see `?StyleFuns` and
291## `?StyleText` for a full list of adjustable elements
292my.style <- StyleAnsi8NeutralYb()
293my.style   ## `show` method gives you a preview of the style
294my.style@text@gutter.insert <- "+++"
295my.style@text@gutter.delete <- "---"
296my.style@funs@text.guide <- crayon::green
297my.style   ## Notice gutters and guide color
298
299## Provide a custom style sheet; here we assume there is a style sheet at
300## `HOME/web/mycss.css`
301my.css <- file.path(path.expand("~"), "web", "mycss.css")
302diffPrint(1:5, 2:6, style=StyleHtmlLightYb(css=my.css))
303
304## Turn of scaling; notice how we pass a list to `style`
305## and we do not need to specify a specific style
306diffPrint(letters, letters[-5], format="html", style=list(scale=FALSE))
307
308## Alternatively we can do the same by specifying a style, but we must
309## give an exact html style instead of relying on preferences to pick
310## one for us
311my.style <- StyleHtmlLightYb(scale=FALSE)
312diffPrint(letters, letters[-5], style=my.style)
313}
314## Return only the raw HTML without any of the headers
315as.character(
316  diffPrint(1:5, 2:6, format="html", style=list(html.output="diff.only"))
317)
318}
319