packages feed

kibro-0.0: executable/Utils.hs

module Utils where

import Data.Validate
import System.FilePath
import Control.Applicative
import System.Process
import System.FilePath
import System.Exit
import Directory
import Data.List

------------------------------------------------------------------------------
-- Output helpers

todo :: String -> IO ()
todo = putStrLn . ("TODO: "++)

doing :: String -> IO ()
doing = putStrLn . (++" ...")

appError :: String -> IO ()
appError err = error err

------------------------------------------------------------------------------
-- Input validation

-- | With a valid value, do an action, otherwise throw an error.
(.->.) :: Str e => a -> Test e a b -> IO () -> IO ()
(val .->. test) thunk =
    case validate test val of
      Just errs -> appError $ "invalid: " ++ (concat $ map strToString errs)
      Nothing   -> thunk

-- | Make a platform-independant path separator
(./.) :: String -> String -> FilePath
a ./. b = a ++ [pathSeparator] ++ b
infixl 9 ./.

inKibro :: (String -> String -> IO ()) -> IO ()
inKibro m = do
  dir <- getCurrentDirectory
  let name = dirName dir
  inKibro <- any (any (==".kibro") . tails) <$> getDirectoryContents dir
  if inKibro
     then m dir name
     else appError "not in a Kibro directory"

dirName :: String -> String
dirName = reverse . takeWhile (/=pathSeparator) . reverse

-- TODO: use something cross platform.. no idea why I can't find one like
-- this already. "~_~
deleteFile = runCommand . ("rm " ++)