hledger/hledger-lib/Hledger/Data/Utils.hs

365 lines
12 KiB
Haskell
Raw Normal View History

{-# LANGUAGE CPP #-}
{-|
Standard imports and utilities which are useful everywhere, or needed low
in the module hierarchy. This is the bottom of hledger's module graph.
-}
2010-05-20 03:08:53 +04:00
module Hledger.Data.Utils (
2010-03-09 04:51:21 +03:00
module Data.Char,
2008-10-10 15:52:15 +04:00
module Control.Monad,
module Data.List,
2008-10-10 06:19:53 +04:00
--module Data.Map,
module Data.Maybe,
2008-10-10 06:19:53 +04:00
module Data.Ord,
module Data.Tree,
2008-11-22 15:18:19 +03:00
module Data.Time.Clock,
module Data.Time.Calendar,
module Data.Time.LocalTime,
module Debug.Trace,
2010-05-20 03:08:53 +04:00
module Hledger.Data.Utils,
2008-10-10 06:19:53 +04:00
module Text.Printf,
module Text.RegexPR,
module Test.HUnit,
)
where
2010-03-09 04:51:21 +03:00
import Data.Char
2010-09-05 22:17:24 +04:00
import Codec.Binary.UTF8.String as UTF8 (decodeString, encodeString, isUTF8Encoded)
import Control.Exception
2008-10-10 15:52:15 +04:00
import Control.Monad
2007-02-16 14:51:30 +03:00
import Data.List
2008-10-10 06:19:53 +04:00
--import qualified Data.Map as Map
import Data.Maybe
2008-10-10 06:19:53 +04:00
import Data.Ord
import Data.Tree
2008-11-22 15:18:19 +03:00
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime
2008-10-10 06:19:53 +04:00
import Debug.Trace
#if __GLASGOW_HASKELL__ <= 610
import Prelude hiding (readFile,putStr,print)
import System.IO.UTF8
#endif
import Test.HUnit
2008-10-10 06:19:53 +04:00
import Text.Printf
import Text.RegexPR
import Text.ParserCombinators.Parsec
2010-09-05 22:17:24 +04:00
import System.Info (os)
2008-10-10 11:39:20 +04:00
2008-11-22 12:06:44 +03:00
-- strings
2008-11-27 09:32:31 +03:00
lowercase = map toLower
uppercase = map toUpper
2009-04-10 08:59:43 +04:00
strip = lstrip . rstrip
lstrip = dropws
rstrip = reverse . dropws . reverse
dropws = dropWhile (`elem` " \t")
2008-11-27 00:11:44 +03:00
2008-10-15 23:14:34 +04:00
elideLeft width s =
2009-09-23 13:29:31 +04:00
if length s > width then ".." ++ reverse (take (width - 2) $ reverse s) else s
2008-10-15 23:14:34 +04:00
elideRight width s =
2009-09-23 13:29:31 +04:00
if length s > width then take (width - 2) s ++ ".." else s
2008-10-15 23:14:34 +04:00
underline :: String -> String
underline s = s' ++ replicate (length s) '-' ++ "\n"
where s'
| last s == '\n' = s
| otherwise = s ++ "\n"
unbracket :: String -> String
unbracket s
| (head s == '[' && last s == ']') || (head s == '(' && last s == ')') = init $ tail s
| otherwise = s
2008-11-22 12:06:44 +03:00
-- | Join multi-line strings as side-by-side rectangular strings of the same height, top-padded.
concatTopPadded :: [String] -> String
concatTopPadded strs = intercalate "\n" $ map concat $ transpose padded
where
lss = map lines strs
h = maximum $ map length lss
ypad ls = replicate (difforzero h (length ls)) "" ++ ls
xpad ls = map (padleft w) ls where w | null ls = 0
| otherwise = maximum $ map length ls
padded = map (xpad . ypad) lss
-- | Join multi-line strings as side-by-side rectangular strings of the same height, bottom-padded.
concatBottomPadded :: [String] -> String
concatBottomPadded strs = intercalate "\n" $ map concat $ transpose padded
where
lss = map lines strs
h = maximum $ map length lss
ypad ls = ls ++ replicate (difforzero h (length ls)) ""
xpad ls = map (padleft w) ls where w | null ls = 0
| otherwise = maximum $ map length ls
padded = map (xpad . ypad) lss
-- | Compose strings vertically and right-aligned.
vConcatRightAligned :: [String] -> String
vConcatRightAligned ss = intercalate "\n" $ map showfixedwidth ss
where
showfixedwidth = printf (printf "%%%ds" width)
width = maximum $ map length ss
2008-11-22 12:06:44 +03:00
-- | Convert a multi-line string to a rectangular string top-padded to the specified height.
padtop :: Int -> String -> String
padtop h s = intercalate "\n" xpadded
where
ls = lines s
sh = length ls
sw | null ls = 0
| otherwise = maximum $ map length ls
ypadded = replicate (difforzero h sh) "" ++ ls
xpadded = map (padleft sw) ypadded
-- | Convert a multi-line string to a rectangular string bottom-padded to the specified height.
padbottom :: Int -> String -> String
padbottom h s = intercalate "\n" xpadded
where
ls = lines s
sh = length ls
sw | null ls = 0
| otherwise = maximum $ map length ls
ypadded = ls ++ replicate (difforzero h sh) ""
xpadded = map (padleft sw) ypadded
-- | Convert a multi-line string to a rectangular string left-padded to the specified width.
padleft :: Int -> String -> String
padleft w "" = concat $ replicate w " "
padleft w s = intercalate "\n" $ map (printf (printf "%%%ds" w)) $ lines s
-- | Convert a multi-line string to a rectangular string right-padded to the specified width.
padright :: Int -> String -> String
padright w "" = concat $ replicate w " "
padright w s = intercalate "\n" $ map (printf (printf "%%-%ds" w)) $ lines s
2008-12-06 10:15:19 +03:00
-- | Clip a multi-line string to the specified width and height from the top left.
cliptopleft :: Int -> Int -> String -> String
2009-09-22 19:56:59 +04:00
cliptopleft w h = intercalate "\n" . take h . map (take w) . lines
2008-12-06 10:15:19 +03:00
-- | Clip and pad a multi-line string to fill the specified width and height.
fitto :: Int -> Int -> String -> String
fitto w h s = intercalate "\n" $ take h $ rows ++ repeat blankline
where
rows = map (fit w) $ lines s
2009-09-22 19:56:59 +04:00
fit w = take w . (++ repeat ' ')
2008-12-06 10:15:19 +03:00
blankline = replicate w ' '
2010-09-05 22:17:24 +04:00
-- encoded platform strings
-- | A platform string is a string value from or for the operating system,
-- such as a file path or command-line argument (or environment variable's
-- name or value ?). On some platforms (such as unix) these are not real
-- unicode strings but have some encoding such as UTF-8. This alias does
-- no type enforcement but aids code clarity.
type PlatformString = String
-- | Convert a possibly encoded platform string to a real unicode string.
-- We decode the UTF-8 encoding recommended for unix systems
-- (cf http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html)
-- and leave anything else unchanged.
fromPlatformString :: PlatformString -> String
fromPlatformString s = if UTF8.isUTF8Encoded s then UTF8.decodeString s else s
-- | Convert a unicode string to a possibly encoded platform string.
-- On unix we encode with the recommended UTF-8
-- (cf http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html)
-- and elsewhere we leave it unchanged.
toPlatformString :: String -> PlatformString
toPlatformString = case os of
"unix" -> UTF8.encodeString
"linux" -> UTF8.encodeString
"darwin" -> UTF8.encodeString
_ -> id
2008-11-22 12:06:44 +03:00
-- math
difforzero :: (Num a, Ord a) => a -> a -> a
difforzero a b = maximum [(a - b), 0]
2008-10-09 11:08:36 +04:00
-- regexps
containsRegex :: String -> String -> Bool
containsRegex r s = case matchRegexPR ("(?i)"++r) s of
2008-10-09 11:08:36 +04:00
Just _ -> True
_ -> False
2008-10-09 11:08:36 +04:00
2007-07-03 03:54:17 +04:00
-- lists
splitAtElement :: Eq a => a -> [a] -> [[a]]
splitAtElement e l =
case dropWhile (e==) l of
[] -> []
l' -> first : splitAtElement e rest
where
(first,rest) = break (e==) l'
2007-07-03 03:54:17 +04:00
-- trees
root = rootLabel
2008-10-10 15:52:15 +04:00
subs = subForest
branches = subForest
2008-12-04 22:32:42 +03:00
-- | List just the leaf nodes of a tree
leaves :: Tree a -> [a]
leaves (Node v []) = [v]
leaves (Node _ branches) = concatMap leaves branches
2008-10-11 08:18:26 +04:00
-- | get the sub-tree rooted at the first (left-most, depth-first) occurrence
-- of the specified node value
subtreeat :: Eq a => a -> Tree a -> Maybe (Tree a)
subtreeat v t
| root t == v = Just t
| otherwise = subtreeinforest v $ subs t
-- | get the sub-tree for the specified node value in the first tree in
-- forest in which it occurs.
subtreeinforest :: Eq a => a -> [Tree a] -> Maybe (Tree a)
subtreeinforest _ [] = Nothing
2008-10-11 08:18:26 +04:00
subtreeinforest v (t:ts) = case (subtreeat v t) of
Just t' -> Just t'
Nothing -> subtreeinforest v ts
-- | remove all nodes past a certain depth
2007-03-11 02:05:30 +03:00
treeprune :: Int -> Tree a -> Tree a
treeprune 0 t = Node (root t) []
2007-07-02 20:43:14 +04:00
treeprune d t = Node (root t) (map (treeprune $ d-1) $ branches t)
2007-03-11 02:05:30 +03:00
2008-10-11 08:18:26 +04:00
-- | apply f to all tree nodes
treemap :: (a -> b) -> Tree a -> Tree b
treemap f t = Node (f $ root t) (map (treemap f) $ branches t)
2008-10-11 08:18:26 +04:00
-- | remove all subtrees whose nodes do not fulfill predicate
treefilter :: (a -> Bool) -> Tree a -> Tree a
treefilter f t = Node
(root t)
(map (treefilter f) $ filter (treeany f) $ branches t)
2008-10-11 08:18:26 +04:00
-- | is predicate true in any node of tree ?
treeany :: (a -> Bool) -> Tree a -> Bool
2009-09-22 15:55:11 +04:00
treeany f t = f (root t) || any (treeany f) (branches t)
-- treedrop -- remove the leaves which do fulfill predicate.
-- treedropall -- do this repeatedly.
2008-10-11 08:18:26 +04:00
-- | show a compact ascii representation of a tree
2008-10-09 16:59:05 +04:00
showtree :: Show a => Tree a -> String
2009-08-12 10:40:01 +04:00
showtree = unlines . filter (containsRegex "[^ \\|]") . lines . drawTree . treemap show
2008-10-09 16:59:05 +04:00
2008-10-11 08:18:26 +04:00
-- | show a compact ascii representation of a forest
showforest :: Show a => Forest a -> String
showforest = concatMap showtree
2007-07-02 20:43:14 +04:00
-- debugging
2008-12-10 10:08:50 +03:00
-- | trace (print on stdout at runtime) a showable expression
-- (for easily tracing in the middle of a complex expression)
strace :: Show a => a -> a
2008-10-11 08:18:26 +04:00
strace a = trace (show a) a
2009-03-15 06:58:43 +03:00
-- | labelled trace - like strace, with a newline and a label prepended
ltrace :: Show a => String -> a -> a
2009-04-01 08:57:05 +04:00
ltrace l a = trace (l ++ ": " ++ show a) a
2008-10-11 08:18:26 +04:00
2009-06-05 14:41:57 +04:00
-- | trace an expression using a custom show function
tracewith f e = trace (f e) e
-- parsing
2010-04-15 01:37:03 +04:00
choice' = choice . map Text.ParserCombinators.Parsec.try
parsewith :: Parser a -> String -> Either ParseError a
2009-09-22 19:56:59 +04:00
parsewith p = parse p ""
2009-06-20 07:59:37 +04:00
parseWithCtx :: b -> GenParser Char b a -> String -> Either ParseError a
2009-09-22 19:56:59 +04:00
parseWithCtx ctx p = runParser p ctx ""
2009-06-20 07:59:37 +04:00
fromparse :: Either ParseError a -> a
2010-03-09 21:33:26 +03:00
fromparse = either parseerror id
parseerror e = error $ showParseError e
showParseError e = "parse error at " ++ show e
showDateParseError e = printf "date parse error (%s)" (intercalate ", " $ tail $ lines $ show e)
2008-10-11 08:18:26 +04:00
nonspace :: GenParser Char st Char
nonspace = satisfy (not . isSpace)
spacenonewline :: GenParser Char st Char
2009-09-22 19:56:59 +04:00
spacenonewline = satisfy (`elem` " \v\f\t")
restofline :: GenParser Char st String
restofline = anyChar `manyTill` newline
2008-10-11 08:18:26 +04:00
2009-01-25 09:47:05 +03:00
-- time
getCurrentLocalTime :: IO LocalTime
getCurrentLocalTime = do
t <- getCurrentTime
tz <- getCurrentTimeZone
return $ utcToLocalTime tz t
-- testing
-- | Get a Test's label, or the empty string.
tname :: Test -> String
tname (TestLabel n _) = n
tname _ = ""
-- | Flatten a Test containing TestLists into a list of single tests.
tflatten :: Test -> [Test]
tflatten (TestLabel _ t@(TestList _)) = tflatten t
tflatten (TestList ts) = concatMap tflatten ts
tflatten t = [t]
-- | Filter TestLists in a Test, recursively, preserving the structure.
tfilter :: (Test -> Bool) -> Test -> Test
tfilter p (TestLabel l ts) = TestLabel l (tfilter p ts)
tfilter p (TestList ts) = TestList $ filter (any p . tflatten) $ map (tfilter p) ts
tfilter _ t = t
-- | Simple way to assert something is some expected value, with no label.
is :: (Eq a, Show a) => a -> a -> Assertion
a `is` e = assertEqual "" e a
2010-03-10 22:48:46 +03:00
-- | Assert a parse result is successful, printing the parse error on failure.
assertParse :: (Either ParseError a) -> Assertion
assertParse parse = either (assertFailure.show) (const (return ())) parse
-- | Assert a parse result is some expected value, printing the parse error on failure.
assertParseEqual :: (Show a, Eq a) => (Either ParseError a) -> a -> Assertion
assertParseEqual parse expected = either (assertFailure.show) (`is` expected) parse
printParseError :: (Show a) => a -> IO ()
printParseError e = do putStr "parse error at "; print e
-- misc
isLeft :: Either a b -> Bool
isLeft (Left _) = True
isLeft _ = False
isRight :: Either a b -> Bool
isRight = not . isLeft
strictReadFile :: FilePath -> IO String
strictReadFile f = readFile f >>= \s -> Control.Exception.evaluate (length s) >> return s
-- -- | Expand ~ in a file path (does not handle ~name).
-- tildeExpand :: FilePath -> IO FilePath
-- tildeExpand ('~':[]) = getHomeDirectory
-- tildeExpand ('~':'/':xs) = getHomeDirectory >>= return . (++ ('/':xs))
-- --handle ~name, requires -fvia-C or ghc 6.8:
-- --import System.Posix.User
-- -- tildeExpand ('~':xs) = do let (user, path) = span (/= '/') xs
-- -- pw <- getUserEntryForName user
-- -- return (homeDirectory pw ++ path)
-- tildeExpand xs = return xs