Skip to content

Commit 78bd6c8

Browse files
committed
Add offset function to get the width of a Doc
1 parent c79c483 commit 78bd6c8

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ If `a` and `b` are `Doc` elements, then `a // b` puts `a` above
332332

333333
Returns `true` iff `doc` is the empty document, `false` otherwise.
334334

335+
#### offset
336+
337+
`offset (doc)`
338+
339+
Returns the width of a `Doc` (as number of characters).
340+
335341

336342
License
337343
-------

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module Foreign.Lua.Module.DocLayout (
5353

5454
-- * Document Querying
5555
, is_empty
56+
, offset
5657

5758
-- * Marshaling
5859
, peekDoc
@@ -114,6 +115,7 @@ pushModule = do
114115
Lua.addfunction "vfill" vfill
115116
-- querying
116117
Lua.addfunction "is_empty" is_empty
118+
Lua.addfunction "offset" offset
117119
-- rendering
118120
Lua.addfunction "render" render
119121
return 1
@@ -137,6 +139,9 @@ render doc optLength = return $ Doc.render (Lua.fromOptional optLength) doc
137139
is_empty :: Doc Text -> Lua Bool
138140
is_empty = return . Doc.isEmpty
139141

142+
-- | Returns the width of a @'Doc'@.
143+
offset :: Doc Text -> Lua Int
144+
offset = return . Doc.offset
140145

141146
--
142147
-- Constructors

test/test-doclayout.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ return {
5858
assert.is_truthy(doclayout.is_empty(doclayout.empty))
5959
assert.is_truthy(doclayout.is_empty(''))
6060
assert.is_falsy(doclayout.is_empty('non-empty'))
61+
end),
62+
63+
test('offset', function ()
64+
assert.are_equal(doclayout.offset 'four', 4)
65+
assert.are_equal(doclayout.offset(doclayout.literal 'four' / 'radio'), 5)
6166
end)
6267
},
6368

0 commit comments

Comments
 (0)