module TDisplay.Output.Raw where import TDisplay import System.IO.Unsafe {- | The TDisplayRaw object is intended for debugging TDisplay output. The TDisplayRaw object simply displays all TDOutput with the attendant TDTag information and without any other formatting. It is implemented via unsafePerformIO to avoid requiring render to be a Monad. The \"done\" value of TDisplayRaw is used to cause Haskell to evaluate the rendering method, as well as providing the return status from renderComplete. -} data TDisplayRaw = TDRaw { done::Bool } instance Show TDisplayRaw where show d = if done d then "" else "" instance TDisplay TDisplayRaw where render e _ = unsafePerformIO $ mapM_ (render' "") e >> return (TDRaw True) where render' p x | isTDGroup x = subrender p x | otherwise = putStr p >> putStrLn (show x) subrender p (TDGroup t c) = grprender p "TDGroup" t c subrender p (TDHsepGroup t c) = grprender p "TDHsepGroup" t c subrender p (TDVsepGroup t c) = grprender p "TDVsepGroup" t c subrender _ _ = undefined grprender p n t c = putStr p >> putStr n >> putStrLn (show t) >> mapM_ (render' (p ++ " ")) (either id (\s -> s []) c) renderComplete = done {- | runTDisplayIO is used to perform the necessary rendering in an IO Monad (e.g. main). It will render its argument until the display object returns true (for TDisplayRaw, this is usually a single rendering pass), and then output a completion message. The completion message is usually the empty string, indicating that the rendering has completed, but it may be an incomplete message if the renderComplete does not return true. The TDisplayRaw does not require complex, multi-pass rendering, but the variable result value causes Haskell to perform the rendering call to determine the value to pass (via show) to the final putStr IO monad operation, thereby ensuring the render occurs. -} runTDisplayIO :: [TDOutput] -> IO () runTDisplayIO x = return (until renderComplete (render x) (TDRaw False)) >>= putStrLn . show