diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-06-01 20:39:28 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-06-01 20:39:28 +0200 |
commit | 90f18cb6899aaddcbce4b1afeb0e1ca5c3102fcf (patch) | |
tree | 146c02510e84aaa9b8cfa3bb4aa18f413a09a793 /src | |
parent | 5c2f59852e88811791955c8582fb67ad9e625fdd (diff) | |
download | stylish-haskell-90f18cb6899aaddcbce4b1afeb0e1ca5c3102fcf.tar.gz |
Add an --utf8 flag
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.hs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/Main.hs b/src/Main.hs index 203ab52..820d814 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,12 +6,12 @@ module Main -------------------------------------------------------------------------------- -import Control.Monad (forM_) -import Data.List (intercalate) -import Data.Version (Version(..)) +import Control.Monad (forM_, when) +import Data.List (intercalate) +import Data.Version (Version (..)) import System.Console.CmdArgs -import System.IO (hPutStrLn, stderr, withFile, hSetEncoding, IOMode(ReadMode), utf8) -import System.IO.Strict (hGetContents) +import qualified System.IO as IO +import System.IO.Strict (hGetContents) -------------------------------------------------------------------------------- @@ -24,6 +24,7 @@ data StylishArgs = StylishArgs , verbose :: Bool , defaults :: Bool , inPlace :: Bool + , utf8 :: Bool , files :: [FilePath] } deriving (Data, Show, Typeable) @@ -35,6 +36,7 @@ stylishArgs = StylishArgs , verbose = False &= help "Run in verbose mode" , defaults = False &= help "Dump default config and exit" , inPlace = False &= help "Overwrite the given files in place" + , utf8 = False &= help "Force UTF-8 stdin/stdout" , files = [] &= typFile &= args } &= summary ("stylish-haskell-" ++ versionString version) where @@ -48,18 +50,21 @@ main = cmdArgs stylishArgs >>= stylishHaskell -------------------------------------------------------------------------------- stylishHaskell :: StylishArgs -> IO () -stylishHaskell sa - | defaults sa = do - fileName <- defaultConfigFilePath - verbose' $ "Dumping config from " ++ fileName - readUTF8File fileName >>= putStr - | otherwise = do - conf <- loadConfig verbose' (config sa) - let steps = configSteps conf - forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step" - verbose' $ "Extra language extensions: " ++ - show (configLanguageExtensions conf) - mapM_ (file sa conf) files' +stylishHaskell sa = do + when (utf8 sa) $ + mapM_ (`IO.hSetEncoding` IO.utf8) [IO.stdin, IO.stdout] + case defaults sa of + True -> do + fileName <- defaultConfigFilePath + verbose' $ "Dumping config from " ++ fileName + readUTF8File fileName >>= putStr + False -> do + conf <- loadConfig verbose' (config sa) + let steps = configSteps conf + forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step" + verbose' $ "Extra language extensions: " ++ + show (configLanguageExtensions conf) + mapM_ (file sa conf) files' where verbose' = makeVerbose (verbose sa) files' = if null (files sa) then [Nothing] else map Just (files sa) @@ -73,7 +78,7 @@ file sa conf mfp = do let result = runSteps (configLanguageExtensions conf) mfp (configSteps conf) $ lines contents case result of - Left err -> hPutStrLn stderr err >> write contents contents + Left err -> IO.hPutStrLn IO.stderr err >> write contents contents Right ok -> write contents $ unlines ok where write old new = case mfp of @@ -84,7 +89,7 @@ file sa conf mfp = do readUTF8File :: FilePath -> IO String readUTF8File fp = - withFile fp ReadMode $ \h -> do - hSetEncoding h utf8 - content <- hGetContents h + IO.withFile fp IO.ReadMode $ \h -> do + IO.hSetEncoding h IO.utf8 + content <- IO.hGetContents h return content |