module Client.Image.Utils (lineWrap) where
import Graphics.Vty.Image
import Graphics.Vty.Attributes
lineWrap ::
Int ->
Maybe Int ->
Image ->
Image
lineWrap w mi img
| imageWidth img == 0 = emptyImage
| imageWidth img <= w = terminate w img
| otherwise =
terminate w (cropRight w img) <->
maybe (lineWrapNoIndent w) (lineWrapIndent w) mi
(cropLeft (imageWidth img - w) img)
terminate ::
Int ->
Image ->
Image
terminate n img
| imageWidth img == n = img
| otherwise = img <|> char defAttr ' '
lineWrapNoIndent ::
Int ->
Image ->
Image
lineWrapNoIndent w img
| iw <= w = terminate w img
| otherwise = cropRight w img <->
lineWrapNoIndent w (cropLeft (iw - w) img)
where
iw = imageWidth img
lineWrapIndent ::
Int ->
Int ->
Image ->
Image
lineWrapIndent w i img
| 20 + i > w = lineWrapNoIndent w img
| iw + i <= w = terminate w (leftPad i img)
| otherwise = leftPad i (cropRight (w-i) img) <->
lineWrapIndent w i (cropLeft (iw - w + i) img)
where
iw = imageWidth img
leftPad :: Int -> Image -> Image
leftPad i = pad i 0 0 0