Wrapping widget for Markup

22 views
Skip to first unread message

Samuel Tay

unread,
Dec 12, 2017, 11:00:05 PM12/12/17
to Brick Users
Hi. I'd like a function similar to txtWrapWith and strWrapWith, but instead of just accepting Text and String, one that accepts Brick.Markup.Markup. From what I can tell, no such function exists.

I started to look into writing one.. it might be a PITA. It looks like I'll have to go into the word-wrap module and rewrite all of the functions dealing with "Text" to handle some other type that attaches attributes to text, such as "[(Text, a)]". After that, I would think the logic for txtWrapWith would transfer over with some tweaking.

Just wanted to check and see if anything like this is already in the works, has it been thought about, and am I thinking about it the correct way.

Jonathan Daugherty

unread,
Dec 12, 2017, 11:34:04 PM12/12/17
to Samuel Tay, Brick Users
> Hi. I'd like a function similar to txtWrapWith and strWrapWith,
> but instead of just accepting Text and String, one that accepts
> Brick.Markup.Markup. From what I can tell, no such function exists.
>
> I started to look into writing one.. it might be a PITA. It looks
> like I'll have to go into the word-wrap module and rewrite all of the
> functions dealing with "Text" to handle some other type that attaches
> attributes to text, such as "[(Text, a)]". After that, I would think
> the logic for txtWrapWith would transfer over with some tweaking.

The only application I've worked on that had non-trivial formatting
requirements couldn't use either of those approaches because the
original text was in Markdown format, so that application has a process
to do line-wrapping on Markdown ASTs, format the results, and then build
Widgets from that.[1]

One option that might or might not be appropriate for your purposes
(but would save you having to hack on word-wrap) is to flip your
transformations around (paraphrasing):

1) Wrap original text with word-wrap:
let wrappedLines = doTheWrapping originalInput

2) Apply markup to each line of text:
let markupLines = doMarkup <$> wrappedLines

3) Render each line of text as its own widget, then put them in a box:
vBox $ renderMarkup <$> markupLines

If you're trying to do something more sophisticated, such as write
multi-line-aware syntax highlighting, then that won't work - but then
I'd also argue that Markup isn't a great approach for that anyway,
because there's likely to be a much more efficient custom approach that
would be better.

[1] https://github.com/matterhorn-chat/matterhorn/blob/master/src/Markdown.hs

--
Jonathan Daugherty
Reply all
Reply to author
Forward
0 new messages