fork download
  1.  
  2. import Data.List
  3.  
  4. import Magic
  5.  
  6. import System.Directory
  7.  
  8.  
  9.  
  10. magic = magicOpen [MagicMimeType]
  11.  
  12.  
  13.  
  14. getMimeType :: [FilePath] -> IO [String]
  15.  
  16. getMimeType fps = magic >>= (\m -> magicLoadDefault m >> mapM (magicFile m) fps)
  17.  
  18.  
  19.  
  20. groupByMime :: [FilePath] -> IO [[(FilePath, String)]]
  21.  
  22. groupByMime fps = do
  23.  
  24. mimes <- getMimeType fps
  25.  
  26. let zipped = zip fps mimes
  27.  
  28. srted = sortBy (\(_, m0) (_, m1) -> compare m0 m1) zipped
  29.  
  30. grped = groupBy (\(_, m0) (_, m1) -> m0 == m1) srted
  31.  
  32. return grped
  33.  
  34.  
  35.  
  36. ls :: FilePath -> IO [FilePath]
  37.  
  38. ls fp = listDirectory fp >>= (\list -> return $ map ((fp ++ "/") ++) list)
  39.  
  40.  
  41.  
  42. fillTo :: Int -> String -> String
  43.  
  44. fillTo n xs
  45.  
  46. | xsLen >= n = xs
  47.  
  48. | otherwise = xs ++ (replicate (n - xsLen) ' ')
  49.  
  50. where
  51.  
  52. xsLen = length xs
  53.  
  54.  
  55.  
  56. main =
  57.  
  58. ls "/proc" >>= groupByMime >>=
  59.  
  60.  
  61. (\list ->
  62.  
  63. let (sh, hd) = splitAt 10 list
  64.  
  65. in putStrLn (fillTo 30 "path" ++ "type") >>
  66.  
  67. mapM_ (\x -> putStrLn $ fillTo 30 (fst x) ++ (snd x)) sh >>
  68.  
  69. putStrLn (fillTo 30 (show $ length hd) ++ "omited.\n"))
  70.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:4:1: error:
    Could not find module ‘Magic’
    Use -v to see a list of the files searched for.
  |
4 | import Magic
  | ^^^^^^^^^^^^
stdout
Standard output is empty