on master help sign in
Add padded image ctor and related code
adfb3b3Ben Boeckel
src/Graphics/Vty/Image.hs+9
@@ -20,6 +20,7 @@ module Graphics.Vty.Image ( Image(..)
20 , empty_image
20 , empty_image
21 , translate
21 , translate
22 , crop
22 , crop
23+ , pad
23 -- | The possible display attributes used in constructing an `Image`.
24 -- | The possible display attributes used in constructing an `Image`.
24 , module Graphics.Vty.Attributes
25 , module Graphics.Vty.Attributes
25 )
26 )
@@ -102,6 +103,8 @@ data Image =
102 | Translation (Int, Int) Image
103 | Translation (Int, Int) Image
103 -- Crop an image to a size
104 -- Crop an image to a size
104 | ImageCrop (Int, Int) Image
105 | ImageCrop (Int, Int) Image
106+ -- Pad an image up to a size
107+ | ImagePad (Int, Int) Image
105 deriving Eq
108 deriving Eq
106
109
107instance Show Image where
110instance Show Image where
@@ -117,6 +120,8 @@ instance Show Image where
117 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
120 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
118 show ( ImageCrop size i )
121 show ( ImageCrop size i )
119 = "ImageCrop " ++ show size ++ " ( " ++ show i ++ " )"
122 = "ImageCrop " ++ show size ++ " ( " ++ show i ++ " )"
123+ show ( ImagePad size i )
124+ = "ImagePad " ++ show size ++ " ( " ++ show i ++ " )"
120 show ( EmptyImage ) = "EmptyImage"
125 show ( EmptyImage ) = "EmptyImage"
121
126
122-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
127-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
@@ -209,6 +214,7 @@ image_width BGFill { output_width = w } = w
209image_width EmptyImage = 0
214image_width EmptyImage = 0
210image_width ( Translation _v i ) = image_width i
215image_width ( Translation _v i ) = image_width i
211image_width ( ImageCrop _v i ) = min (image_width i) (toEnum $ fst _v)
216image_width ( ImageCrop _v i ) = min (image_width i) (toEnum $ fst _v)
217+image_width ( ImagePad _v i ) = max (image_width i) (toEnum $ fst _v)
212
218
213-- | The height of an Image. This is the number of display rows the image will occupy.
219-- | The height of an Image. This is the number of display rows the image will occupy.
214image_height :: Image -> Word
220image_height :: Image -> Word
@@ -219,6 +225,7 @@ image_height BGFill { output_height = r } = r
219image_height EmptyImage = 0
225image_height EmptyImage = 0
220image_height ( Translation _v i ) = image_height i
226image_height ( Translation _v i ) = image_height i
221image_height ( ImageCrop _v i ) = min (image_height i) (toEnum $ snd _v)
227image_height ( ImageCrop _v i ) = min (image_height i) (toEnum $ snd _v)
228+image_height ( ImagePad _v i ) = max (image_height i) (toEnum $ snd _v)
222
229
223-- | Combines two images side by side.
230-- | Combines two images side by side.
224--
231--
@@ -338,3 +345,5 @@ translate v i = Translation v i
338crop :: (Int, Int) -> Image -> Image
345crop :: (Int, Int) -> Image -> Image
339crop v i = ImageCrop v i
346crop v i = ImageCrop v i
340
347
348+pad :: (Int, Int) -> Image -> Image
349+pad = ImagePad v i
src/Graphics/Vty/Picture.hs+1
@@ -17,6 +17,7 @@ module Graphics.Vty.Picture ( module Graphics.Vty.Picture
17 , empty_image
17 , empty_image
18 , translate
18 , translate
19 , crop
19 , crop
20+ , pad
20 -- | The possible display attributes used in constructing an `Image`.
21 -- | The possible display attributes used in constructing an `Image`.
21 , module Graphics.Vty.Attributes
22 , module Graphics.Vty.Attributes
22 )
23 )
src/Graphics/Vty/Span.hs+8
@@ -156,6 +156,14 @@ ops_for_row mrow_ops bg region image y remaining_columns
156 if y >= (toEnum $ snd _size)
156 if y >= (toEnum $ snd _size)
157 then return ()
157 then return ()
158 else ops_for_row mrow_ops bg region i y (min remaining_columns $ toEnum $ fst _size)
158 else ops_for_row mrow_ops bg region i y (min remaining_columns $ toEnum $ fst _size)
159+ ImagePad (x,y) i -> do
160+ let hpad = if image_width i < x
161+ then background_fill (x - image_width i) (image_height i)
162+ else empty_image
163+ let vpad = if image_height i < y
164+ then background_fill (image_width i) (y - image_height i)
165+ else empty_image
166+ ops_for_row mrow_ops bg region (vert_join (horiz_join i hpad) vpad) y remaining_columns
159
167
160snoc_text_span :: (Foldable.Foldable t)
168snoc_text_span :: (Foldable.Foldable t)
161 => Attr
169 => Attr