module Imj.Graphics.Render.Naive
( NaiveDraw(..)
) where
import Data.Text(unpack)
import Control.Monad.Reader(liftIO)
import System.IO(hFlush, stdout)
import System.Console.ANSI(setCursorPosition, clearFromCursorToScreenEnd)
import System.Console.ANSI.Codes(csi)
import Imj.Geo.Discrete
import Imj.Graphics.Class.Draw
import Imj.Graphics.Class.Render
import Imj.Graphics.Color.Types
data NaiveDraw = NaiveDraw
move' :: Coords Pos -> IO ()
move' (Coords (Coord y) (Coord x)) =
setCursorPosition y x
color :: LayeredColor -> IO ()
color (LayeredColor bg fg) = do
let bgCodes = color8BgSGRToCode bg
fgCodes = color8FgSGRToCode fg
putStr $ csi (bgCodes ++ fgCodes) "m"
instance Draw NaiveDraw where
drawChar' _ b c d = liftIO $ move' c >> color d >> putChar b
drawChars' _ b c d e = liftIO $ move' d >> color e >> putStr (replicate b c)
drawTxt' _ b c d = liftIO $ move' c >> color d >> putStr (unpack b)
drawStr' _ b c d = liftIO $ move' c >> color d >> putStr b
{-# INLINABLE drawChar' #-}
{-# INLINABLE drawChars' #-}
{-# INLINABLE drawTxt' #-}
{-# INLINABLE drawStr' #-}
instance Render NaiveDraw where
renderToScreen' _ = liftIO $ hFlush stdout
>> setCursorPosition 0 0
>> clearFromCursorToScreenEnd
{-# INLINABLE renderToScreen' #-}