module Game.LambdaHack.Msg
( Msg, more, msgEnd, yesno, addMsg, splitMsg, padMsg
) where
import qualified Data.List as L
import Data.Char
type Msg = String
more :: Msg
more = " --more-- "
msgEnd :: Msg
msgEnd = " --end-- "
yesno :: Msg
yesno = " [yn]"
addMsg :: Msg -> Msg -> Msg
addMsg [] x = x
addMsg xs [] = xs
addMsg xs x = xs ++ " " ++ x
splitMsg :: Int -> Msg -> Int -> [String]
splitMsg w xs m
| w <= m = [xs]
| w >= length xs = [xs]
| otherwise =
let (pre, post) = splitAt (w m) xs
(ppre, ppost) = break (`elem` " .,:;!?") $ reverse pre
rpost = dropWhile isSpace ppost
in if L.null rpost
then pre : splitMsg w post m
else reverse rpost : splitMsg w (reverse ppre ++ post) m
padMsg :: Int -> String -> String
padMsg w xs =
case L.reverse xs of
[] -> xs
' ' : _ -> xs
_ | w == length xs -> xs
reversed -> L.reverse $ ' ' : reversed