module Text.Show.Text.System.IO (
showbHandle
, showbIOMode
, showbBufferModePrec
, showbHandlePosn
, showbSeekMode
#if MIN_VERSION_base(4,3,0)
, showbTextEncoding
#endif
#if MIN_VERSION_base(4,4,0)
, showbCodingProgress
, showbCodingFailureMode
#endif
, showbNewline
, showbNewlineModePrec
) where
import Data.Text.Lazy.Builder (Builder, fromString)
#if MIN_VERSION_base(4,3,0)
import GHC.IO.Encoding.Types (TextEncoding(..))
#endif
#if MIN_VERSION_base(4,4,0)
import GHC.IO.Encoding.Failure (CodingFailureMode(..))
import GHC.IO.Encoding.Types (CodingProgress(..))
#endif
import GHC.IO.Handle (HandlePosn(..))
import GHC.IO.Handle.Types (Handle(..))
import GHC.Show (appPrec, appPrec1)
import Prelude hiding (Show)
import System.IO (BufferMode(..), IOMode(..), Newline(..),
NewlineMode(..), SeekMode(..))
import Text.Show.Text.Class (Show(showb, showbPrec), showbParen)
import Text.Show.Text.Data.Integral (showbIntegerPrec)
import Text.Show.Text.Data.Maybe (showbMaybePrec)
import Text.Show.Text.Utils ((<>), s)
showbHandle :: Handle -> Builder
showbHandle (FileHandle file _) = showbHandleFilePath file
showbHandle (DuplexHandle file _ _) = showbHandleFilePath file
showbHandleFilePath :: FilePath -> Builder
showbHandleFilePath file = "{handle: " <> fromString file <> s '}'
showbIOMode :: IOMode -> Builder
showbIOMode ReadMode = "ReadMode"
showbIOMode WriteMode = "WriteMode"
showbIOMode AppendMode = "AppendMode"
showbIOMode ReadWriteMode = "ReadWriteMode"
showbBufferModePrec :: Int -> BufferMode -> Builder
showbBufferModePrec _ NoBuffering = "NoBuffering"
showbBufferModePrec _ LineBuffering = "LineBuffering"
showbBufferModePrec p (BlockBuffering size)
= showbParen (p > appPrec) $ "BlockBuffering " <> showbMaybePrec appPrec1 size
showbHandlePosn :: HandlePosn -> Builder
showbHandlePosn (HandlePosn h pos)
= showbHandle h <> " at position " <> showbIntegerPrec 0 pos
showbSeekMode :: SeekMode -> Builder
showbSeekMode AbsoluteSeek = "AbsoluteSeek"
showbSeekMode RelativeSeek = "RelativeSeek"
showbSeekMode SeekFromEnd = "SeekFromEnd"
#if MIN_VERSION_base(4,3,0)
showbTextEncoding :: TextEncoding -> Builder
showbTextEncoding = fromString . textEncodingName
#endif
#if MIN_VERSION_base(4,4,0)
showbCodingProgress :: CodingProgress -> Builder
showbCodingProgress InputUnderflow = "InputUnderflow"
showbCodingProgress OutputUnderflow = "OutputUnderflow"
showbCodingProgress InvalidSequence = "InvalidSequence"
showbCodingFailureMode :: CodingFailureMode -> Builder
showbCodingFailureMode ErrorOnCodingFailure = "ErrorOnCodingFailure"
showbCodingFailureMode IgnoreCodingFailure = "IgnoreCodingFailure"
showbCodingFailureMode TransliterateCodingFailure = "TransliterateCodingFailure"
showbCodingFailureMode RoundtripFailure = "RoundtripFailure"
#endif
showbNewline :: Newline -> Builder
showbNewline LF = "LF"
showbNewline CRLF = "CRLF"
showbNewlineModePrec :: Int -> NewlineMode -> Builder
showbNewlineModePrec p (NewlineMode inl onl) = showbParen (p > appPrec) $
"NewlineMode {inputNL = "
<> showbNewline inl
<> ", outputNL = "
<> showbNewline onl
<> s '}'
instance Show Handle where
showb = showbHandle
instance Show IOMode where
showb = showbIOMode
instance Show BufferMode where
showbPrec = showbBufferModePrec
instance Show HandlePosn where
showb = showbHandlePosn
instance Show SeekMode where
showb = showbSeekMode
#if MIN_VERSION_base(4,3,0)
instance Show TextEncoding where
showb = showbTextEncoding
#endif
#if MIN_VERSION_base(4,4,0)
instance Show CodingProgress where
showb = showbCodingProgress
instance Show CodingFailureMode where
showb = showbCodingFailureMode
#endif
instance Show Newline where
showb = showbNewline
instance Show NewlineMode where
showbPrec = showbNewlineModePrec