split GUI into several modules
[hs-pgms.git] / src / Util.hs
blobf58b6cc85a35b3a5fd970683eaa28c1356f57b69
1 module Util (
2 findFile,
3 formatString
4 ) where
6 import Paths_mine
7 import System.Directory
8 import Data.Char
9 import Data.List
11 findFile :: FilePath -> IO FilePath
12 findFile name = do
13 let scan :: [IO FilePath] -> IO FilePath
14 scan [] = error $ "Couldn't find file '" ++ name ++ "'"
15 scan (c:cs) = do
16 f <- c
17 b <- doesFileExist f
18 if b then return f else scan cs
19 scan [getDataFileName name,
20 return $ "data/" ++ name,
21 return $ "../data/" ++ name,
22 return $ name]
24 formatString :: String -> String
25 formatString = unlines . concatMap (block . (++" ")) . intersperse "" . lines
26 where
27 block "" = []
28 block text = let
29 (chunk, rest) = splitAt 78 text
30 (end', start') = span (not . isSpace) (reverse chunk)
32 if null start' then chunk : block (dropWhile isSpace rest)
33 else reverse (dropWhile isSpace start')
34 : block (reverse end' ++ rest)