Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created March 15, 2016 15:40
Show Gist options
  • Save tfausak/52e4560cbe175a84fd30 to your computer and use it in GitHub Desktop.
Save tfausak/52e4560cbe175a84fd30 to your computer and use it in GitHub Desktop.
Pretty print YAML with Haskell.
#!/usr/bin/env stack
-- stack --install-ghc --resolver nightly runghc --package bytestring --package yaml -- -Wall
import qualified Control.Exception as X
import qualified Data.ByteString.Char8 as B
import qualified Data.Yaml as Y
import qualified Data.Yaml.Pretty as Y
import qualified System.Environment as E
import qualified System.IO as IO
main :: IO ()
main = do
files <- E.getArgs
mapM_ safePrettyPrint files
safePrettyPrint :: FilePath -> IO ()
safePrettyPrint file = X.catch (prettyPrint file) (handleException file)
prettyPrint :: FilePath -> IO ()
prettyPrint file = do
input <- Y.decodeFile file
let output = Y.encodePretty config (input :: Maybe Y.Object)
B.writeFile file output
config :: Y.Config
config = Y.setConfCompare compare Y.defConfig
handleException :: FilePath -> Y.ParseException -> IO ()
handleException file exception = do
let message = concat
[ file
, ": "
, Y.prettyPrintParseException exception
]
IO.hPutStrLn IO.stderr message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment