Skip to content

Instantly share code, notes, and snippets.

@MichaelBurge
Created September 15, 2017 09:09
Show Gist options
  • Save MichaelBurge/1cc8e45ac021729c133fd42155a6284f to your computer and use it in GitHub Desktop.
Save MichaelBurge/1cc8e45ac021729c133fd42155a6284f to your computer and use it in GitHub Desktop.
Haskell -- diff anything
import System.IO.Temp
import System.IO.Unsafe
import System.Process
import Data.Text
debugDiffIO :: Show a => a -> a -> IO (Maybe Text)
debugDiffIO x y = do
xf <- writeSystemTempFile "x" $ show x
yf <- writeSystemTempFile "y" $ show y
(exitCode, result, _) <- readProcessWithExitCode "git" ["diff", "--no-index", "--color-words=.", xf, yf] ""
case exitCode of
ExitSuccess -> return Nothing
ExitFailure n -> return $ Just $ pack result
debugDiff' :: Show a => a -> a -> Maybe Text
debugDiff' x y = unsafePerformIO $ debugDiffIO x y
debugDiff x y = fromMaybe "" $ debugDiff' x y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment