@@ -30,7 +30,6 @@ module Reflex.Vty.Widget
30 , modifyImages
30 , modifyImages
31 , tellImages
31 , tellImages
32 , tellShutdown
32 , tellShutdown
35 , splitVDrag
34 , splitVDrag
36 , fractionSz
35 , fractionSz
@@ -92,7 +91,9 @@ instance (Reflex t) => Monoid (VtyWidgetOut t) where
92 mempty = VtyWidgetOut mempty mempty
91 mempty = VtyWidgetOut mempty mempty
93 mappend wo wo' = wo <> wo'
92 mappend wo wo' = wo <> wo'
94+newtype VtyWidget t m a = VtyWidget
95−newtype VtyWidget t m a = VtyWidget { unVtyWidget :: WriterT (VtyWidgetOut t) (ReaderT (VtyWidgetCtx t) m) a }
95+ { unVtyWidget :: WriterT (VtyWidgetOut t) (ReaderT (VtyWidgetCtx t) m) a
96 deriving (Functor, Applicative, Monad, MonadSample t, MonadHold t, MonadFix)
97 deriving (Functor, Applicative, Monad, MonadSample t, MonadHold t, MonadFix)
98-- | Runs a 'VtyWidget' with a given context
99-- | Runs a 'VtyWidget' with a given context
@@ -104,7 +105,10 @@ runVtyWidget
104runVtyWidget ctx w = runReaderT (runWriterT (unVtyWidget w)) ctx
105runVtyWidget ctx w = runReaderT (runWriterT (unVtyWidget w)) ctx
106-- | Sets up the top-level context for a 'VtyWidget' and runs it with that context
107-- | Sets up the top-level context for a 'VtyWidget' and runs it with that context
107−mainWidgetWithHandle :: V.Vty -> (forall t m. MonadVtyApp t m => VtyWidget t m ()) -> IO ()
110+ -> (forall t m. MonadVtyApp t m => VtyWidget t m ())
108mainWidgetWithHandle vty child =
112mainWidgetWithHandle vty child =
109 runVtyAppWithHandle vty $ \dr0 inp -> do
113 runVtyAppWithHandle vty $ \dr0 inp -> do
110 size <- holdDyn dr0 $ fforMaybe inp $ \case
114 size <- holdDyn dr0 $ fforMaybe inp $ \case
@@ -125,7 +129,9 @@ mainWidgetWithHandle vty child =
127-- | Like 'mainWidgetWithHandle', but uses a default vty configuration
131-- | Like 'mainWidgetWithHandle', but uses a default vty configuration
128−mainWidget :: (forall t m. MonadVtyApp t m => VtyWidget t m ()) -> IO ()
133+ :: (forall t m. MonadVtyApp t m => VtyWidget t m ())
129mainWidget child = do
135mainWidget child = do
130 vty <- getDefaultVty
136 vty <- getDefaultVty
131 mainWidgetWithHandle vty child
137 mainWidgetWithHandle vty child
@@ -414,25 +420,33 @@ box style child = do
415 in sides ++ if width > 1 && height > 1 then corners else []
421 in sides ++ if width > 1 && height > 1 then corners else []
417−text :: (Reflex t, Monad m) => Behavior t Text -> VtyWidget t m ()
424+ :: (Reflex t, Monad m)
418text msg = do
427text msg = do
419 dw <- displayWidth
428 dw <- displayWidth
420 let img = (\w s -> [wrapText w V.defAttr s]) <$> current dw <*> msg
429 let img = (\w s -> [wrapText w V.defAttr s]) <$> current dw <*> msg
421 tellImages img
430 tellImages img
432+ wrapText maxWidth attrs = V.vertCat
433+ . concatMap (fmap (V.string attrs . T.unpack) . TZ.wrapWithOffset maxWidth 0)
423−display :: (Reflex t, Monad m, Show a) => Behavior t a -> VtyWidget t m ()
437+ :: (Reflex t, Monad m, Show a)
424display a = text $ T.pack . show <$> a
440display a = text $ T.pack . show <$> a
426regionBlankImage :: Region -> Image
442regionBlankImage :: Region -> Image
427regionBlankImage r@(Region _ _ width height) =
443regionBlankImage r@(Region _ _ width height) =
428 withinImage r $ V.charFill V.defAttr ' ' width height
444 withinImage r $ V.charFill V.defAttr ' ' width height
430−withinImage :: Region -> Image -> Image
446+withinImage
431withinImage (Region left top width height)
450withinImage (Region left top width height)
432 | width < 0 || height < 0 = withinImage (Region left top 0 0)
451 | width < 0 || height < 0 = withinImage (Region left top 0 0)
433 | otherwise = V.translate left top . V.crop width height
452 | otherwise = V.translate left top . V.crop width height
435−wrapText :: Int -> Attr -> Text -> Image
436−wrapText maxWidth attrs = V.vertCat
437− . concatMap (fmap (V.string attrs . T.unpack) . TZ.wrapWithOffset maxWidth 0)