{-# LANGUAGE OverloadedStrings #-}
module Dep.Bricks.Utils (
fromRaster
, hline, hline'
, vline, vline'
, harrow, harrow'
, varrow, varrow'
, Row, Raster
, inRaster, inRaster'
) where
import Data.Text(Text, cons, pack, singleton, unpack)
import qualified Data.Text as T
import Dep.Utils(udiv)
import Graphics.Vty.Attributes(Attr)
import Graphics.Vty.Image(Image, (<->), (<|>), char, emptyImage, imageWidth, imageHeight, string, text', vertCat)
type Row = String
type Raster = [Row]
fromRaster
:: Attr
-> Raster
-> Image
fromRaster :: Attr -> Raster -> Image
fromRaster Attr
atr = (String -> Image -> Image) -> Image -> Raster -> Image
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Image -> Image -> Image
(<->) (Image -> Image -> Image)
-> (String -> Image) -> String -> Image -> Image
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> String -> Image
string Attr
atr) Image
emptyImage
harrow
:: Char
-> Text
-> Char
-> Attr
-> Int
-> Image
harrow :: Char -> Text -> Char -> Attr -> Int -> Image
harrow Char
c0 Text
ci Char
cn = Attr -> Int -> Image
go
where go :: Attr -> Int -> Image
go Attr
atr Int
n = Attr -> Text -> Image
text' Attr
atr (Char -> Text -> Text
cons Char
c0 (Int -> Text -> Text
T.take Int
n (Int -> Text -> Text
T.replicate (Int -> Int -> Int
forall i. Integral i => i -> i -> i
udiv Int
n (Text -> Int
T.length Text
ci)) Text
ci) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
singleton Char
cn))
harrow'
:: Char
-> String
-> Char
-> Attr
-> Int
-> Image
harrow' :: Char -> String -> Char -> Attr -> Int -> Image
harrow' Char
c0 = Char -> Text -> Char -> Attr -> Int -> Image
harrow Char
c0 (Text -> Char -> Attr -> Int -> Image)
-> (String -> Text) -> String -> Char -> Attr -> Int -> Image
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack
varrow
:: Char
-> Text
-> Char
-> Attr
-> Int
-> Image
varrow :: Char -> Text -> Char -> Attr -> Int -> Image
varrow Char
c0 = Char -> String -> Char -> Attr -> Int -> Image
varrow' Char
c0 (String -> Char -> Attr -> Int -> Image)
-> (Text -> String) -> Text -> Char -> Attr -> Int -> Image
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack
varrow'
:: Char
-> String
-> Char
-> Attr
-> Int
-> Image
varrow' :: Char -> String -> Char -> Attr -> Int -> Image
varrow' Char
c0 String
ci Char
cn = Attr -> Int -> Image
go
where go :: Attr -> Int -> Image
go Attr
atr Int
n = [Image] -> Image
vertCat ((Char -> Image) -> String -> [Image]
forall a b. (a -> b) -> [a] -> [b]
map (Attr -> Char -> Image
char Attr
atr) (Char
c0 Char -> String -> String
forall a. a -> [a] -> [a]
: Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
n (String -> String
forall a. [a] -> [a]
cycle String
ci) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [Char
cn]))
hline
:: Text
-> Attr
-> Int
-> Image
hline :: Text -> Attr -> Int -> Image
hline Text
ci = Attr -> Int -> Image
go
where go :: Attr -> Int -> Image
go Attr
atr Int
n = Attr -> Text -> Image
text' Attr
atr (Int -> Text -> Text
T.take Int
n (Int -> Text -> Text
T.replicate (Int -> Int -> Int
forall i. Integral i => i -> i -> i
udiv Int
n (Text -> Int
T.length Text
ci)) Text
ci))
hline'
:: String
-> Attr
-> Int
-> Image
hline' :: String -> Attr -> Int -> Image
hline' = Text -> Attr -> Int -> Image
hline (Text -> Attr -> Int -> Image)
-> (String -> Text) -> String -> Attr -> Int -> Image
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack
vline
:: Text
-> Attr
-> Int
-> Image
vline :: Text -> Attr -> Int -> Image
vline = String -> Attr -> Int -> Image
vline' (String -> Attr -> Int -> Image)
-> (Text -> String) -> Text -> Attr -> Int -> Image
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack
vline'
:: String
-> Attr
-> Int
-> Image
vline' :: String -> Attr -> Int -> Image
vline' String
ci = Attr -> Int -> Image
go
where go :: Attr -> Int -> Image
go Attr
atr Int
n = [Image] -> Image
vertCat ((Char -> Image) -> String -> [Image]
forall a b. (a -> b) -> [a] -> [b]
map (Attr -> Char -> Image
char Attr
atr) (Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
n (String -> String
forall a. [a] -> [a]
cycle String
ci)))
inRaster
:: Attr
-> Image
-> Image
inRaster :: Attr -> Image -> Image
inRaster Attr
atr Image
img = Image
top Image -> Image -> Image
<-> (Image
lft Image -> Image -> Image
<|> Image
img Image -> Image -> Image
<|> Image
rght) Image -> Image -> Image
<-> Image
bot
where w :: Int
w = Image -> Int
imageWidth Image
img
h :: Int
h = Image -> Int
imageHeight Image
img
lft :: Image
lft = Text -> Attr -> Int -> Image
vline Text
"\x2503\x2520" Attr
atr Int
h
rght :: Image
rght = Text -> Attr -> Int -> Image
vline Text
"\x2503\x2528" Attr
atr Int
h
top :: Image
top = Char -> Text -> Char -> Attr -> Int -> Image
harrow Char
'\x250f' Text
"\x2501\x252f\x2501\x252f\x2501\x252f\x2501\x2513" Char
'\x2513' Attr
atr Int
w
bot :: Image
bot = Char -> Text -> Char -> Attr -> Int -> Image
harrow Char
'\x2517' Text
"\x2501\x2537" Char
'\x251b'Attr
atr Int
w
inRaster'
:: Raster
-> Raster
inRaster' :: Raster -> Raster
inRaster' Raster
img = (Char
'\x250f' Char -> String -> String
forall a. a -> [a] -> [a]
: Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
w (String -> String
forall a. [a] -> [a]
cycle String
"\x2501\x252f\x2501\x252f\x2501\x252f\x2501\x2513 \x250f") String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\x2513") String -> Raster -> Raster
forall a. a -> [a] -> [a]
: String -> String -> Raster -> Raster
forall a. [a] -> [a] -> [[a]] -> [[a]]
go (String -> String
forall a. [a] -> [a]
cycle String
"\x2503\x2520\x2503\x2520\x2503\x2520\x2503\x2517 \x250f") (String -> String
forall a. [a] -> [a]
cycle String
"\x2503\x2528\x2503\x2528\x2503\x2528\x2503\x251b \x2513") Raster
img Raster -> Raster -> Raster
forall a. [a] -> [a] -> [a]
++ [Char
'\x2517' Char -> String -> String
forall a. a -> [a] -> [a]
: Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
w (String -> String
forall a. [a] -> [a]
cycle String
"\x2501\x2537\x2501\x2537\x2501\x2537\x2501\x251b \x2517") String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\x251b"]
where w :: Int
w = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ((String -> Int) -> Raster -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Raster
img)
go :: [a] -> [a] -> [[a]] -> [[a]]
go = (a -> a -> [a] -> [a]) -> [a] -> [a] -> [[a]] -> [[a]]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 (\a
x a
y [a]
z -> a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
z [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
y])