{-# LANGUAGE Safe #-}

{- |
    Module      :  Text.Show.SDP
    Copyright   :  (c) Andrey Mulik 2019
    License     :  BSD-style
    Maintainer  :  work.a.mulik@gmail.com
    Portability :  portable
    
    "Text.Show.SDP" provides common 'ShowS' stuff.
-}
module Text.Show.SDP
(
  -- * Common show templates
  assocsPrec, showsRaw, showsRawLinear
)
where

import Prelude ()
import SDP.SafePrelude
import SDP.Indexed

import GHC.Show ( appPrec )

default ()

--------------------------------------------------------------------------------

-- | 'assocsPrec' is 'showsPrec' template.
assocsPrec :: (Indexed v i e, Show i, Show e) => String -> Int -> v -> ShowS
assocsPrec :: String -> Int -> v -> ShowS
assocsPrec String
name = \ Int
p v
es -> Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
name
                                                    ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (i, i) -> ShowS
forall a. Show a => a -> ShowS
shows (v -> (i, i)
forall b i. Bordered b i => b -> (i, i)
bounds v
es)
                                                    ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> ShowS
showChar Char
' '
                                                    ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [(i, e)] -> ShowS
forall a. Show a => a -> ShowS
shows (v -> [(i, e)]
forall map key e. Map map key e => map -> [(key, e)]
assocs v
es)

{- |
  'showsRaw' is a primitive list-to-string conversion pattern.
  
  Note that attempting to parse the resulting string with standard @ReadS@-based
  functions will cause an error (ambiguous parse). To properly parse a string,
  use the @readRawSequence@ function from the "SDP.Text.Read" module.
-}
showsRaw :: (Show e) => Int -> [e] -> ShowS
showsRaw :: Int -> [e] -> ShowS
showsRaw Int
_    []    = ShowS
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
showsRaw Int
p (e
x : [e]
xs) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec) ShowS
stream
  where
    stream :: ShowS
stream = e -> ShowS
forall a. Show a => a -> ShowS
shows e
x ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (e -> ShowS -> ShowS) -> ShowS -> [e] -> ShowS
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ e
e ShowS
rest -> Char -> ShowS
showChar Char
' ' ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. e -> ShowS
forall a. Show a => a -> ShowS
shows e
e ShowS -> ShowS -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ShowS
rest) ShowS
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id [e]
xs

-- | Just 'showsRaw' version for 'Linear'.
showsRawLinear :: (Linear l e, Show e) => Int -> l -> ShowS
showsRawLinear :: Int -> l -> ShowS
showsRawLinear Int
p = Int -> [e] -> ShowS
forall e. Show e => Int -> [e] -> ShowS
showsRaw Int
p ([e] -> ShowS) -> (l -> [e]) -> l -> ShowS
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. l -> [e]
forall l e. Linear l e => l -> [e]
listL