前に Go言語で 「すべてのディレクトリで git statusする」 という記事を書いた。
今度は、 haskell で同じようなのを書いてみた。
haskell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import System.IO
import System.Process
import System.Directory
import Control.Exception
import System.FilePath
getAllFiles :: FilePath -> IO [String]
getAllFiles dir = do
contents <- getDirectoryContents dir `catch` (\(SomeException e) -> const (return []) e)
let contents' = [dir </> path | path <- contents, notElem path [".", ".."]]
contents'' <- mapM getAllFiles contents'
return $ contents' ++ concat contents''
takeLast list n = reverse . take n $ reverse list
join :: String -> [String] -> String
join sep [] = []
join sep [x] = x
join sep (x:xs) = x ++ sep ++ join sep xs
onlyGitDir :: FilePath -> Bool
onlyGitDir fileName =
if last == ".git"
then True
else False
where last = takeLast fileName 4
gitStatus :: FilePath -> IO String
gitStatus path = do
setCurrentDirectory path
readProcess "git" ["status"] ""
printGitStatus :: FilePath -> IO ()
printGitStatus path = do
putStrLn "--------------------------------------------------------------------------------"
putStrLn $ "* " ++ path
putStrLn "--------------------------------------------------------------------------------"
gitStatus path >>= putStrLn
return ()
main = do
path <- getCurrentDirectory
files <- getAllFiles path
let dirs = map takeDirectory $ filter onlyGitDir files
mapM printGitStatus dirs
return ()haskell は勉強始めたばっかだからいろいろ改善の余地があると思われる。 詳しい人教えてくだしあ