on develop help sign in
11 files changed+343−329
since splitsnapshots
unifiedsplit
ChangeLog.md+17−11 src-bin/Example/CPU.hs+2−2 src-bin/example.hs+11−3 src/Data/Text/Zipper.hs+82−195 src/Reflex/Vty/Widget.hs+83−10 src/Reflex/Vty/Widget/Box.hs+19−16 src/Reflex/Vty/Widget/Input.hs+9−7 src/Reflex/Vty/Widget/Input/Text.hs+16−11 src/Reflex/Vty/Widget/Layout.hs+4 src/Reflex/Vty/Widget/Text.hs+17−10 test/Data/Text/ZipperSpec.hs+83−64
ChangeLog.md+17−11
@@ -33,6 +33,8 @@
3333 * Introduce a `FocusReader` monad transformer
3434 * Replace `HasVtyInput` with `HasInput`
3535 * Introduce an `Input` monad transformer
36+ * Introduce `HasTheme` reader class to allow setting Vty attributes of all built-in widgets
37+ * Introduce `ThemeReader` monad transformer
3638 * Remove `DynRegion` and `currentRegion`. Use `Dynamic t Region` and `current` instead. This also changes the type of `pane`'s argument.
3739 * `CheckboxConfig` now has a field taking an `Event` to set the value of the checkbox.
3840 * `checkbox` now accepts keyboard input (spacebar to check and uncheck) and is displayed in bold when focused.
@@ -54,20 +56,24 @@
5456 * `splitH`: Now requires `HasDisplayRegion t m, HasInput t m, HasImageWriter t m, HasFocusReader t m`
5557 * `splitVDrag`: Now requires `HasDisplayRegion t m, HasInput t m, HasImageWriter t m, HasFocusReader t m`
5658 * `fill`: Now requires `HasImageWriter` and `HasDisplayRegion`
57− * `boxTitle`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m`
58− * `box`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m`
59− * `boxStatic`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m`
59+ * `boxTitle`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m`
60+ * `box`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m`
61+ * `boxStatic`: Now requires `HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m`
6062 * `richText`: Now requires `HasImageWriter`, and `HasDisplayRegion`
61− * `scrollableText`: Now requires `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
63+ * `scrollableText`: Now requires `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
6264 * `blank`: Now requires `Monad`
63− * `button`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
64− * `textButton`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
65− * `textButtonStatic`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
66− * `link`: Now requires `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
65+ * `button`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
66+ * `textButton`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
67+ * `textButtonStatic`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
68+ * `link`: Now requires `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
6769 * `checkbox`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
68− * `textInput`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
69− * `multilineTextInput`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, and `HasDisplayRegion`
70− * `textInputTile`: Now requires `HasFocusReader`, `HasInput`, `HasLayout`, and `HasFocus`
70+ * TextZipper interface changes
71+ * `_displayLines_offsetMap` type changed to `OffsetMapWithAlignment`
72+ * `_displayLines_cursorY` replaced with `_displayLines_cursorPos` which include X position
73+ * some exposed methods intended for internal use only have been removed
74+ * `textInput`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
75+ * `multilineTextInput`: Now requires `HasFocusReader`, `HasInput`, `HasImageWriter`, `HasTheme`, and `HasDisplayRegion`
76+ * `textInputTile`: Now requires `HasFocusReader`, `HasInput`, `HasLayout`, `HasTheme`, and `HasFocus`
7177* _Misc_:
7278 * (#40 Add alignment support to TextZipper)[https://github.com/reflex-frp/reflex-vty/pull/40]
7379 * Add alignment (left/center/right) support to TextZipper
src-bin/Example/CPU.hs+2−2
@@ -100,7 +100,7 @@ cpuStats
100100 , HasLayout t m
101101 , HasFocus t m
102102 , HasInput t m
103− , HasFocusReader t m
103+ , HasFocusReader t m, HasTheme t m
104104 )
105105 => m ()
106106cpuStats = do
@@ -122,7 +122,7 @@ chart
122122 , HasImageWriter t m
123123 , HasInput t m
124124 , HasDisplayRegion t m
125− , HasFocusReader t m
125+ , HasFocusReader t m, HasTheme t m
126126 )
127127 => Dynamic t (Ratio Word64) -> m ()
128128chart pct = do
src-bin/example.hs+11−3
@@ -23,7 +23,7 @@ type VtyExample t m =
2323 , HasImageWriter t m
2424 , HasDisplayRegion t m
2525 , HasFocus t m
26− , HasFocusReader t m
26+ , HasFocusReader t m, HasTheme t m
2727 )
2828
2929type Manager t m =
@@ -46,12 +46,20 @@ withCtrlC f = do
4646 V.EvKey (V.KChar 'c') [V.MCtrl] -> Just ()
4747 _ -> Nothing
4848
49+darkTheme :: V.Attr
50+darkTheme = V.Attr {
51+ V.attrStyle = V.SetTo V.standout
52+ , V.attrForeColor = V.SetTo V.black
53+ , V.attrBackColor = V.Default
54+ , V.attrURL = V.Default
55+}
56+
4957main :: IO ()
5058main = mainWidget $ withCtrlC $ do
5159 initManager_ $ do
5260 tabNavigation
5361 let gf = grout . fixed
54− t = tile flex
62+ t = tile flex
5563 buttons = col $ do
5664 gf 3 $ col $ do
5765 gf 1 $ text "Select an example."
@@ -77,7 +85,7 @@ main = mainWidget $ withCtrlC $ do
7785 _ -> Nothing
7886 rec out <- networkHold buttons $ ffor (switch (current out)) $ \case
7987 Left Example_Todo -> escapable taskList
80− Left Example_TextEditor -> escapable testBoxes
88+ Left Example_TextEditor -> escapable $ localTheme (const (constant darkTheme)) testBoxes
8189 Left Example_ScrollableTextDisplay -> escapable scrolling
8290 Left Example_ClickButtonsGetEmojis -> escapable easyExample
8391 Left Example_CPUStat -> escapable cpuStats
src/Data/Text/Zipper.hs+82−195
@@ -12,7 +12,7 @@ module Data.Text.Zipper where
1212import Prelude
1313
1414import Control.Exception (assert)
15−import Control.Monad.State (evalState, forM, get, put, join)
15+import Control.Monad.State (evalState, forM, get, put)
1616import Data.Char (isSpace)
1717import Data.Map (Map)
1818import Data.Maybe (fromMaybe)
@@ -170,7 +170,7 @@ deleteLeftWord (TextZipper lb b a la) =
170170-- | Insert up to n spaces to get to the next logical column that is a multiple of n
171171tab :: Int -> TextZipper -> TextZipper
172172tab n z@(TextZipper _ b _ _) =
173− insert (T.replicate (fromEnum $ n - (T.length b `mod` max 1 n)) " ") z
173+ insert (T.replicate (fromEnum $ n - T.length b `mod` max 1 n) " ") z
174174
175175-- | The plain text contents of the zipper
176176value :: TextZipper -> Text
@@ -193,105 +193,35 @@ fromText = flip insert empty
193193data Span tag = Span tag Text
194194 deriving (Show)
195195
196+-- | Text alignment type
197+data TextAlignment =
198+ TextAlignment_Left
199+ | TextAlignment_Right
200+ | TextAlignment_Center
201+ deriving (Eq, Show)
202+
203+-- A map from the index (row) of display line to (fst,snd)
204+-- fst: leading empty spaces from left (may be negative) to adjust for alignment
205+-- snd: the text offset from the beginning of the document
206+-- to the first character of the display line
207+type OffsetMapWithAlignment = Map Int (Int, Int)
208+
209+-- helper type representing a single visual line that may be part of a wrapped logical line
210+data WrappedLine = WrappedLine
211+ { _wrappedLines_text :: Text
212+ , _wrappedLines_hiddenWhitespace :: Bool -- ^ 'True' if this line ends with a deleted whitespace character
213+ , _wrappedLines_offset :: Int -- ^ offset from beginning of line
214+ }
215+ deriving (Eq, Show)
216+
196217-- | Information about the document as it is displayed (i.e., post-wrapping)
197218data DisplayLines tag = DisplayLines
198219 { _displayLines_spans :: [[Span tag]]
199− , _displayLines_offsetMap :: Map Int Int
200− , _displayLines_cursorY :: Int
220+ , _displayLines_offsetMap :: OffsetMapWithAlignment
221+ , _displayLines_cursorPos :: (Int, Int) -- cursor position relative to upper left hand corner
201222 }
202223 deriving (Show)
203224
204−-- | Given a width and a 'TextZipper', produce a list of display lines
205−-- (i.e., lines of wrapped text) with special attributes applied to
206−-- certain segments (e.g., the cursor). Additionally, produce the current
207−-- y-coordinate of the cursor and a mapping from display line number to text
208−-- offset
209−displayLines
210− :: Int -- ^ Width, used for wrapping
211− -> tag -- ^ Metadata for normal characters
212− -> tag -- ^ Metadata for the cursor
213− -> TextZipper -- ^ The text input contents and cursor state
214− -> DisplayLines tag
215−displayLines width tag cursorTag (TextZipper lb b a la) =
216− let linesBefore :: [[Text]] -- The wrapped lines before the cursor line
217− linesBefore = map (wrapWithOffset width 0) $ reverse lb
218− linesAfter :: [[Text]] -- The wrapped lines after the cursor line
219− linesAfter = map (wrapWithOffset width 0) la
220− offsets :: Map Int Int
221− offsets = offsetMap $ mconcat
222− [ linesBefore
223− , [wrapWithOffset width 0 $ b <> a]
224− , linesAfter
225− ]
226− spansBefore = map ((:[]) . Span tag) $ concat linesBefore
227− spansAfter = map ((:[]) . Span tag) $ concat linesAfter
228− -- Separate the spans before the cursor into
229− -- * spans that are on earlier display lines (though on the same logical line), and
230− -- * spans that are on the same display line
231− (spansCurrentBefore, spansCurLineBefore) = fromMaybe ([], []) $
232− initLast $ map ((:[]) . Span tag) (wrapWithOffset width 0 b)
233− -- Calculate the number of columns on the cursor's display line before the cursor
234− curLineOffset = spansWidth spansCurLineBefore
235− -- Check whether the spans on the current display line are long enough that
236− -- the cursor has to go to the next line
237− cursorAfterEOL = curLineOffset == width
238− cursorCharWidth = case T.uncons a of
239− Nothing -> 1
240− Just (c, _) -> charWidth c
241− -- Separate the span after the cursor into
242− -- * spans that are on the same display line, and
243− -- * spans that are on later display lines (though on the same logical line)
244− (spansCurLineAfter, spansCurrentAfter) = fromMaybe ([], []) $
245− headTail $ case T.uncons a of
246− Nothing -> [[Span cursorTag " "]]
247− Just (c, rest) ->
248− let o = if cursorAfterEOL then cursorCharWidth else curLineOffset + cursorCharWidth
249− cursor = Span cursorTag (T.singleton c)
250− in case map ((:[]) . Span tag) (wrapWithOffset width o rest) of
251− [] -> [[cursor]]
252− (l:ls) -> (cursor : l) : ls
253− in DisplayLines
254− { _displayLines_spans = concat
255− [ spansBefore
256− , spansCurrentBefore
257− , if cursorAfterEOL
258− then [ spansCurLineBefore, spansCurLineAfter ]
259− else [ spansCurLineBefore <> spansCurLineAfter ]
260− , spansCurrentAfter
261− , spansAfter
262− ]
263− , _displayLines_offsetMap = offsets
264− , _displayLines_cursorY = sum
265− [ length spansBefore
266− , length spansCurrentBefore
267− , if cursorAfterEOL then 1 else 0
268− ]
269− }
270− where
271− initLast :: [a] -> Maybe ([a], a)
272− initLast = \case
273− [] -> Nothing
274− (x:xs) -> case initLast xs of
275− Nothing -> Just ([], x)
276− Just (ys, y) -> Just (x:ys, y)
277− headTail :: [a] -> Maybe (a, [a])
278− headTail = \case
279− [] -> Nothing
280− x:xs -> Just (x, xs)
281−
282−-- | Wraps a logical line of text to fit within the given width. The first
283−-- wrapped line is offset by the number of columns provided. Subsequent wrapped
284−-- lines are not.
285−wrapWithOffset
286− :: Int -- ^ Maximum width
287− -> Int -- ^ Offset for first line
288− -> Text -- ^ Text to be wrapped
289− -> [Text]
290−wrapWithOffset maxWidth _ _ | maxWidth <= 0 = []
291−wrapWithOffset maxWidth n xs =
292− let (firstLine, rest) = splitAtWidth (maxWidth - n) xs
293− in firstLine : (fmap (takeWidth maxWidth) . takeWhile (not . T.null) . iterate (dropWidth maxWidth) $ rest)
294−
295225-- | Split a 'Text' at the given column index. For example
296226--
297227-- > splitAtWidth 3 "ᄀabc" == ("ᄀa", "bc")
@@ -339,56 +269,6 @@ dropWidth n = snd . splitAtWidth n
339269charWidth :: Char -> Int
340270charWidth = wcwidth
341271
342−
343−-- | For a given set of wrapped logical lines, computes a map
344−-- from display line index to text offset in the original text.
345−-- This is used to help determine how interactions with the displayed
346−-- text map back to the original text.
347−-- For example, given the document @\"AA\\nBBB\\nCCCCCCCC\\n\"@ wrapped to 5 columns,
348−-- this function will compute the offset in the original document of each character
349−-- in column 1:
350−--
351−-- > AA... (0, 0)
352−-- > BBB.. (1, 3)
353−-- > CCCCC (2, 7) -- (this line wraps to the next row)
354−-- > CCC.. (3, 12)
355−-- > ..... (4, 16)
356−offsetMap
357− :: [[Text]] -- ^ The outer list represents logical lines, and the
358− -- inner list represents the display lines into which
359− -- the logical line has been wrapped
360− -> Map Int Int -- ^ A map from the index (row) of display line to
361− -- the text offset from the beginning of the document
362− -- to the first character of the display line
363−offsetMap ts = evalState (offsetMap' ts) (0, 0)
364− where
365− offsetMap' xs = fmap Map.unions $ forM xs $ \x -> do
366− maps <- forM x $ \line -> do
367− let l = T.length line
368− (dl, o) <- get
369− put (dl + 1, o + l)
370− return $ Map.singleton dl o
371− (dl, o) <- get
372− put (dl, o + 1)
373− return $ Map.insert dl (o + 1) $ Map.unions maps
374−
375−-- | Move the cursor of the given 'TextZipper' to the logical position indicated
376−-- by the given display line coordinates, using the provided 'DisplayLines'
377−-- information. If the x coordinate is beyond the end of a line, the cursor is
378−-- moved to the end of the line.
379−goToDisplayLinePosition :: Int -> Int -> DisplayLines tag -> TextZipper -> TextZipper
380−goToDisplayLinePosition x y dl tz =
381− let offset = Map.lookup y $ _displayLines_offsetMap dl
382− in case offset of
383− Nothing -> tz
384− Just o ->
385− let
386− moveRight = case drop y $ _displayLines_spans dl of
387− [] -> x
388− (s:_) -> charIndexAt x . stream . mconcat . fmap (\(Span _ t) -> t) $ s
389− in rightN (o + min moveRight x) $ top tz
390−
391−
392272-- | Get the width of the text in a set of 'Span's, taking into account unicode character widths
393273spansWidth :: [Span tag] -> Int
394274spansWidth = sum . map (\(Span _ t) -> textWidth t)
@@ -434,9 +314,7 @@ wordsWithWhitespace :: Text -> [Text]
434314wordsWithWhitespace t@(Text arr off len) = loop 0 0 False
435315 where
436316 loop !start !n !wasSpace
437− | n >= len = if start == n
438− then []
439− else [Text arr (start+off) (n-start)]
317+ | n >= len = [Text arr (start+off) (n-start) | not (start == n)]
440318 | isSpace c = loop start (n+d) True
441319 | wasSpace = Text arr (start+off) (n-start) : loop n n False
442320 | otherwise = loop start (n+d) False
@@ -474,33 +352,6 @@ splitWordsAtDisplayWidth maxWidth wwws = reverse $ loop wwws 0 [] where
474352 else loop (x:xs) 0 [] <> modifyOutForNewLine out
475353 else loop xs newWidth $ appendOut out x False
476354
477−data TextAlignment =
478− TextAlignment_Left
479− | TextAlignment_Right
480− | TextAlignment_Center
481− deriving (Eq, Show)
482−
483−-- A map from the index (row) of display line to
484−-- fst: leading empty spaces from left (may be negative) to adjust for alignment
485−-- snd: the text offset from the beginning of the document
486−-- to the first character of the display line
487−type OffsetMapWithAlignment = Map Int (Int, Int)
488−
489−-- | Information about the document as it is displayed (i.e., post-wrapping)
490−data DisplayLinesWithAlignment tag = DisplayLinesWithAlignment
491− { _displayLinesWithAlignment_spans :: [[Span tag]]
492− , _displayLinesWithAlignment_offsetMap :: OffsetMapWithAlignment
493− -- cursor position relative to upper left hand corner
494− , _displayLinesWithAlignment_cursorPos :: (Int, Int) -- cursor position relative to upper left hand corner
495− }
496− deriving (Show)
497−
498−data WrappedLine = WrappedLine
499− { _wrappedLines_text :: Text
500− , _wrappedLines_hiddenWhitespace :: Bool -- ^ 'True' if this line ends with a deleted whitespace character
501− , _wrappedLines_offset :: Int -- ^ offset from beginning of line
502− }
503− deriving (Eq, Show)
504355
505356
506357-- | Wraps a logical line of text to fit within the given width. The first
@@ -514,7 +365,8 @@ wrapWithOffsetAndAlignment
514365 -> [WrappedLine] -- (words on that line, hidden space char, offset from beginning of line)
515366wrapWithOffsetAndAlignment _ maxWidth _ _ | maxWidth <= 0 = []
516367wrapWithOffsetAndAlignment alignment maxWidth n txt = assert (n <= maxWidth) r where
517− r' = splitWordsAtDisplayWidth maxWidth $ T.replicate n " " : wordsWithWhitespace txt
368+ -- we pad by offset amount with any non-space character which we will remove later so that no changes need to be made to splitWordsAtDisplayWidth
369+ r' = splitWordsAtDisplayWidth maxWidth $ wordsWithWhitespace ( T.replicate n "." <> txt)
518370 fmapfn (t,b) = case alignment of
519371 TextAlignment_Left -> WrappedLine t b 0
520372 TextAlignment_Right -> WrappedLine t b (maxWidth-l)
@@ -527,7 +379,7 @@ wrapWithOffsetAndAlignment alignment maxWidth n txt = assert (n <= maxWidth) r w
527379
528380-- converts deleted eol spaces into logical lines
529381eolSpacesToLogicalLines :: [[WrappedLine]] -> [[(Text, Int)]]
530−eolSpacesToLogicalLines = fmap (fmap (\(WrappedLine a _ c) -> (a,c))) . join . fmap (L.groupBy (\(WrappedLine _ b _) _ -> not b))
382+eolSpacesToLogicalLines = fmap (fmap (\(WrappedLine a _ c) -> (a,c))) . concatMap (L.groupBy (\(WrappedLine _ b _) _ -> not b))
531383
532384offsetMapWithAlignmentInternal :: [[WrappedLine]] -> OffsetMapWithAlignment
533385offsetMapWithAlignmentInternal = offsetMapWithAlignment . eolSpacesToLogicalLines
@@ -555,21 +407,24 @@ offsetMapWithAlignment ts = evalState (offsetMap' ts) (0, 0)
555407-- y-coordinate of the cursor and a mapping from display line number to text
556408-- offset
557409displayLinesWithAlignment
558− :: (Show tag) => TextAlignment
410+ :: TextAlignment
559411 -> Int -- ^ Width, used for wrapping
560412 -> tag -- ^ Metadata for normal characters
561413 -> tag -- ^ Metadata for the cursor
562414 -> TextZipper -- ^ The text input contents and cursor state
563− -> DisplayLinesWithAlignment tag
415+ -> DisplayLines tag
564416displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
565417 let linesBefore :: [[WrappedLine]] -- The wrapped lines before the cursor line
566418 linesBefore = map (wrapWithOffsetAndAlignment alignment width 0) $ reverse lb
567419 linesAfter :: [[WrappedLine]] -- The wrapped lines after the cursor line
568420 linesAfter = map (wrapWithOffsetAndAlignment alignment width 0) la
421+
422+ -- simulate trailing cursor character when computing OffsetMap
423+ afterWithCursor = if T.null a then " " else a
569424 offsets :: OffsetMapWithAlignment
570425 offsets = offsetMapWithAlignmentInternal $ mconcat
571426 [ linesBefore
572− , [wrapWithOffsetAndAlignment alignment width 0 $ b <> a]
427+ , [wrapWithOffsetAndAlignment alignment width 0 $ b <> afterWithCursor]
573428 , linesAfter
574429 ]
575430 flattenLines = concatMap (fmap _wrappedLines_text)
@@ -579,9 +434,8 @@ displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
579434 -- * spans that are on earlier display lines (though on the same logical line), and
580435 -- * spans that are on the same display line
581436
582− -- TODO cursor goes here if align right
583437 (spansCurrentBefore, spansCurLineBefore) = fromMaybe ([], []) $
584− initLast $ map ((:[]) . Span tag) $ fmap _wrappedLines_text $ (wrapWithOffsetAndAlignment alignment width 0 b)
438+ initLast $ map ((:[]) . Span tag) $ _wrappedLines_text <$> (wrapWithOffsetAndAlignment alignment width 0 b)
585439 -- Calculate the number of columns on the cursor's display line before the cursor
586440 curLineOffset = spansWidth spansCurLineBefore
587441 -- Check whether the spans on the current display line are long enough that
@@ -595,17 +449,29 @@ displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
595449 -- * spans that are on the same display line, and
596450 -- * spans that are on later display lines (though on the same logical line)
597451
598− -- TODO do not show cursor if align right
599452 (spansCurLineAfter, spansCurrentAfter) = fromMaybe ([], []) $
600453 headTail $ case T.uncons a of
601454 Nothing -> [[Span cursorTag " "]]
602455 Just (c, rest) ->
603456 let o = if cursorAfterEOL then cursorCharWidth else curLineOffset + cursorCharWidth
604457 cursor = Span cursorTag (T.singleton c)
605− in case map ((:[]) . Span tag) $ fmap _wrappedLines_text $ (wrapWithOffsetAndAlignment alignment width o rest) of
458+ in case map ((:[]) . Span tag) $ _wrappedLines_text <$> (wrapWithOffsetAndAlignment alignment width o rest) of
606459 [] -> [[cursor]]
607460 (l:ls) -> (cursor : l) : ls
608461
462+ curLineSpanNormalCase = if cursorAfterEOL
463+ then [ spansCurLineBefore, spansCurLineAfter ]
464+ else [ spansCurLineBefore <> spansCurLineAfter ]
465+
466+ -- for right alignment, we want draw the cursor tag to be on the character just before the logical cursor position
467+ curLineSpan = if alignment == TextAlignment_Right && not cursorAfterEOL
468+ then case reverse spansCurLineBefore of
469+ [] -> curLineSpanNormalCase
470+ (Span _ x):xs -> case spansCurLineAfter of
471+ [] -> error "should not be possible" -- curLineSpanNormalCase
472+ (Span _ y):ys -> [reverse (Span cursorTag x:xs) <> ((Span tag y):ys)]
473+ else curLineSpanNormalCase
474+
609475 cursorY = sum
610476 [ length spansBefore
611477 , length spansCurrentBefore
@@ -614,18 +480,16 @@ displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
614480 -- a little silly to convert back to text but whatever, it works
615481 cursorX = if cursorAfterEOL then 0 else textWidth (mconcat $ fmap (\(Span _ t) -> t) spansCurLineBefore)
616482
617− in DisplayLinesWithAlignment
618− { _displayLinesWithAlignment_spans = concat
483+ in DisplayLines
484+ { _displayLines_spans = concat
619485 [ spansBefore
620486 , spansCurrentBefore
621− , if cursorAfterEOL
622− then [ spansCurLineBefore, spansCurLineAfter ]
623− else [ spansCurLineBefore <> spansCurLineAfter ]
487+ , curLineSpan
624488 , spansCurrentAfter
625489 , spansAfter
626490 ]
627− , _displayLinesWithAlignment_offsetMap = offsets
628− , _displayLinesWithAlignment_cursorPos = (cursorX, cursorY)
491+ , _displayLines_offsetMap = offsets
492+ , _displayLines_cursorPos = (cursorX, cursorY)
629493 }
630494 where
631495 initLast :: [a] -> Maybe ([a], a)
@@ -644,15 +508,38 @@ displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
644508-- by the given display line coordinates, using the provided 'DisplayLinesWithAlignment'
645509-- information. If the x coordinate is beyond the end of a line, the cursor is
646510-- moved to the end of the line.
647−goToDisplayLineWithAlignmentPosition :: Int -> Int -> DisplayLinesWithAlignment tag -> TextZipper -> TextZipper
648−goToDisplayLineWithAlignmentPosition x y dl tz =
649− let offset = Map.lookup y $ _displayLinesWithAlignment_offsetMap dl
511+goToDisplayLinePosition :: Int -> Int -> DisplayLines tag -> TextZipper -> TextZipper
512+goToDisplayLinePosition x y dl tz =
513+ let offset = Map.lookup y $ _displayLines_offsetMap dl
650514 in case offset of
651515 Nothing -> tz
652516 Just (alignOff,o) ->
653517 let
654518 trueX = max 0 (x - alignOff)
655− moveRight = case drop y $ _displayLinesWithAlignment_spans dl of
519+ moveRight = case drop y $ _displayLines_spans dl of
656520 [] -> 0
657521 (s:_) -> charIndexAt trueX . stream . mconcat . fmap (\(Span _ t) -> t) $ s
658522 in rightN (o + moveRight) $ top tz
523+
524+-- | Given a width and a 'TextZipper', produce a list of display lines
525+-- (i.e., lines of wrapped text) with special attributes applied to
526+-- certain segments (e.g., the cursor). Additionally, produce the current
527+-- y-coordinate of the cursor and a mapping from display line number to text
528+-- offset
529+displayLines
530+ :: Int -- ^ Width, used for wrapping
531+ -> tag -- ^ Metadata for normal characters
532+ -> tag -- ^ Metadata for the cursor
533+ -> TextZipper -- ^ The text input contents and cursor state
534+ -> DisplayLines tag
535+displayLines = displayLinesWithAlignment TextAlignment_Left
536+
537+-- | Wraps a logical line of text to fit within the given width. The first
538+-- wrapped line is offset by the number of columns provided. Subsequent wrapped
539+-- lines are not.
540+wrapWithOffset
541+ :: Int -- ^ Maximum width
542+ -> Int -- ^ Offset for first line
543+ -> Text -- ^ Text to be wrapped
544+ -> [Text]
545+wrapWithOffset maxWidth n xs = _wrappedLines_text <$> wrapWithOffsetAndAlignment TextAlignment_Left maxWidth n xs
src/Reflex/Vty/Widget.hs+83−10
@@ -35,6 +35,7 @@ mainWidgetWithHandle
3535 , HasDisplayRegion t m
3636 , HasFocusReader t m
3737 , HasInput t m
38+ , HasTheme t m
3839 ) => m (Event t ()))
3940 -> IO ()
4041mainWidgetWithHandle vty child =
@@ -45,12 +46,14 @@ mainWidgetWithHandle vty child =
4546 let inp' = fforMaybe inp $ \case
4647 V.EvResize {} -> Nothing
4748 x -> Just x
48− (shutdown, images) <- runFocusReader (pure True) $ runDisplayRegion (fmap (\(w, h) -> Region 0 0 w h) size) $
49− runImageWriter $
50− runNodeIdT $
51− runInput inp' $ do
52− tellImages . ffor (current size) $ \(w, h) -> [V.charFill V.defAttr ' ' w h]
53− child
49+ (shutdown, images) <- runThemeReader (constant V.defAttr) $
50+ runFocusReader (pure True) $
51+ runDisplayRegion (fmap (\(w, h) -> Region 0 0 w h) size) $
52+ runImageWriter $
53+ runNodeIdT $
54+ runInput inp' $ do
55+ tellImages . ffor (current size) $ \(w, h) -> [V.charFill V.defAttr ' ' w h]
56+ child
5457 return $ VtyResult
5558 { _vtyResult_picture = fmap (V.picForLayers . reverse) images
5659 , _vtyResult_shutdown = shutdown
@@ -69,6 +72,7 @@ mainWidget
6972 , MonadNodeId m
7073 , HasDisplayRegion t m
7174 , HasFocusReader t m
75+ , HasTheme t m
7276 , HasInput t m
7377 ) => m (Event t ()))
7478 -> IO ()
@@ -233,9 +237,9 @@ regionSize :: Region -> (Int, Int)
233237regionSize (Region _ _ w h) = (w, h)
234238
235239-- | Produces an 'Image' that fills a region with space characters
236−regionBlankImage :: Region -> Image
237−regionBlankImage r@(Region _ _ width height) =
238− withinImage r $ V.charFill V.defAttr ' ' width height
240+regionBlankImage :: V.Attr -> Region -> Image
241+regionBlankImage attr r@(Region _ _ width height) =
242+ withinImage r $ V.charFill attr ' ' width height
239243
240244-- | A class for things that know their own display size dimensions
241245class (Reflex t, Monad m) => HasDisplayRegion t m | m -> t where
@@ -403,7 +407,7 @@ newtype ImageWriter t m a = ImageWriter
403407 , NotReady t
404408 , PerformEvent t
405409 , PostBuild t
406− , TriggerEvent t
410+ , TriggerEvent t
407411 )
408412
409413instance MonadTrans (ImageWriter t) where
@@ -441,6 +445,75 @@ runImageWriter
441445 -> m (a, Behavior t [Image])
442446runImageWriter = runBehaviorWriterT . unImageWriter
443447
448+-- * Theming
449+
450+-- | A class for things that can be visually styled
451+class (Reflex t, Monad m) => HasTheme t m | m -> t where
452+ theme :: m (Behavior t V.Attr)
453+ default theme :: (f m' ~ m, Monad m', MonadTrans f, HasTheme t m') => m (Behavior t V.Attr)
454+ theme = lift theme
455+ localTheme :: (Behavior t V.Attr -> Behavior t V.Attr) -> m a -> m a
456+ default localTheme :: (f m' ~ m, Monad m', MFunctor f, HasTheme t m') => (Behavior t V.Attr -> Behavior t V.Attr) -> m a -> m a
457+ localTheme f = hoist (localTheme f)
458+
459+instance HasTheme t m => HasTheme t (ReaderT x m)
460+instance HasTheme t m => HasTheme t (BehaviorWriterT t x m)
461+instance HasTheme t m => HasTheme t (DynamicWriterT t x m)
462+instance HasTheme t m => HasTheme t (EventWriterT t x m)
463+instance HasTheme t m => HasTheme t (NodeIdT m)
464+instance HasTheme t m => HasTheme t (Input t m)
465+instance HasTheme t m => HasTheme t (ImageWriter t m)
466+instance HasTheme t m => HasTheme t (DisplayRegion t m)
467+instance HasTheme t m => HasTheme t (FocusReader t m)
468+
469+-- | A widget that has access to theme information
470+newtype ThemeReader t m a = ThemeReader
471+ { unThemeReader :: ReaderT (Behavior t V.Attr) m a }
472+ deriving
473+ ( Functor
474+ , Applicative
475+ , Monad
476+ , MonadFix
477+ , MonadHold t
478+ , MonadIO
479+ , MonadRef
480+ , MonadSample t
481+ )
482+
483+instance (Monad m, Reflex t) => HasTheme t (ThemeReader t m) where
484+ theme = ThemeReader ask
485+ localTheme f = ThemeReader . local f . unThemeReader
486+
487+deriving instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ThemeReader t m)
488+deriving instance NotReady t m => NotReady t (ThemeReader t m)
489+deriving instance PerformEvent t m => PerformEvent t (ThemeReader t m)
490+deriving instance PostBuild t m => PostBuild t (ThemeReader t m)
491+deriving instance TriggerEvent t m => TriggerEvent t (ThemeReader t m)
492+instance HasImageWriter t m => HasImageWriter t (ThemeReader t m)
493+
494+instance (Adjustable t m, MonadFix m, MonadHold t m) => Adjustable t (ThemeReader t m) where
495+ runWithReplace (ThemeReader a) e = ThemeReader $ runWithReplace a $ fmap unThemeReader e
496+ traverseIntMapWithKeyWithAdjust f m e = ThemeReader $ traverseIntMapWithKeyWithAdjust (\k v -> unThemeReader $ f k v) m e
497+ traverseDMapWithKeyWithAdjust f m e = ThemeReader $ traverseDMapWithKeyWithAdjust (\k v -> unThemeReader $ f k v) m e
498+ traverseDMapWithKeyWithAdjustWithMove f m e = ThemeReader $ traverseDMapWithKeyWithAdjustWithMove (\k v -> unThemeReader $ f k v) m e
499+
500+instance MonadTrans (ThemeReader t) where
501+ lift = ThemeReader . lift
502+
503+instance MFunctor (ThemeReader t) where
504+ hoist f = ThemeReader . hoist f . unThemeReader
505+
506+instance MonadNodeId m => MonadNodeId (ThemeReader t m)
507+
508+-- | Run a 'ThemeReader' action with the given focus value
509+runThemeReader
510+ :: (Reflex t, Monad m)
511+ => Behavior t V.Attr
512+ -> ThemeReader t m a
513+ -> m a
514+runThemeReader b = flip runReaderT b . unThemeReader
515+
516+
444517-- ** Manipulating images
445518
446519-- | Translates and crops an 'Image' so that it is contained by
src/Reflex/Vty/Widget/Box.hs+19−16
@@ -13,7 +13,7 @@ import Reflex.Vty.Widget
1313import Reflex.Vty.Widget.Text
1414
1515-- | Fill the background with the bottom box style
16−hRule :: (HasDisplayRegion t m, HasImageWriter t m) => BoxStyle -> m ()
16+hRule :: (HasDisplayRegion t m, HasImageWriter t m, HasTheme t m) => BoxStyle -> m ()
1717hRule boxStyle = fill $ pure (_boxStyle_s boxStyle)
1818
1919-- | Defines a set of symbols to use to draw the outlines of boxes
@@ -54,7 +54,7 @@ roundedBoxStyle :: BoxStyle
5454roundedBoxStyle = BoxStyle '╭' '─' '╮' '│' '╯' '─' '╰' '│'
5555
5656-- | Draws a titled box in the provided style and a child widget inside of that box
57−boxTitle :: (Monad m, Reflex t ,HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m)
57+boxTitle :: (Monad m, Reflex t ,HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m)
5858 => Behavior t BoxStyle
5959 -> Behavior t Text
6060 -> m a
@@ -62,36 +62,39 @@ boxTitle :: (Monad m, Reflex t ,HasDisplayRegion t m, HasImageWriter t m, HasInp
6262boxTitle boxStyle title child = do
6363 dh <- displayHeight
6464 dw <- displayWidth
65+ bt <- theme
6566 let boxReg = Region 0 0 <$> dw <*> dh
6667 innerReg = Region 1 1 <$> (subtract 2 <$> dw) <*> (subtract 2 <$> dh)
67− tellImages (boxImages <$> title <*> boxStyle <*> current boxReg)
68− tellImages (fmap (\r -> [regionBlankImage r]) (current innerReg))
68+
69+ tellImages (boxImages <$> bt <*> title <*> boxStyle <*> current boxReg)
70+ tellImages (ffor2 (current innerReg) bt (\r attr -> [regionBlankImage attr r]))
71+
6972 pane innerReg (pure True) child
7073 where
71− boxImages :: Text -> BoxStyle -> Region -> [Image]
72− boxImages title' style (Region left top width height) =
74+ boxImages :: V.Attr -> Text -> BoxStyle -> Region -> [Image]
75+ boxImages attr title' style (Region left top width height) =
7376 let right = left + width - 1
7477 bottom = top + height - 1
7578 sides =
7679 [ withinImage (Region (left + 1) top (width - 2) 1) $
77− V.text' V.defAttr $
80+ V.text' attr $
7881 hPadText title' (_boxStyle_n style) (width - 2)
7982 , withinImage (Region right (top + 1) 1 (height - 2)) $
80− V.charFill V.defAttr (_boxStyle_e style) 1 (height - 2)
83+ V.charFill attr (_boxStyle_e style) 1 (height - 2)
8184 , withinImage (Region (left + 1) bottom (width - 2) 1) $
82− V.charFill V.defAttr (_boxStyle_s style) (width - 2) 1
85+ V.charFill attr (_boxStyle_s style) (width - 2) 1
8386 , withinImage (Region left (top + 1) 1 (height - 2)) $
84− V.charFill V.defAttr (_boxStyle_w style) 1 (height - 2)
87+ V.charFill attr (_boxStyle_w style) 1 (height - 2)
8588 ]
8689 corners =
8790 [ withinImage (Region left top 1 1) $
88− V.char V.defAttr (_boxStyle_nw style)
91+ V.char attr (_boxStyle_nw style)
8992 , withinImage (Region right top 1 1) $
90− V.char V.defAttr (_boxStyle_ne style)
93+ V.char attr (_boxStyle_ne style)
9194 , withinImage (Region right bottom 1 1) $
92− V.char V.defAttr (_boxStyle_se style)
95+ V.char attr (_boxStyle_se style)
9396 , withinImage (Region left bottom 1 1) $
94− V.char V.defAttr (_boxStyle_sw style)
97+ V.char attr (_boxStyle_sw style)
9598 ]
9699 in sides ++ if width > 1 && height > 1 then corners else []
97100 hPadText :: T.Text -> Char -> Int -> T.Text
@@ -106,7 +109,7 @@ boxTitle boxStyle title child = do
106109 right = mkHalf delta
107110
108111-- | A box without a title
109−box :: (Monad m, Reflex t, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m)
112+box :: (Monad m, Reflex t, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m)
110113 => Behavior t BoxStyle
111114 -> m a
112115 -> m a
@@ -114,7 +117,7 @@ box boxStyle = boxTitle boxStyle mempty
114117
115118-- | A box whose style is static
116119boxStatic
117− :: (Monad m, Reflex t, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m)
120+ :: (Monad m, Reflex t, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m)
118121 => BoxStyle
119122 -> m a
120123 -> m a
src/Reflex/Vty/Widget/Input.hs+9−7
@@ -34,7 +34,7 @@ instance Reflex t => Default (ButtonConfig t) where
3434
3535-- | A button widget that contains a sub-widget
3636button
37− :: (Reflex t, Monad m, HasFocusReader t m, HasDisplayRegion t m, HasImageWriter t m, HasInput t m)
37+ :: (Reflex t, Monad m, HasFocusReader t m, HasTheme t m, HasDisplayRegion t m, HasImageWriter t m, HasInput t m)
3838 => ButtonConfig t
3939 -> m ()
4040 -> m (Event t ())
@@ -52,7 +52,7 @@ button cfg child = do
5252
5353-- | A button widget that displays text that can change
5454textButton
55− :: (Reflex t, Monad m, HasDisplayRegion t m, HasFocusReader t m, HasImageWriter t m, HasInput t m)
55+ :: (Reflex t, Monad m, HasDisplayRegion t m, HasFocusReader t m, HasTheme t m, HasImageWriter t m, HasInput t m)
5656 => ButtonConfig t
5757 -> Behavior t Text
5858 -> m (Event t ())
@@ -60,7 +60,7 @@ textButton cfg = button cfg . text -- TODO Centering etc.
6060
6161-- | A button widget that displays a static bit of text
6262textButtonStatic
63− :: (Reflex t, Monad m, HasDisplayRegion t m, HasFocusReader t m, HasImageWriter t m, HasInput t m)
63+ :: (Reflex t, Monad m, HasDisplayRegion t m, HasFocusReader t m, HasTheme t m, HasImageWriter t m, HasInput t m)
6464 => ButtonConfig t
6565 -> Text
6666 -> m (Event t ())
@@ -70,19 +70,20 @@ textButtonStatic cfg = textButton cfg . pure
7070
7171-- | A clickable link widget
7272link
73− :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m, HasInput t m)
73+ :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasTheme t m)
7474 => Behavior t Text
7575 -> m (Event t MouseUp)
7676link t = do
77+ bt <- theme
7778 let cfg = RichTextConfig
78− { _richTextConfig_attributes = pure $ V.withStyle V.defAttr V.underline
79+ { _richTextConfig_attributes = fmap (\attr -> V.withStyle attr V.underline) bt
7980 }
8081 richText cfg t
8182 mouseUp
8283
8384-- | A clickable link widget with a static label
8485linkStatic
85− :: (Reflex t, Monad m, HasImageWriter t m, HasDisplayRegion t m, HasInput t m)
86+ :: (Reflex t, Monad m, HasImageWriter t m, HasDisplayRegion t m, HasInput t m, HasTheme t m)
8687 => Text
8788 -> m (Event t MouseUp)
8889linkStatic = link . pure
@@ -115,6 +116,7 @@ checkboxStyleTick = CheckboxStyle
115116-- | Configuration options for a checkbox
116117data CheckboxConfig t = CheckboxConfig
117118 { _checkboxConfig_checkboxStyle :: Behavior t CheckboxStyle
119+ -- TODO DELETE and use HasTheme instead
118120 , _checkboxConfig_attributes :: Behavior t V.Attr
119121 , _checkboxConfig_setValue :: Event t Bool
120122 }
@@ -128,7 +130,7 @@ instance (Reflex t) => Default (CheckboxConfig t) where
128130
129131-- | A checkbox widget
130132checkbox
131− :: (MonadHold t m, MonadFix m, Reflex t, HasInput t m, HasDisplayRegion t m, HasImageWriter t m, HasFocusReader t m)
133+ :: (MonadHold t m, MonadFix m, Reflex t, HasInput t m, HasDisplayRegion t m, HasImageWriter t m, HasFocusReader t m, HasTheme t m)
132134 => CheckboxConfig t
133135 -> Bool
134136 -> m (Dynamic t Bool)
src/Reflex/Vty/Widget/Input/Text.hs+16−11
@@ -43,7 +43,7 @@ data TextInput t = TextInput
4343
4444-- | A widget that allows text input
4545textInput
46− :: (Reflex t, MonadHold t m, MonadFix m, HasInput t m, HasFocusReader t m, HasDisplayRegion t m, HasImageWriter t m, HasDisplayRegion t m)
46+ :: (Reflex t, MonadHold t m, MonadFix m, HasInput t m, HasFocusReader t m, HasTheme t m, HasDisplayRegion t m, HasImageWriter t m, HasDisplayRegion t m)
4747 => TextInputConfig t
4848 -> m (TextInput t)
4949textInput cfg = do
@@ -51,6 +51,8 @@ textInput cfg = do
5151 f <- focus
5252 dh <- displayHeight
5353 dw <- displayWidth
54+ bt <- theme
55+ attr0 <- sample bt
5456 rec v <- foldDyn ($) (_textInputConfig_initialValue cfg) $ mergeWith (.)
5557 [ uncurry (updateTextZipper (_textInputConfig_tabWidth cfg)) <$> attach (current dh) i
5658 , _textInputConfig_modify cfg
@@ -59,13 +61,20 @@ textInput cfg = do
5961 goToDisplayLinePosition mx (st + my) dl
6062 ]
6163 click <- mouseDown V.BLeft
62− let cursorAttrs = ffor f $ \x -> if x then cursorAttributes else V.defAttr
63− let rows = (\w s c -> displayLines w V.defAttr c s)
64+
65+ -- TODO reverseVideo is prob not what we want. Does not work with `darkTheme` in example.hs (cursor is dark rather than light bg)
66+ let toCursorAttrs attr = V.withStyle attr V.reverseVideo
67+ rowInputDyn = (,,)
6468 <$> dw
6569 <*> (mapZipper <$> _textInputConfig_display cfg <*> v)
66− <*> cursorAttrs
70+ <*> f
71+ toDisplayLines attr (w, s, x) =
72+ let c = if x then toCursorAttrs attr else attr
73+ in displayLines w attr c s
74+ attrDyn <- holdDyn attr0 $ pushAlways (\_ -> sample bt) (updated rowInputDyn)
75+ let rows = ffor2 attrDyn rowInputDyn toDisplayLines
6776 img = images . _displayLines_spans <$> rows
68− y <- holdUniqDyn $ _displayLines_cursorY <$> rows
77+ y <- holdUniqDyn $ fmap snd _displayLines_cursorPos <$> rows
6978 let newScrollTop :: Int -> (Int, Int) -> Int
7079 newScrollTop st (h, cursorY)
7180 | cursorY < st = cursorY
@@ -81,7 +90,7 @@ textInput cfg = do
8190
8291-- | A widget that allows multiline text input
8392multilineTextInput
84− :: (Reflex t, MonadHold t m, MonadFix m, HasInput t m, HasFocusReader t m, HasDisplayRegion t m, HasImageWriter t m)
93+ :: (Reflex t, MonadHold t m, MonadFix m, HasInput t m, HasFocusReader t m, HasTheme t m, HasDisplayRegion t m, HasImageWriter t m)
8594 => TextInputConfig t
8695 -> m (TextInput t)
8796multilineTextInput cfg = do
@@ -99,7 +108,7 @@ multilineTextInput cfg = do
99108-- the computed line count to greedily size the tile when vertically
100109-- oriented, and uses the fallback width when horizontally oriented.
101110textInputTile
102− :: (Monad m, Reflex t, MonadFix m, HasLayout t m, HasInput t m, HasFocus t m, HasImageWriter t m, HasDisplayRegion t m, HasFocusReader t m)
111+ :: (Monad m, Reflex t, MonadFix m, HasLayout t m, HasInput t m, HasFocus t m, HasImageWriter t m, HasDisplayRegion t m, HasFocusReader t m, HasTheme t m)
103112 => m (TextInput t)
104113 -> Dynamic t Int
105114 -> m (TextInput t)
@@ -111,10 +120,6 @@ textInputTile txt width = do
111120 Orientation_Row -> width
112121 return t
113122
114−-- | Default attributes for the text cursor
115−cursorAttributes :: V.Attr
116−cursorAttributes = V.withStyle V.defAttr V.reverseVideo
117−
118123-- | Turn a set of display line rows into a list of images (one per line)
119124images :: [[Span V.Attr]] -> [V.Image]
120125images = map (V.horizCat . map spanToImage)
src/Reflex/Vty/Widget/Layout.hs+4
@@ -133,6 +133,8 @@ instance (HasImageWriter t m, MonadFix m) => HasImageWriter t (Focus t m) where
133133
134134instance (HasFocusReader t m, Monad m) => HasFocusReader t (Focus t m)
135135
136+instance (HasTheme t m, Monad m) => HasTheme t (Focus t m)
137+
136138instance (Reflex t, MonadFix m, MonadNodeId m) => HasFocus t (Focus t m) where
137139 makeFocus = do
138140 fid <- FocusId <$> lift getNextNodeId
@@ -430,6 +432,8 @@ instance (HasDisplayRegion t m, HasImageWriter t m, MonadFix m) => HasImageWrite
430432
431433instance (HasFocusReader t m, Monad m) => HasFocusReader t (Layout t m)
432434
435+instance (HasTheme t m, Monad m) => HasTheme t (Layout t m)
436+
433437instance (Monad m, MonadNodeId m, Reflex t, MonadFix m) => HasLayout t (Layout t m) where
434438 axis o c (Layout x) = Layout $ do
435439 nodeId <- getNextNodeId
src/Reflex/Vty/Widget/Text.hs+17−10
@@ -14,13 +14,15 @@ import Reflex.Vty.Widget
1414import Reflex.Vty.Widget.Input.Mouse
1515
1616-- | Fill the background with a particular character.
17−fill :: (HasDisplayRegion t m, HasImageWriter t m) => Behavior t Char -> m ()
17+fill :: (HasDisplayRegion t m, HasImageWriter t m, HasTheme t m) => Behavior t Char -> m ()
1818fill bc = do
1919 dw <- displayWidth
2020 dh <- displayHeight
21+ bt <- theme
2122 let fillImg =
22− (\w h c -> [V.charFill V.defAttr c w h])
23− <$> current dw
23+ (\attr w h c -> [V.charFill attr c w h])
24+ <$> bt
25+ <*> current dw
2426 <*> current dh
2527 <*> bc
2628 tellImages fillImg
@@ -33,9 +35,11 @@ data RichTextConfig t = RichTextConfig
3335instance Reflex t => Default (RichTextConfig t) where
3436 def = RichTextConfig $ pure V.defAttr
3537
38+
39+-- TODO delete this and use new local theming
3640-- | A widget that displays text with custom time-varying attributes
3741richText
38− :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m)
42+ :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m, HasTheme t m)
3943 => RichTextConfig t
4044 -> Behavior t Text
4145 -> m ()
@@ -53,15 +57,17 @@ richText cfg t = do
5357
5458-- | Renders text, wrapped to the container width
5559text
56− :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m)
60+ :: (Reflex t, Monad m, HasDisplayRegion t m, HasImageWriter t m, HasTheme t m)
5761 => Behavior t Text
5862 -> m ()
59−text = richText def
63+text t = do
64+ bt <- theme
65+ richText (RichTextConfig bt) t
6066
6167-- | Scrollable text widget. The output pair exposes the current scroll position and total number of lines (including those
6268-- that are hidden)
6369scrollableText
64− :: forall t m. (Reflex t, MonadHold t m, MonadFix m, HasDisplayRegion t m, HasInput t m, HasImageWriter t m)
70+ :: forall t m. (Reflex t, MonadHold t m, MonadFix m, HasDisplayRegion t m, HasInput t m, HasImageWriter t m, HasTheme t m)
6571 => Event t Int
6672 -- ^ Number of lines to scroll by
6773 -> Behavior t Text
@@ -69,7 +75,8 @@ scrollableText
6975 -- ^ (Current scroll position, total number of lines)
7076scrollableText scrollBy t = do
7177 dw <- displayWidth
72− let imgs = wrap <$> current dw <*> t
78+ bt <- theme
79+ let imgs = wrap <$> bt <*> current dw <*> t
7380 kup <- key V.KUp
7481 kdown <- key V.KDown
7582 m <- mouseScroll
@@ -88,12 +95,12 @@ scrollableText scrollBy t = do
8895 tellImages $ fmap ((:[]) . V.vertCat) $ drop <$> current lineIndex <*> imgs
8996 return $ (,) <$> ((+) <$> current lineIndex <*> pure 1) <*> (length <$> imgs)
9097 where
91− wrap maxWidth = concatMap (fmap (V.string V.defAttr . T.unpack) . TZ.wrapWithOffset maxWidth 0) . T.split (=='\n')
98+ wrap attr maxWidth = concatMap (fmap (V.string attr . T.unpack) . TZ.wrapWithOffset maxWidth 0) . T.split (=='\n')
9299
93100-- | Renders any behavior whose value can be converted to
94101-- 'String' as text
95102display
96− :: (Reflex t, Monad m, Show a, HasDisplayRegion t m, HasImageWriter t m)
103+ :: (Reflex t, Monad m, Show a, HasDisplayRegion t m, HasImageWriter t m, HasTheme t m)
97104 => Behavior t a
98105 -> m ()
99106display a = text $ T.pack . show <$> a
test/Data/Text/ZipperSpec.hs+83−64
@@ -9,77 +9,96 @@ import Prelude
99import Test.Hspec
1010
1111import qualified Data.Map as Map
12−import Data.Text
12+import qualified Data.Text as T
13+import Control.Monad
1314
1415import Data.Text.Zipper
1516
1617
1718someSentence = "12345 1234 12"
1819
19−splitSentenceAtDisplayWidth :: Int -> Text -> [(Text, Bool)]
20+splitSentenceAtDisplayWidth :: Int -> T.Text -> [(T.Text, Bool)]
2021splitSentenceAtDisplayWidth w t = splitWordsAtDisplayWidth w (wordsWithWhitespace t)
2122
2223spec :: Spec
23−spec = do
24+spec =
2425 describe "Zipper" $ do
25− it "wordsWithWhitespace" $ do
26− wordsWithWhitespace "" `shouldBe` []
27− wordsWithWhitespace "😱😱😱" `shouldBe` ["😱😱😱"]
28− wordsWithWhitespace "abcd efgf f" `shouldBe` ["abcd ","efgf ", "f"]
29− wordsWithWhitespace "aoeu " `shouldBe` ["aoeu "]
30− it "splitWordsAtDisplayWidth" $ do
31− fmap fst (splitSentenceAtDisplayWidth 5 "123456") `shouldBe` ["12345","6"]
32− fmap fst (splitSentenceAtDisplayWidth 5 "12345 6") `shouldBe` ["12345","6"]
33− fmap fst (splitSentenceAtDisplayWidth 5 "1234 56") `shouldBe` ["1234","56"]
34− fmap fst (splitSentenceAtDisplayWidth 5 "12345678912345") `shouldBe` ["12345","67891","2345"]
35− fmap fst (splitSentenceAtDisplayWidth 5 "1234 56") `shouldBe` ["1234 "," 56"]
36− fmap fst (splitSentenceAtDisplayWidth 8 "1 2 3 4 5 6 7 8 9 1") `shouldBe` ["1 2 3 4","5 6 7 8", "9 1"]
37− it "wrapWithOffsetAndAlignment" $ do
38− wrapWithOffsetAndAlignment TextAlignment_Left 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
39− wrapWithOffsetAndAlignment TextAlignment_Right 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 1), (WrappedLine "12" False 3)]
40− wrapWithOffsetAndAlignment TextAlignment_Center 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 1)]
41− it "eolSpacesToLogicalLines" $ do
42− eolSpacesToLogicalLines
43− [
44− [ (WrappedLine "😱" True 1), (WrappedLine "😱" False 2), (WrappedLine "😱" False 3) ]
45− , [ (WrappedLine "aa" True 1), (WrappedLine "aa" True 2), (WrappedLine "aa" False 3) ]
46− ]
47− `shouldBe`
48− [
49− [ ("😱",1) ]
50− , [ ("😱",2), ("😱",3) ]
51− , [ ("aa",1) ]
52− , [ ("aa",2) ]
53− , [ ("aa",3) ]
54− ]
55− it "offsetMapWithAlignmentInternal" $ do
56− offsetMapWithAlignmentInternal
57− [
58− [ (WrappedLine "😱" True 1), (WrappedLine "😱" False 2), (WrappedLine "😱" False 3) ]
59− , [ (WrappedLine "aa" True 1), (WrappedLine "aa" True 2), (WrappedLine "aa" False 3) ]
60− ]
61− `shouldBe`
62− Map.fromList
63− [ (0, (1,0))
64− , (1, (2,2)) -- jump by 1 for char and 1 for space
65− , (2, (3,3)) -- jump by 1 for char
66− , (3, (1,5)) -- jump by 1 for char and 1 for newline
67− , (4, (2,8)) -- jump by 2 for char and 1 for space
68− , (5, (3,11)) -- jump by 2 for char and 1 for space
69− ]
70− it "displayLinesWithAlignment - cursorPos" $ do
71− let
72− dl0 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "")
73− dl1 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "aoeu")
74− dl2 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "aoeu\n")
75− dl3 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "0123456789")
76− dl4 = displayLinesWithAlignment TextAlignment_Right 10 () () (insertChar 'a' $ fromText "aoeu")
77− dl5 = displayLinesWithAlignment TextAlignment_Right 10 () () (left $ insertChar 'a' $ fromText "aoeu")
78− dl6 = displayLinesWithAlignment TextAlignment_Right 10 () () (deleteLeft $ insertChar 'a' $ fromText "aoeu")
79− _displayLinesWithAlignment_cursorPos dl0 `shouldBe` (0,0)
80− _displayLinesWithAlignment_cursorPos dl1 `shouldBe` (4,0)
81− _displayLinesWithAlignment_cursorPos dl2 `shouldBe` (0,1)
82− _displayLinesWithAlignment_cursorPos dl3 `shouldBe` (0,1)
83− _displayLinesWithAlignment_cursorPos dl4 `shouldBe` (5,0)
84− _displayLinesWithAlignment_cursorPos dl5 `shouldBe` (4,0)
85− _displayLinesWithAlignment_cursorPos dl6 `shouldBe` (4,0)
26+ it "wordsWithWhitespace" $ do
27+ wordsWithWhitespace "" `shouldBe` []
28+ wordsWithWhitespace "😱😱😱" `shouldBe` ["😱😱😱"]
29+ wordsWithWhitespace "abcd efgf f" `shouldBe` ["abcd ","efgf ", "f"]
30+ wordsWithWhitespace "aoeu " `shouldBe` ["aoeu "]
31+ it "splitWordsAtDisplayWidth" $ do
32+ fmap fst (splitSentenceAtDisplayWidth 5 "123456") `shouldBe` ["12345","6"]
33+ fmap fst (splitSentenceAtDisplayWidth 5 "12345 6") `shouldBe` ["12345","6"]
34+ fmap fst (splitSentenceAtDisplayWidth 5 "1234 56") `shouldBe` ["1234","56"]
35+ fmap fst (splitSentenceAtDisplayWidth 5 "12345678912345") `shouldBe` ["12345","67891","2345"]
36+ fmap fst (splitSentenceAtDisplayWidth 5 "1234 56") `shouldBe` ["1234 "," 56"]
37+ fmap fst (splitSentenceAtDisplayWidth 8 "1 2 3 4 5 6 7 8 9 1") `shouldBe` ["1 2 3 4","5 6 7 8", "9 1"]
38+ it "wrapWithOffsetAndAlignment" $ do
39+ wrapWithOffsetAndAlignment TextAlignment_Left 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
40+ wrapWithOffsetAndAlignment TextAlignment_Right 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 1), (WrappedLine "12" False 3)]
41+ wrapWithOffsetAndAlignment TextAlignment_Center 5 0 someSentence `shouldBe` [(WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 1)]
42+ wrapWithOffsetAndAlignment TextAlignment_Left 5 1 someSentence `shouldBe` [(WrappedLine "1234" False 0), (WrappedLine "5" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
43+
44+ -- leading spaces and offset case
45+ wrapWithOffsetAndAlignment TextAlignment_Left 5 1 (" " <> someSentence) `shouldBe` [(WrappedLine " " True 0), (WrappedLine "12345" True 0), (WrappedLine "1234" True 0), (WrappedLine "12" False 0)]
46+ it "eolSpacesToLogicalLines" $ do
47+ eolSpacesToLogicalLines
48+ [
49+ [ (WrappedLine "😱" True 1), (WrappedLine "😱" False 2), (WrappedLine "😱" False 3) ]
50+ , [ (WrappedLine "aa" True 1), (WrappedLine "aa" True 2), (WrappedLine "aa" False 3) ]
51+ ]
52+ `shouldBe`
53+ [
54+ [ ("😱",1) ]
55+ , [ ("😱",2), ("😱",3) ]
56+ , [ ("aa",1) ]
57+ , [ ("aa",2) ]
58+ , [ ("aa",3) ]
59+ ]
60+ it "offsetMapWithAlignmentInternal" $ do
61+ offsetMapWithAlignmentInternal
62+ [
63+ [ (WrappedLine "😱" True 1), (WrappedLine "😱" False 2), (WrappedLine "😱" False 3) ]
64+ , [ (WrappedLine "aa" True 1), (WrappedLine "aa" True 2), (WrappedLine "aa" False 3) ]
65+ ]
66+ `shouldBe`
67+ Map.fromList
68+ [ (0, (1,0))
69+ , (1, (2,2)) -- jump by 1 for char and 1 for space
70+ , (2, (3,3)) -- jump by 1 for char
71+ , (3, (1,5)) -- jump by 1 for char and 1 for newline
72+ , (4, (2,8)) -- jump by 2 for char and 1 for space
73+ , (5, (3,11)) -- jump by 2 for char and 1 for space
74+ ]
75+ it "displayLines - cursorPos" $ do
76+ let
77+ dl0 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "")
78+ dl1 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "aoeu")
79+ dl2 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "aoeu\n")
80+ dl3 = displayLinesWithAlignment TextAlignment_Right 10 () () (fromText "0123456789")
81+ dl4 = displayLinesWithAlignment TextAlignment_Right 10 () () (insertChar 'a' $ fromText "aoeu")
82+ dl5 = displayLinesWithAlignment TextAlignment_Right 10 () () (left $ insertChar 'a' $ fromText "aoeu")
83+ dl6 = displayLinesWithAlignment TextAlignment_Right 10 () () (deleteLeft $ insertChar 'a' $ fromText "aoeu")
84+ _displayLines_cursorPos dl0 `shouldBe` (0,0)
85+ _displayLines_cursorPos dl1 `shouldBe` (4,0)
86+ _displayLines_cursorPos dl2 `shouldBe` (0,1)
87+ _displayLines_cursorPos dl3 `shouldBe` (0,1)
88+ _displayLines_cursorPos dl4 `shouldBe` (5,0)
89+ _displayLines_cursorPos dl5 `shouldBe` (4,0)
90+ _displayLines_cursorPos dl6 `shouldBe` (4,0)
91+ it "displayLines - offsetMap" $ do
92+ let
93+ dl0 = displayLinesWithAlignment TextAlignment_Left 5 () () (end $ fromText "aoeku")
94+ _displayLines_cursorPos dl0 `shouldBe` (0,1)
95+ Map.size (_displayLines_offsetMap dl0) `shouldBe` 2 -- cursor character is on second line
96+ it "displayLinesWithAlignment - spans" $ do
97+ let
98+ someText = top $ fromText "0123456789abcdefgh"
99+ -- outer span length should be invariant when changing TextAlignment and CursorPosition
100+ forM_ [0..4] $ \x -> do
101+ forM_ [TextAlignment_Left, TextAlignment_Center, TextAlignment_Right] $ \ta -> do
102+ let t = rightN x $ someText
103+ length (_displayLines_spans $ (displayLinesWithAlignment ta 5 () () t)) `shouldBe` 4
104+ length (_displayLines_spans $ (displayLinesWithAlignment ta 10 () () t)) `shouldBe` 2