Skip to content

Commit 3295054

Browse files
authored
Add DocBox printer for 2D layout. (#8)
1 parent 540dba0 commit 3295054

File tree

7 files changed

+758
-4
lines changed

7 files changed

+758
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ Hello,
162162
Dodo inserts the break and indent. "Hello, World!" on a single line would
163163
exceed the maximum page width, so it uses the flex default (`break`) instead.
164164

165+
### Two-Dimensional Layouts
166+
167+
Dodo also supports two-dimensional layouts through the `Dodo.Box` interface.
168+
Boxes can be joined and aligned both vertically and horizontally to create
169+
complex layouts such as tables or grids.
170+
165171
## Examples
166172

167173
* Colorful, flexible JSON printer ([code](test/snapshots/DodoExampleJson.purs), [output](test/snapshots/DodoExampleJson.output))
168174
* Text paragraphs ([code](test/snapshots/DodoTextParagraph.purs), [output](test/snapshots/DodoTextParagraph.output))
175+
* 2D layout ([code](test/snapshots/DodoBox.purs), [output](test/snapshots/DodoBox.output))

spago.dhall

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ You can edit this file as you like.
1818
, "lists"
1919
, "maybe"
2020
, "minibench"
21+
, "newtype"
2122
, "node-buffer"
2223
, "node-child-process"
2324
, "node-fs-aff"
2425
, "node-path"
2526
, "node-process"
2627
, "node-streams"
2728
, "parallel"
29+
, "partial"
2830
, "posix-types"
2931
, "prelude"
3032
, "psci-support"
33+
, "safe-coerce"
3134
, "strings"
3235
, "tuples"
3336
]

src/Dodo.purs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Dodo
2929
, foldWithSeparator
3030
, foldWith
3131
, locally
32+
, withLocalOptions
3233
, print
3334
, Printer(..)
3435
, plainText
@@ -49,6 +50,7 @@ import Data.String as String
4950
import Data.String.Regex as Regex
5051
import Data.String.Regex.Flags (global)
5152
import Data.String.Regex.Unsafe (unsafeRegex)
53+
import Data.Tuple (Tuple(..))
5254
import Dodo.Internal (Doc(..), Position, LocalOptions, bothNotEmpty, isEmpty, notEmpty)
5355
import Dodo.Internal (Doc, Position, bothNotEmpty, isEmpty, notEmpty) as Exports
5456
import Dodo.Internal.Buffer (Buffer)
@@ -197,7 +199,13 @@ foldWith f = foldr (bothNotEmpty f) mempty
197199
-- | *EXPERIMENTAL:* modifies printing state and options locally for a document.
198200
-- | This may change or be removed at any time.
199201
locally :: forall a. (LocalOptions -> LocalOptions) -> Doc a -> Doc a
200-
locally = Local
202+
locally k doc = Local \options -> Tuple (k options) doc
203+
204+
-- | *EXPERIMENTAL:* modifies printing state and options locally for a document.
205+
-- | This may change or be removed at any time. Differs from `locally` in that the
206+
-- | document can be responsive to options.
207+
withLocalOptions :: forall a. (LocalOptions -> Tuple LocalOptions (Doc a)) -> Doc a
208+
withLocalOptions = Local
201209

202210
-- | Custom printers can be used to render richer documents than just plain
203211
-- | text.
@@ -453,7 +461,7 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
453461
{ annotations = ann : state.annotations
454462
, buffer = Buffer.modify (printer.enterAnnotation ann state.annotations) state.buffer
455463
}
456-
Local k doc1 -> do
464+
Local k -> do
457465
let
458466
prevOptions =
459467
{ indent: state.position.indent
@@ -463,7 +471,7 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc
463471
, pageWidth: state.options.pageWidth
464472
, ribbonRatio: state.options.ribbonRatio
465473
}
466-
localOptions = k prevOptions
474+
Tuple localOptions doc1 = k prevOptions
467475
go (Doc doc1 : LeaveLocal prevOptions: stk) $ storeOptions state.position.indent localOptions state
468476
Empty ->
469477
go stk state

0 commit comments

Comments
 (0)