module Debug.Diff.Config
( Config(..)
, defConfig
, diffWith
, diff
) where
import Text.Groom
import Text.Printf
import System.IO
import System.IO.Temp
import System.Process
import System.Exit
data Config = Config
{ context :: Maybe Int
, command :: String
, args :: [String]
} deriving (Eq, Ord, Read, Show)
defConfig :: Config
defConfig = Config
{ context = Just 3
, command = "colordiff"
, args = [] }
diffWith :: (Show a, Show b) => Config -> a -> b -> IO ()
diffWith cfg x y =
withSystemTempFile "ddiff_x" $ \px hx ->
withSystemTempFile "ddiff_y" $ \py hy -> do
hPutStrLn hx (groom x)
hClose hx
hPutStrLn hy (groom y)
hClose hy
let ctxArg n = ["-U", show n]
allArgs = args cfg ++ maybe [] ctxArg (context cfg) ++ [px, py]
(_, _, _, hdl) <- createProcess (proc (command cfg) allArgs)
ec <- waitForProcess hdl
case ec of
ExitFailure n | n > 1 ->
hPrintf stderr
"debug-diff: command %s with args %s exited with code %d\n"
(show (command cfg)) (show allArgs) n
_ -> return ()
diff :: (Show a, Show b) => a -> b -> IO ()
diff = diffWith defConfig