summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorChris Done <chrisdone@gmail.com>2012-06-12 09:44:22 +0200
committerChris Done <chrisdone@gmail.com>2012-06-12 09:44:22 +0200
commit01018bc9b3d142fd22aaadc633ca58e98379f9ae (patch)
treec91c2cf6cd6080f7bf28e7ba04979388e6c062f7 /src
parent2204fdd63aa79b9a75a080dfb19990838444d029 (diff)
downloadstylish-haskell-01018bc9b3d142fd22aaadc633ca58e98379f9ae.tar.gz
In-place writing of a file
Diffstat (limited to 'src')
-rw-r--r--src/Main.hs9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/Main.hs b/src/Main.hs
index eb331cc..7a33530 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -11,6 +11,8 @@ import Data.List (intercalate)
import Data.Maybe (listToMaybe)
import Data.Version (Version(..))
import System.Console.CmdArgs
+import System.IO.Strict (readFile)
+import Prelude hiding (readFile)
--------------------------------------------------------------------------------
@@ -26,6 +28,7 @@ data StylishArgs = StylishArgs
{ config :: Maybe FilePath
, verbose :: Bool
, defaults :: Bool
+ , inPlace :: Bool
, files :: [FilePath]
} deriving (Data, Show, Typeable)
@@ -36,6 +39,7 @@ stylishArgs = StylishArgs
{ config = Nothing &= typFile &= help "Configuration file"
, verbose = False &= help "Run in verbose mode"
, defaults = False &= help "Dump default config and exit"
+ , inPlace = False &= help "Overwrite the given files in place"
, files = [] &= typFile &= args
} &= summary ("stylish-haskell-" ++ versionString version)
where
@@ -59,7 +63,10 @@ stylishHaskell sa
let steps = configSteps conf
forM_ steps $ \s -> verbose' $ "Enabled " ++ stepName s ++ " step"
contents <- maybe getContents readFile filePath
- putStr $ unlines $ runSteps filePath steps $ lines contents
+ write $ unlines $ runSteps filePath steps $ lines contents
where
verbose' = makeVerbose (verbose sa)
filePath = listToMaybe $ files sa
+ write = maybe putStr
+ (if inPlace sa then writeFile else const putStr)
+ filePath