module Game.LambdaHack.Common.Msg
( makePhrase, makeSentence
, Msg, (<>), (<+>), tshow, toWidth, moreMsg, yesnoMsg, truncateMsg
, Report, emptyReport, nullReport, singletonReport, addMsg
, splitReport, renderReport, findInReport
, History, emptyHistory, singletonHistory, mergeHistory
, addReport, renderHistory, takeHistory
, Overlay(overlay), emptyOverlay, truncateToOverlay, toOverlay
, Slideshow(slideshow), splitOverlay, toSlideshow)
where
import Data.Binary
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import qualified NLP.Miniutter.English as MU
import qualified Text.Show.Pretty as Show.Pretty
import Game.LambdaHack.Common.Misc
import Game.LambdaHack.Common.Point
infixr 6 <+>
(<+>) :: Text -> Text -> Text
(<+>) = (MU.<+>)
tshow :: Show a => a -> Text
tshow x = T.pack $ Show.Pretty.ppShow x
toWidth :: Int -> Text -> Text
toWidth n x = T.take n (T.justifyLeft n ' ' x)
makePhrase, makeSentence :: [MU.Part] -> Text
makePhrase = MU.makePhrase MU.defIrregular
makeSentence = MU.makeSentence MU.defIrregular
type Msg = Text
moreMsg :: Msg
moreMsg = "--more-- "
yesnoMsg :: Msg
yesnoMsg = "[yn]"
truncateMsg :: X -> Text -> Text
truncateMsg w xsRaw =
let xs = case T.lines xsRaw of
[] -> xsRaw
[line] -> line
line : _ -> T.justifyLeft (w + 1) ' ' line
len = T.length xs
in case compare w len of
LT -> T.snoc (T.take (w 1) xs) '$'
EQ -> xs
GT -> if T.null xs || T.last xs == ' '
then xs
else T.snoc xs ' '
newtype Report = Report [(BS.ByteString, Int)]
deriving (Show, Binary)
emptyReport :: Report
emptyReport = Report []
nullReport :: Report -> Bool
nullReport (Report l) = null l
singletonReport :: Msg -> Report
singletonReport = addMsg emptyReport
addMsg :: Report -> Msg -> Report
addMsg r m | T.null m = r
addMsg (Report ((x, n) : xns)) y' | x == y =
Report $ (y, n + 1) : xns
where y = encodeUtf8 y'
addMsg (Report xns) y = Report $ (encodeUtf8 y, 1) : xns
splitReport :: X -> Report -> Overlay
splitReport w r = Overlay $ splitReportList w r
splitReportList :: X -> Report -> [Text]
splitReportList w r = splitText w $ renderReport r
renderReport :: Report -> Text
renderReport (Report []) = T.empty
renderReport (Report (xn : xs)) =
renderReport (Report xs) <+> renderRepetition xn
renderRepetition :: (BS.ByteString, Int) -> Text
renderRepetition (s, 1) = decodeUtf8 s
renderRepetition (s, n) = decodeUtf8 s <> "<x" <> tshow n <> ">"
findInReport :: (BS.ByteString -> Bool) -> Report -> Maybe BS.ByteString
findInReport f (Report xns) = find f $ map fst xns
splitText :: X -> Text -> [Text]
splitText w xs = concatMap (splitText' w . T.stripStart) $ T.lines xs
splitText' :: X -> Text -> [Text]
splitText' w xs
| w >= T.length xs = [xs]
| otherwise =
let (pre, post) = T.splitAt w xs
(ppre, ppost) = T.break (== ' ') $ T.reverse pre
testPost = T.stripEnd ppost
in if T.null testPost
then pre : splitText w post
else T.reverse ppost : splitText w (T.reverse ppre <> post)
newtype History = History [Report]
deriving (Show, Binary)
emptyHistory :: History
emptyHistory = History []
singletonHistory :: Report -> History
singletonHistory r = addReport r emptyHistory
mergeHistory :: [(Msg, History)] -> History
mergeHistory l =
let unhist (History x) = x
f (msg, h) = singletonReport msg : unhist h
in History $ concatMap f l
renderHistory :: History -> Overlay
renderHistory (History h) =
let w = fst normalLevelBound + 1
in Overlay $ concatMap (splitReportList w) h
addReport :: Report -> History -> History
addReport (Report []) h = h
addReport m (History []) = History [m]
addReport (Report m) (History (Report h : hs)) =
case (reverse m, h) of
((s1, n1) : rs, (s2, n2) : hhs) | s1 == s2 ->
let hist = Report ((s2, n1 + n2) : hhs) : hs
in History $ if null rs then hist else Report (reverse rs) : hist
_ -> History $ Report m : Report h : hs
takeHistory :: Int -> History -> History
takeHistory k (History h) = History $ take k h
newtype Overlay = Overlay {overlay :: [Text]}
deriving (Show, Eq, Binary)
emptyOverlay :: Overlay
emptyOverlay = Overlay []
truncateToOverlay :: X -> Text -> Overlay
truncateToOverlay lxsize msg = Overlay [truncateMsg lxsize msg]
toOverlay :: [Text] -> Overlay
toOverlay = Overlay
splitOverlay :: Bool -> Y -> Overlay -> Overlay -> Slideshow
splitOverlay onBlank yspace (Overlay msg) (Overlay ls) =
let over = msg ++ ls
in if length over <= yspace
then Slideshow (onBlank, [Overlay over])
else let (pre, post) = splitAt (yspace 1) over
Slideshow (_, slides) =
splitOverlay onBlank yspace (Overlay msg) (Overlay post)
in Slideshow $ (onBlank, (Overlay $ pre ++ [moreMsg]) : slides)
newtype Slideshow = Slideshow {slideshow :: (Bool, [Overlay])}
deriving Show
instance Monoid Slideshow where
mempty = Slideshow (False, [])
mappend (Slideshow (b1, l1)) (Slideshow (b2, l2)) =
Slideshow (b1 || b2, l1 ++ l2)
toSlideshow :: Bool -> [[Text]] -> Slideshow
toSlideshow onBlank l = Slideshow (onBlank, map Overlay l)