Class Doc
- java.lang.Object
-
- com.opencastsoftware.prettier4j.Doc
-
- Direct Known Subclasses:
Doc.Alternatives
,Doc.Append
,Doc.CloseLink
,Doc.Empty
,Doc.Escape
,Doc.Indent
,Doc.LineOr
,Doc.Link
,Doc.Margin
,Doc.OpenLink
,Doc.Param
,Doc.Reset
,Doc.Styled
,Doc.Text
,Doc.WrapText
public abstract class Doc extends Object
Implements the algorithm described in Philip Wadler's "A prettier printer", a pretty printing algorithm for laying out hierarchical documents as text.To construct a document, see the static methods of
Doc
, especiallytext
,empty
,line
and its related methods.To concatenate documents, see
append
and its related instance methods.To declare groups of content which should be collapsed onto one line if possible, see the static method
group
.To style a
Doc
with ANSI escape codes, see the instance methodstyled(Styles.StylesOperator...)
or static methodstyled(Doc, Styles.StylesOperator...)
.To render documents to an
Appendable
output, see the instance methodrender(RenderOptions, Appendable)
or static methodrender(Doc, RenderOptions, Appendable)
.To render documents to
String
, see the instance methodrender(RenderOptions)
or static methodrender(Doc, RenderOptions)
.- See Also:
- A prettier printer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Doc.Alternatives
Represents a choice between a flattened and expanded layout for a singleDoc
.static class
Doc.Append
Represents the concatenation of twoDoc
s.static class
Doc.CloseLink
Represents the ANSI escape code sequence for closing a hyperlink.static class
Doc.Empty
Represents an emptyDoc
.static class
Doc.Escape
Represents an ANSI escape code sequence.static class
Doc.Indent
Represents an indentedDoc
.static class
Doc.Line
Represents a line break which cannot be flattened into a more compact layout.static class
Doc.LineOr
Represents a line break which can be flattened into an alternative documentaltDoc
.static class
Doc.LineOrEmpty
Represents a line break which can be flattened into an empty document.static class
Doc.LineOrSpace
Represents a line break which can be flattened into a single space character.static class
Doc.Link
static class
Doc.Margin
Represents aDoc
within which every new line is prefixed by a margin.static class
Doc.OpenLink
Represents the ANSI escape code sequence for opening a hyperlink.static class
Doc.Param
Represents a placeholder for aDoc
that will be provided as a parameter.static class
Doc.Reset
static class
Doc.Styled
Represents aDoc
styled with ANSI escape codes.static class
Doc.Text
Represents an atomic piece of text.static class
Doc.WrapText
Represents a long text string which may be wrapped to fit within the preferred rendering width.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Doc
append(Doc other)
Append theother
Doc
to this one.Doc
appendLine(Doc other)
Append theother
Doc
to this one, separated by a line break which cannot be flattened.Doc
appendLineOr(Doc altDoc, Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into thealtDoc
document.Doc
appendLineOr(String altText, Doc other)
Doc
appendLineOrEmpty(Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into an empty document.Doc
appendLineOrSpace(Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into a space character.Doc
appendSpace(Doc other)
Append theother
Doc
to this one, separated by a space character.Doc
bind(Object... bindings)
Bind named parameters to the name-to-Doc
pairs provided viabindings
.abstract Doc
bind(String name, Doc value)
Bind a named parameter to theDoc
provided viavalue
.Doc
bind(String name, String value)
Bind a named parameter to theString
provided viavalue
.abstract Doc
bind(Map<String,Doc> bindings)
Bind named parameters to theDoc
s provided viabindings
.Doc
bracket(int indent, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces.Doc
bracket(int indent, Doc lineDoc, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces.Doc
bracket(int indent, Doc lineDoc, Doc marginDoc, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces, applying the margin documentmargin
.Doc
bracket(int indent, Doc lineDoc, String left, String right)
Bracket the current document by theleft
andright
Strings, indented byindent
spaces.Doc
bracket(int indent, String left, String right)
Bracket the current document by theleft
andright
Strings, indented byindent
spaces.static Doc
empty()
Creates an emptyDoc
.static Doc
fold(Collection<Doc> documents, BinaryOperator<Doc> fn)
Reduce a collection of documents using the binary operatorfn
, returning an empty document if the collection is empty.static Doc
fold(Stream<Doc> documents, BinaryOperator<Doc> fn)
Reduce a stream of documents using the binary operatorfn
, returning an empty document if the stream is empty.static Doc
group(Doc doc)
Creates aDoc
which represents a group that can be flattened into a more compact layout.Doc
indent(int indent)
Indent the currentDoc
byindent
spaces.static Doc
indent(int indent, Doc doc)
Indent the inputDoc
byindent
spaces.static Doc
intersperse(Doc separator, Collection<Doc> documents)
Intersperse aseparator
document in between the elements of a collection of documents.static Doc
intersperse(Doc separator, Stream<Doc> documents)
Intersperse aseparator
document in between the elements of a stream of documents.static Doc
line()
Creates aDoc
representing a line break which cannot be flattened.static Doc
lineOr(Doc altDoc)
Creates aDoc
representing a line break which may be flattened into an alternative documentaltDoc
.static Doc
lineOr(String altText)
Creates aDoc
representing a line break which may be flattened into the alternative textaltText
.static Doc
lineOrEmpty()
Creates aDoc
representing a line break which may be flattened into an empty document.static Doc
lineOrSpace()
Creates aDoc
representing a line break which may be flattened into a single space character.static Doc
link(Doc doc, URI uri)
Converts the inputDoc
into a hyperlink to theuri
provided.Doc
link(URI uri)
Converts the currentDoc
into a hyperlink to theuri
provided.Doc
margin(Doc margin)
Apply the margin documentmargin
to the currentDoc
, emitting the margin at the start of every new line from the start of this document until the end of the document.static Doc
margin(Doc margin, Doc doc)
Apply the margin documentmargin
to the currentDoc
, emitting the margin at the start of every new line from the start of this document until the end of the document.static Doc
param(String name)
String
render()
String
render(int width)
void
render(int width, Appendable output)
Renders the currentDoc
into anAppendable
, attempting to lay out the document with at mostwidth
characters on each line.static String
render(Doc doc)
static String
render(Doc doc, RenderOptions options)
static void
render(Doc doc, RenderOptions options, Appendable output)
Renders the inputDoc
into anAppendable
, attempting to lay out the document according to the renderingoptions
.static void
render(Doc doc, Appendable output)
Renders the inputDoc
into anAppendable
, attempting to lay out the document according to thedefault
rendering options.String
render(RenderOptions options)
void
render(RenderOptions options, Appendable output)
Renders the currentDoc
into anAppendable
, attempting to lay out the document according to the renderingoptions
.void
render(Appendable output)
Renders the currentDoc
into anAppendable
, attempting to lay out the document according to thedefault
rendering options.Doc
styled(Styles.StylesOperator... styles)
Styles the currentDoc
using the styles provided viastyles
.static Doc
styled(Doc doc, Styles.StylesOperator... styles)
Styles the inputDoc
using the styles provided viastyles
.static Doc
text(String text)
Construct aDoc
from thetext
.static Doc
wrapText(String text)
Construct aDoc
which attempts to wrap the inputtext
to fit within the preferred rendering width.
-
-
-
Method Detail
-
bind
public abstract Doc bind(String name, Doc value)
Bind a named parameter to theDoc
provided viavalue
.- Parameters:
name
- the name of the parameter.value
- the value to use to replace the parameter placeholder.- Returns:
- this
Doc
with all instances of the named parameter replaced byvalue
.
-
bind
public abstract Doc bind(Map<String,Doc> bindings)
Bind named parameters to theDoc
s provided viabindings
.- Parameters:
bindings
- the bindings to use to replace named parameters.- Returns:
- this
Doc
with all matching named parameters replaced by their corresponding values.
-
bind
public Doc bind(Object... bindings)
Bind named parameters to the name-to-Doc
pairs provided viabindings
.- Parameters:
bindings
- the bindings to use to replace named parameters.- Returns:
- this
Doc
with all matching named parameters replaced by their corresponding values.
-
bind
public Doc bind(String name, String value)
Bind a named parameter to theString
provided viavalue
.- Returns:
- this
Doc
with all instances of the named parameter replaced byvalue
.
-
append
public Doc append(Doc other)
Append theother
Doc
to this one.- Parameters:
other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendSpace
public Doc appendSpace(Doc other)
Append theother
Doc
to this one, separated by a space character.- Parameters:
other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendLine
public Doc appendLine(Doc other)
Append theother
Doc
to this one, separated by a line break which cannot be flattened.- Parameters:
other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendLineOrSpace
public Doc appendLineOrSpace(Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into a space character.- Parameters:
other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendLineOrEmpty
public Doc appendLineOrEmpty(Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into an empty document.- Parameters:
other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendLineOr
public Doc appendLineOr(String altText, Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into thealtText
String
.- Parameters:
altText
- the alternative text to display if the line break is flattened.other
- the other document.- Returns:
- the concatenated
Doc
.
-
appendLineOr
public Doc appendLineOr(Doc altDoc, Doc other)
Append theother
Doc
to this one, separated by a line break which may be flattened into thealtDoc
document.- Parameters:
altDoc
- the alternative document to display if the line break is flattened.other
- the other document.- Returns:
- the concatenated
Doc
.
-
indent
public Doc indent(int indent)
Indent the currentDoc
byindent
spaces.- Parameters:
indent
- the number of spaces of indent to apply.- Returns:
- the indented document.
-
bracket
public Doc bracket(int indent, String left, String right)
Bracket the current document by theleft
andright
Strings, indented byindent
spaces.When collapsed, line separators are replaced by spaces.
- Parameters:
indent
- the number of spaces of indent to apply.left
- the left-hand bracket String.right
- the right-hand bracket String.- Returns:
- the bracketed document.
-
bracket
public Doc bracket(int indent, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces.When collapsed, line separators are replaced by spaces.
- Parameters:
indent
- the number of spaces of indent to apply.left
- the left-hand bracket document.right
- the right-hand bracket document.- Returns:
- the bracketed document.
-
bracket
public Doc bracket(int indent, Doc lineDoc, String left, String right)
Bracket the current document by theleft
andright
Strings, indented byindent
spaces.When collapsed, line separators are replaced by the
lineDoc
.- Parameters:
indent
- the number of spaces of indent to apply.lineDoc
- the line separator document.left
- the left-hand bracket document.right
- the right-hand bracket document.- Returns:
- the bracketed document.
-
bracket
public Doc bracket(int indent, Doc lineDoc, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces.When collapsed, line separators are replaced by the
lineDoc
.- Parameters:
indent
- the number of spaces of indent to apply.lineDoc
- the line separator document.left
- the left-hand bracket document.right
- the right-hand bracket document.- Returns:
- the bracketed document.
-
bracket
public Doc bracket(int indent, Doc lineDoc, Doc marginDoc, Doc left, Doc right)
Bracket the current document by theleft
andright
documents, indented byindent
spaces, applying the margin documentmargin
.When collapsed, line separators are replaced by the
lineDoc
.- Parameters:
indent
- the number of spaces of indent to apply.lineDoc
- the line separator document.marginDoc
- the margin document.left
- the left-hand bracket document.right
- the right-hand bracket document.- Returns:
- the bracketed document.
-
margin
public Doc margin(Doc margin)
Apply the margin documentmargin
to the currentDoc
, emitting the margin at the start of every new line from the start of this document until the end of the document.Note that line separators are forbidden inside the margin document.
This is because each line separator causes the margin document to be produced.
If the margin document in turn contained a line separator, rendering would never terminate.
- Parameters:
margin
- the margin document to apply at the start of every line.- Returns:
- a document which prefixes every new line with the
margin
document. - Throws:
IllegalArgumentException
- if the margin document contains line separators.
-
styled
public final Doc styled(Styles.StylesOperator... styles)
Styles the currentDoc
using the styles provided viastyles
.
-
link
public final Doc link(URI uri)
Converts the currentDoc
into a hyperlink to theuri
provided.- Parameters:
uri
- theURI
to link to.- Returns:
- a
Doc
which acts as a hyperlink to theuri
. - See Also:
- Hyperlinks (a.k.a. HTML-like anchors) in terminal emulators
-
render
public String render()
Renders the currentDoc
into aString
, attempting to lay out the document according to thedefault
rendering options.- Returns:
- the document laid out as a
String
.
-
render
public String render(RenderOptions options)
Renders the currentDoc
into aString
, attempting to lay out the document according to the renderingoptions
.- Parameters:
options
- the options to use for rendering.- Returns:
- the document laid out as a
String
.
-
render
public void render(Appendable output) throws IOException
Renders the currentDoc
into anAppendable
, attempting to lay out the document according to thedefault
rendering options.- Parameters:
output
- the output to render into.- Throws:
IOException
- if theAppendable
output
throws whenappend
ed.
-
render
public String render(int width)
Renders the currentDoc
into aString
, attempting to lay out the document with at mostwidth
characters on each line.- Parameters:
width
- the preferred maximum rendering width.- Returns:
- the document laid out as a
String
.
-
render
public void render(int width, Appendable output) throws IOException
Renders the currentDoc
into anAppendable
, attempting to lay out the document with at mostwidth
characters on each line.- Parameters:
width
- the preferred maximum rendering width.output
- the output to render into.- Throws:
IOException
- if theAppendable
output
throws whenappend
ed.
-
render
public void render(RenderOptions options, Appendable output) throws IOException
Renders the currentDoc
into anAppendable
, attempting to lay out the document according to the renderingoptions
.- Parameters:
options
- the options to use for rendering.output
- the output to render into.- Throws:
IOException
- if theAppendable
output
throws whenappend
ed.
-
wrapText
public static Doc wrapText(String text)
Construct aDoc
which attempts to wrap the inputtext
to fit within the preferred rendering width.
-
indent
public static Doc indent(int indent, Doc doc)
Indent the inputDoc
byindent
spaces.- Parameters:
indent
- the number of spaces of indent to apply.doc
- the input document- Returns:
- the indented document.
-
margin
public static Doc margin(Doc margin, Doc doc)
Apply the margin documentmargin
to the currentDoc
, emitting the margin at the start of every new line from the start of this document until the end of the document.Note that line separators are forbidden inside the margin document.
This is because each line separator causes the margin document to be produced.
If the margin document in turn contained a line separator, rendering would never terminate.
- Parameters:
margin
- the margin document to apply at the start of every line.doc
- the input document.- Returns:
- a document which prefixes every new line with the
margin
document. - Throws:
IllegalArgumentException
- if the margin document contains line separators.
-
line
public static Doc line()
Creates aDoc
representing a line break which cannot be flattened.- Returns:
- a
Doc
representing a line break which cannot be flattened.
-
lineOrEmpty
public static Doc lineOrEmpty()
Creates aDoc
representing a line break which may be flattened into an empty document.- Returns:
- a
Doc
representing a line break which may be flattened into an empty document.
-
lineOrSpace
public static Doc lineOrSpace()
Creates aDoc
representing a line break which may be flattened into a single space character.- Returns:
- a
Doc
representing a line break which may be flattened into a single space character.
-
lineOr
public static Doc lineOr(Doc altDoc)
Creates aDoc
representing a line break which may be flattened into an alternative documentaltDoc
.- Parameters:
altDoc
- the alternative document to use if the line break is flattened.- Returns:
- a
Doc
representing a line break which may be flattened into an alternative documentaltDoc
.
-
lineOr
public static Doc lineOr(String altText)
Creates aDoc
representing a line break which may be flattened into the alternative textaltText
.- Parameters:
altText
- the alternative text to use if the line break is flattened.- Returns:
- a
Doc
representing a line break which may be flattened into the alternative textaltText
.
-
styled
public static Doc styled(Doc doc, Styles.StylesOperator... styles)
Styles the inputDoc
using the styles provided viastyles
.
-
link
public static Doc link(Doc doc, URI uri)
Converts the inputDoc
into a hyperlink to theuri
provided.- Parameters:
doc
- the input document.uri
- theURI
to link to.- Returns:
- a
Doc
that acts as a hyperlink to theuri
. - See Also:
- Hyperlinks (a.k.a. HTML-like anchors) in terminal emulators
-
param
public static Doc param(String name)
Creates aDoc
which acts as a placeholder for an argumentDoc
that will be provided bybinding
parameters prior torender
ing.- Parameters:
name
- the name of the parameter.- Returns:
- a parameter
Doc
.
-
fold
public static Doc fold(Collection<Doc> documents, BinaryOperator<Doc> fn)
Reduce a collection of documents using the binary operatorfn
, returning an empty document if the collection is empty.- Parameters:
documents
- the collection of documents.fn
- the binary operator for combining documents.- Returns:
- a document built by reducing the
documents
using the operatorfn
.
-
fold
public static Doc fold(Stream<Doc> documents, BinaryOperator<Doc> fn)
Reduce a stream of documents using the binary operatorfn
, returning an empty document if the stream is empty.- Parameters:
documents
- the stream of documents.fn
- the binary operator for combining documents.- Returns:
- a document built by reducing the
documents
using the operatorfn
.
-
intersperse
public static Doc intersperse(Doc separator, Collection<Doc> documents)
Intersperse aseparator
document in between the elements of a collection of documents.- Parameters:
separator
- the separator document.documents
- the collection of documents.- Returns:
- a document containing the concatenation of
documents
separated by theseparator
.
-
intersperse
public static Doc intersperse(Doc separator, Stream<Doc> documents)
Intersperse aseparator
document in between the elements of a stream of documents.- Parameters:
separator
- the separator document.documents
- the stream of documents.- Returns:
- a document containing the concatenation of
documents
separated by theseparator
.
-
group
public static Doc group(Doc doc)
Creates aDoc
which represents a group that can be flattened into a more compact layout.- Parameters:
doc
- the document which is declared as a group which may be flattened.- Returns:
- a
Doc
which represents a group that can be flattened into a more compact layout.
-
render
public static void render(Doc doc, RenderOptions options, Appendable output) throws IOException
Renders the inputDoc
into anAppendable
, attempting to lay out the document according to the renderingoptions
.- Parameters:
doc
- the document to be rendered.options
- the options to use for rendering.output
- the output to render into.- Throws:
IOException
- if theAppendable
output
throws whenappend
ed.
-
render
public static void render(Doc doc, Appendable output) throws IOException
Renders the inputDoc
into anAppendable
, attempting to lay out the document according to thedefault
rendering options.- Parameters:
doc
- the document to be rendered.output
- the output to render into.- Throws:
IOException
- if theAppendable
output
throws whenappend
ed.
-
render
public static String render(Doc doc, RenderOptions options)
Renders the inputDoc
into aString
, attempting to lay out the document according to the renderingoptions
.- Parameters:
doc
- the document to be rendered.options
- the options to use for rendering.- Returns:
- the document laid out as a
String
.
-
-