module Graphics.Vty.Output.Interface
where
import Graphics.Vty.Prelude
import Graphics.Vty.Picture
import Graphics.Vty.PictureToSpans
import Graphics.Vty.Span
import Graphics.Vty.DisplayAttributes
import Blaze.ByteString.Builder (Write, writeToByteString)
import Blaze.ByteString.Builder.ByteString (writeByteString)
import Control.Monad.Trans
import qualified Data.ByteString as BS
import Data.IORef
import Data.Monoid (mempty, mappend)
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Vector as Vector
data Output = Output
{
terminalID :: String
, releaseTerminal :: MonadIO m => m ()
, reserveDisplay :: MonadIO m => m ()
, releaseDisplay :: MonadIO m => m ()
, displayBounds :: MonadIO m => m DisplayRegion
, outputByteBuffer :: BS.ByteString -> IO ()
, contextColorCount :: Int
, supportsCursorVisibility :: Bool
, assumedStateRef :: IORef AssumedState
, mkDisplayContext :: MonadIO m => Output -> DisplayRegion -> m DisplayContext
}
displayContext :: MonadIO m => Output -> DisplayRegion -> m DisplayContext
displayContext t = liftIO . mkDisplayContext t t
data AssumedState = AssumedState
{ prevFattr :: Maybe FixedAttr
, prevOutputOps :: Maybe DisplayOps
}
initialAssumedState :: AssumedState
initialAssumedState = AssumedState Nothing Nothing
data DisplayContext = DisplayContext
{ contextDevice :: Output
, contextRegion :: DisplayRegion
, writeMoveCursor :: Int -> Int -> Write
, writeShowCursor :: Write
, writeHideCursor :: Write
, writeSetAttr :: FixedAttr -> Attr -> DisplayAttrDiff -> Write
, writeDefaultAttr :: Write
, writeRowEnd :: Write
, inlineHack :: IO ()
}
writeUtf8Text :: BS.ByteString -> Write
writeUtf8Text = writeByteString
outputPicture :: MonadIO m => DisplayContext -> Picture -> m ()
outputPicture dc pic = liftIO $ do
as <- readIORef (assumedStateRef $ contextDevice dc)
let manipCursor = supportsCursorVisibility (contextDevice dc)
r = contextRegion dc
ops = displayOpsForPic pic r
initialAttr = FixedAttr defaultStyleMask Nothing Nothing
diffs :: [Bool] = case prevOutputOps as of
Nothing -> replicate (fromEnum $ regionHeight $ effectedRegion ops) True
Just previousOps -> if effectedRegion previousOps /= effectedRegion ops
then replicate (displayOpsRows ops) True
else zipWith (/=) (Vector.toList previousOps)
(Vector.toList ops)
out = (if manipCursor then writeHideCursor dc else mempty)
`mappend` writeDefaultAttr dc
`mappend` writeOutputOps dc initialAttr diffs ops
`mappend`
(case picCursor pic of
_ | not manipCursor -> mempty
NoCursor -> mempty
Cursor x y ->
let m = cursorOutputMap ops $ picCursor pic
(ox, oy) = charToOutputPos m (x,y)
in writeShowCursor dc `mappend` writeMoveCursor dc ox oy
)
outputByteBuffer (contextDevice dc) (writeToByteString out)
let as' = as { prevOutputOps = Just ops }
writeIORef (assumedStateRef $ contextDevice dc) as'
writeOutputOps :: DisplayContext -> FixedAttr -> [Bool] -> DisplayOps -> Write
writeOutputOps dc inFattr diffs ops =
let (_, out, _, _) = Vector.foldl' writeOutputOps'
(0, mempty, inFattr, diffs)
ops
in out
where
writeOutputOps' (y, out, fattr, True : diffs') spanOps
= let (spanOut, fattr') = writeSpanOps dc y fattr spanOps
in (y+1, out `mappend` spanOut, fattr', diffs')
writeOutputOps' (y, out, fattr, False : diffs') _spanOps
= (y + 1, out, fattr, diffs')
writeOutputOps' (_y, _out, _fattr, []) _spanOps
= error "vty - output spans without a corresponding diff."
writeSpanOps :: DisplayContext -> Int -> FixedAttr -> SpanOps -> (Write, FixedAttr)
writeSpanOps dc y inFattr spanOps =
let start = writeMoveCursor dc 0 y
in Vector.foldl' (\(out, fattr) op -> case writeSpanOp dc op fattr of
(opOut, fattr') -> (out `mappend` opOut, fattr')
)
(start, inFattr)
spanOps
writeSpanOp :: DisplayContext -> SpanOp -> FixedAttr -> (Write, FixedAttr)
writeSpanOp dc (TextSpan attr _ _ str) fattr =
let attr' = limitAttrForDisplay (contextDevice dc) attr
fattr' = fixDisplayAttr fattr attr'
diffs = displayAttrDiffs fattr fattr'
out = writeSetAttr dc fattr attr' diffs
`mappend` writeUtf8Text (T.encodeUtf8 $ TL.toStrict str)
in (out, fattr')
writeSpanOp _dc (Skip _) _fattr = error "writeSpanOp for Skip"
writeSpanOp dc (RowEnd _) fattr = (writeRowEnd dc, fattr)
data CursorOutputMap = CursorOutputMap
{ charToOutputPos :: (Int, Int) -> (Int, Int)
}
cursorOutputMap :: DisplayOps -> Cursor -> CursorOutputMap
cursorOutputMap spanOps _cursor = CursorOutputMap
{ charToOutputPos = \(cx, cy) -> (cursorColumnOffset spanOps cx cy, cy)
}
cursorColumnOffset :: DisplayOps -> Int -> Int -> Int
cursorColumnOffset ops cx cy =
let cursorRowOps = Vector.unsafeIndex ops (fromEnum cy)
(outOffset, _, _)
= Vector.foldl' ( \(d, currentCx, done) op ->
if done then (d, currentCx, done) else case spanOpHasWidth op of
Nothing -> (d, currentCx, False)
Just (cw, ow) -> case compare cx (currentCx + cw) of
GT -> ( d + ow
, currentCx + cw
, False
)
EQ -> ( d + ow
, currentCx + cw
, True
)
LT -> ( d + columnsToCharOffset (cx currentCx) op
, currentCx + cw
, True
)
)
(0, 0, False)
cursorRowOps
in outOffset
limitAttrForDisplay :: Output -> Attr -> Attr
limitAttrForDisplay t attr
= attr { attrForeColor = clampColor $ attrForeColor attr
, attrBackColor = clampColor $ attrBackColor attr
}
where
clampColor Default = Default
clampColor KeepCurrent = KeepCurrent
clampColor (SetTo c) = clampColor' c
clampColor' (ISOColor v)
| contextColorCount t < 8 = Default
| contextColorCount t < 16 && v >= 8 = SetTo $ ISOColor (v 8)
| otherwise = SetTo $ ISOColor v
clampColor' (Color240 v)
| contextColorCount t < 8 = Default
| contextColorCount t < 16 = Default
| contextColorCount t == 240 = SetTo $ Color240 v
| otherwise
= let p :: Double = fromIntegral v / 240.0
v' = floor $ p * (fromIntegral $ contextColorCount t)
in SetTo $ Color240 v'