summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2014-04-30 11:52:19 +0200
committerJasper Van der Jeugt <jaspervdj@gmail.com>2014-04-30 11:52:19 +0200
commitea86be4a445fc07f0ba882687fd40cca5adfe831 (patch)
treeaa51217de69431f9bca35eadab95385ab8dce0df
parent061225f27c622473cd9f01d6655e64b76eafc750 (diff)
parentee897de52463ce4f4998e6668d0d115d216b9ce5 (diff)
downloadstylish-haskell-ea86be4a445fc07f0ba882687fd40cca5adfe831.tar.gz
Merge pull request #61 from danbst/master
Make UTF read file mode default (fixes Windows file reading behavior)
-rw-r--r--src/Main.hs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/Main.hs b/src/Main.hs
index f97d45d..203ab52 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -9,10 +9,9 @@ module Main
import Control.Monad (forM_)
import Data.List (intercalate)
import Data.Version (Version(..))
-import Prelude hiding (readFile)
import System.Console.CmdArgs
-import System.IO (hPutStrLn, stderr)
-import System.IO.Strict (readFile)
+import System.IO (hPutStrLn, stderr, withFile, hSetEncoding, IOMode(ReadMode), utf8)
+import System.IO.Strict (hGetContents)
--------------------------------------------------------------------------------
@@ -53,7 +52,7 @@ stylishHaskell sa
| defaults sa = do
fileName <- defaultConfigFilePath
verbose' $ "Dumping config from " ++ fileName
- readFile fileName >>= putStr
+ readUTF8File fileName >>= putStr
| otherwise = do
conf <- loadConfig verbose' (config sa)
let steps = configSteps conf
@@ -70,12 +69,22 @@ stylishHaskell sa
-- | Processes a single file, or stdin if no filepath is given
file :: StylishArgs -> Config -> Maybe FilePath -> IO ()
file sa conf mfp = do
- contents <- maybe getContents readFile mfp
+ contents <- maybe getContents readUTF8File mfp
let result = runSteps (configLanguageExtensions conf)
mfp (configSteps conf) $ lines contents
-
case result of
- Left err -> hPutStrLn stderr err >> write contents
- Right ok -> write $ unlines ok
+ Left err -> hPutStrLn stderr err >> write contents contents
+ Right ok -> write contents $ unlines ok
where
- write = maybe putStr (if inPlace sa then writeFile else const putStr) mfp
+ write old new = case mfp of
+ Nothing -> putStr new
+ Just _ | not (inPlace sa) -> putStr new
+ Just path | length new /= 0 && old /= new -> writeFile path new
+ _ -> return ()
+
+readUTF8File :: FilePath -> IO String
+readUTF8File fp =
+ withFile fp ReadMode $ \h -> do
+ hSetEncoding h utf8
+ content <- hGetContents h
+ return content