2012-03-29 23:06:31 +04:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
2008-10-03 06:04:15 +04:00
|
|
|
|
{-|
|
|
|
|
|
|
2010-05-27 07:58:47 +04:00
|
|
|
|
Standard imports and utilities which are useful everywhere, or needed low
|
|
|
|
|
in the module hierarchy. This is the bottom of hledger's module graph.
|
2008-10-03 06:04:15 +04:00
|
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
2011-05-28 08:11:44 +04:00
|
|
|
|
module Hledger.Utils (---- provide these frequently used modules - or not, for clearer api:
|
|
|
|
|
-- module Control.Monad,
|
|
|
|
|
-- module Data.List,
|
|
|
|
|
-- module Data.Maybe,
|
|
|
|
|
-- module Data.Time.Calendar,
|
|
|
|
|
-- module Data.Time.Clock,
|
|
|
|
|
-- module Data.Time.LocalTime,
|
|
|
|
|
-- module Data.Tree,
|
|
|
|
|
-- module Debug.Trace,
|
|
|
|
|
-- module Text.RegexPR,
|
|
|
|
|
-- module Test.HUnit,
|
|
|
|
|
-- module Text.Printf,
|
|
|
|
|
---- all of this one:
|
2011-08-08 05:34:00 +04:00
|
|
|
|
module Hledger.Utils,
|
2014-07-06 21:11:02 +04:00
|
|
|
|
module Hledger.Utils.Regex,
|
2012-03-29 23:06:31 +04:00
|
|
|
|
Debug.Trace.trace,
|
2014-03-20 03:11:46 +04:00
|
|
|
|
-- module Data.PPrint,
|
2012-03-29 23:06:31 +04:00
|
|
|
|
-- module Hledger.Utils.UTF8IOCompat
|
2013-03-29 22:40:10 +04:00
|
|
|
|
SystemString,fromSystemString,toSystemString,error',userError',
|
2014-03-27 23:29:34 +04:00
|
|
|
|
#if __GLASGOW_HASKELL__ >= 704
|
2013-03-29 22:40:10 +04:00
|
|
|
|
ppShow
|
2014-03-27 23:29:34 +04:00
|
|
|
|
#endif
|
2012-03-29 23:06:31 +04:00
|
|
|
|
-- the rest need to be done in each module I think
|
2011-05-28 08:11:44 +04:00
|
|
|
|
)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
where
|
2013-03-29 22:38:43 +04:00
|
|
|
|
import Control.Monad (liftM, when)
|
2012-05-30 12:36:01 +04:00
|
|
|
|
import Control.Monad.Error (MonadIO)
|
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2011-05-28 08:11:44 +04:00
|
|
|
|
import Data.Char
|
2007-02-16 14:51:30 +03:00
|
|
|
|
import Data.List
|
2012-10-21 21:18:18 +04:00
|
|
|
|
import qualified Data.Map as M
|
2014-07-06 21:11:02 +04:00
|
|
|
|
-- import Data.Maybe
|
2014-03-20 03:11:46 +04:00
|
|
|
|
-- import Data.PPrint
|
2008-11-22 15:18:19 +03:00
|
|
|
|
import Data.Time.Clock
|
2008-12-11 04:35:07 +03:00
|
|
|
|
import Data.Time.LocalTime
|
2011-05-28 08:11:44 +04:00
|
|
|
|
import Data.Tree
|
2008-10-10 06:19:53 +04:00
|
|
|
|
import Debug.Trace
|
2013-12-07 01:35:50 +04:00
|
|
|
|
import Safe (readDef)
|
2012-03-24 22:08:11 +04:00
|
|
|
|
import System.Directory (getHomeDirectory)
|
2013-12-07 01:35:50 +04:00
|
|
|
|
import System.Environment (getArgs)
|
|
|
|
|
import System.Exit
|
2012-05-30 12:36:01 +04:00
|
|
|
|
import System.FilePath((</>), isRelative)
|
2013-03-29 22:46:10 +04:00
|
|
|
|
import System.IO
|
2013-12-07 01:35:50 +04:00
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
2008-10-10 12:16:55 +04:00
|
|
|
|
import Test.HUnit
|
2011-05-28 08:11:44 +04:00
|
|
|
|
import Text.ParserCombinators.Parsec
|
2008-10-10 06:19:53 +04:00
|
|
|
|
import Text.Printf
|
2011-05-28 08:11:44 +04:00
|
|
|
|
-- import qualified Data.Map as Map
|
2014-07-06 21:11:02 +04:00
|
|
|
|
|
|
|
|
|
import Hledger.Utils.Regex
|
2012-03-29 23:06:31 +04:00
|
|
|
|
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
|
|
|
|
-- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
|
|
|
|
import Hledger.Utils.UTF8IOCompat (SystemString,fromSystemString,toSystemString,error',userError')
|
2008-10-10 11:39:20 +04:00
|
|
|
|
|
2014-03-27 23:29:34 +04:00
|
|
|
|
#if __GLASGOW_HASKELL__ >= 704
|
|
|
|
|
import Text.Show.Pretty (ppShow)
|
|
|
|
|
#else
|
|
|
|
|
-- the required pretty-show version requires GHC >= 7.4
|
|
|
|
|
ppShow :: Show a => a -> String
|
|
|
|
|
ppShow = show
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-11-22 12:06:44 +03:00
|
|
|
|
-- strings
|
|
|
|
|
|
2008-11-27 09:32:31 +03:00
|
|
|
|
lowercase = map toLower
|
|
|
|
|
uppercase = map toUpper
|
|
|
|
|
|
2014-10-21 23:02:23 +04:00
|
|
|
|
-- | Remove leading and trailing whitespace.
|
2009-04-10 08:59:43 +04:00
|
|
|
|
strip = lstrip . rstrip
|
2014-10-21 23:02:23 +04:00
|
|
|
|
|
|
|
|
|
-- | Remove leading whitespace.
|
|
|
|
|
lstrip = dropWhile (`elem` " \t") :: String -> String -- XXX isSpace ?
|
|
|
|
|
|
|
|
|
|
-- | Remove trailing whitespace.
|
2011-07-16 01:58:20 +04:00
|
|
|
|
rstrip = reverse . lstrip . reverse
|
2008-11-27 00:11:44 +03:00
|
|
|
|
|
2014-10-21 23:02:23 +04:00
|
|
|
|
-- | Remove trailing newlines/carriage returns.
|
|
|
|
|
chomp = reverse . dropWhile (`elem` "\r\n") . reverse
|
|
|
|
|
|
2013-01-11 17:54:49 +04:00
|
|
|
|
stripbrackets = dropWhile (`elem` "([") . reverse . dropWhile (`elem` "])") . reverse :: String -> String
|
2012-12-04 04:03:42 +04: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
|
|
|
|
|
2009-05-29 14:02:14 +04:00
|
|
|
|
underline :: String -> String
|
|
|
|
|
underline s = s' ++ replicate (length s) '-' ++ "\n"
|
|
|
|
|
where s'
|
|
|
|
|
| last s == '\n' = s
|
|
|
|
|
| otherwise = s ++ "\n"
|
|
|
|
|
|
2014-04-15 22:45:30 +04:00
|
|
|
|
-- | Wrap a string in double quotes, and \-prefix any embedded single
|
2011-06-05 22:34:12 +04:00
|
|
|
|
-- quotes, if it contains whitespace and is not already single- or
|
|
|
|
|
-- double-quoted.
|
|
|
|
|
quoteIfSpaced :: String -> String
|
|
|
|
|
quoteIfSpaced s | isSingleQuoted s || isDoubleQuoted s = s
|
|
|
|
|
| not $ any (`elem` s) whitespacechars = s
|
|
|
|
|
| otherwise = "'"++escapeSingleQuotes s++"'"
|
2012-05-27 22:14:20 +04:00
|
|
|
|
|
2014-04-15 22:45:30 +04:00
|
|
|
|
-- | Double-quote this string if it contains whitespace, single quotes
|
|
|
|
|
-- or double-quotes, escaping the quotes as needed.
|
|
|
|
|
quoteIfNeeded s | any (`elem` s) (quotechars++whitespacechars) = "\"" ++ escapeDoubleQuotes s ++ "\""
|
|
|
|
|
| otherwise = s
|
|
|
|
|
|
|
|
|
|
-- | Single-quote this string if it contains whitespace or double-quotes.
|
|
|
|
|
-- No good for strings containing single quotes.
|
|
|
|
|
singleQuoteIfNeeded s | any (`elem` s) whitespacechars = "'"++s++"'"
|
|
|
|
|
| otherwise = s
|
|
|
|
|
|
|
|
|
|
quotechars = "'\""
|
|
|
|
|
whitespacechars = " \t\n\r"
|
|
|
|
|
|
|
|
|
|
escapeDoubleQuotes :: String -> String
|
|
|
|
|
escapeDoubleQuotes = regexReplace "\"" "\""
|
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
|
escapeSingleQuotes :: String -> String
|
|
|
|
|
escapeSingleQuotes = regexReplace "'" "\'"
|
|
|
|
|
|
|
|
|
|
escapeQuotes :: String -> String
|
|
|
|
|
escapeQuotes = regexReplace "([\"'])" "\\1"
|
2011-06-05 22:34:12 +04:00
|
|
|
|
|
|
|
|
|
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
|
2013-02-25 00:04:28 +04:00
|
|
|
|
-- NB correctly handles "a'b" but not "''a''". Can raise an error if parsing fails.
|
2011-06-05 22:34:12 +04:00
|
|
|
|
words' :: String -> [String]
|
2013-02-25 00:04:28 +04:00
|
|
|
|
words' "" = []
|
|
|
|
|
words' s = map stripquotes $ fromparse $ parsewith p s
|
2011-06-05 22:34:12 +04:00
|
|
|
|
where
|
2014-04-15 22:45:30 +04:00
|
|
|
|
p = do ss <- (singleQuotedPattern <|> doubleQuotedPattern <|> pattern) `sepBy` many1 spacenonewline
|
2011-08-15 17:58:16 +04:00
|
|
|
|
-- eof
|
|
|
|
|
return ss
|
2011-06-05 22:34:12 +04:00
|
|
|
|
pattern = many (noneOf whitespacechars)
|
2014-04-15 22:45:30 +04:00
|
|
|
|
singleQuotedPattern = between (char '\'') (char '\'') (many $ noneOf "'")
|
|
|
|
|
doubleQuotedPattern = between (char '"') (char '"') (many $ noneOf "\"")
|
2011-06-05 22:34:12 +04:00
|
|
|
|
|
2011-08-15 17:58:16 +04:00
|
|
|
|
-- | Quote-aware version of unwords - single-quote strings which contain whitespace
|
|
|
|
|
unwords' :: [String] -> String
|
2014-04-16 00:07:43 +04:00
|
|
|
|
unwords' = unwords . map quoteIfNeeded
|
2011-06-05 22:34:12 +04:00
|
|
|
|
|
|
|
|
|
-- | Strip one matching pair of single or double quotes on the ends of a string.
|
|
|
|
|
stripquotes :: String -> String
|
|
|
|
|
stripquotes s = if isSingleQuoted s || isDoubleQuoted s then init $ tail s else s
|
|
|
|
|
|
2011-08-15 02:38:51 +04:00
|
|
|
|
isSingleQuoted s@(_:_:_) = head s == '\'' && last s == '\''
|
|
|
|
|
isSingleQuoted _ = False
|
|
|
|
|
|
|
|
|
|
isDoubleQuoted s@(_:_:_) = head s == '"' && last s == '"'
|
|
|
|
|
isDoubleQuoted _ = False
|
2011-06-05 22:34:12 +04:00
|
|
|
|
|
2009-05-29 15:31:51 +04:00
|
|
|
|
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)) ""
|
2012-05-30 13:17:18 +04:00
|
|
|
|
xpad ls = map (padright w) ls where w | null ls = 0
|
|
|
|
|
| otherwise = maximum $ map length ls
|
2008-11-22 12:06:44 +03:00
|
|
|
|
padded = map (xpad . ypad) lss
|
|
|
|
|
|
2010-03-07 00:47:10 +03:00
|
|
|
|
-- | 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 ' '
|
|
|
|
|
|
2014-03-26 22:15:04 +04:00
|
|
|
|
-- tuples
|
|
|
|
|
|
|
|
|
|
first3 (x,_,_) = x
|
|
|
|
|
second3 (_,x,_) = x
|
|
|
|
|
third3 (_,_,x) = x
|
|
|
|
|
|
|
|
|
|
first4 (x,_,_,_) = x
|
|
|
|
|
second4 (_,x,_,_) = x
|
|
|
|
|
third4 (_,_,x,_) = x
|
|
|
|
|
fourth4 (_,_,_,x) = x
|
|
|
|
|
|
|
|
|
|
first5 (x,_,_,_,_) = x
|
|
|
|
|
second5 (_,x,_,_,_) = x
|
|
|
|
|
third5 (_,_,x,_,_) = x
|
|
|
|
|
fourth5 (_,_,_,x,_) = x
|
|
|
|
|
fifth5 (_,_,_,_,x) = x
|
|
|
|
|
|
2008-11-22 12:06:44 +03:00
|
|
|
|
-- math
|
|
|
|
|
|
|
|
|
|
difforzero :: (Num a, Ord a) => a -> a -> a
|
|
|
|
|
difforzero a b = maximum [(a - b), 0]
|
|
|
|
|
|
2007-07-03 03:54:17 +04:00
|
|
|
|
-- lists
|
|
|
|
|
|
2007-02-16 12:00:17 +03:00
|
|
|
|
splitAtElement :: Eq a => a -> [a] -> [[a]]
|
2014-10-25 01:29:34 +04:00
|
|
|
|
splitAtElement x l =
|
|
|
|
|
case l of
|
|
|
|
|
[] -> []
|
|
|
|
|
e:es | e==x -> split es
|
|
|
|
|
es -> split es
|
|
|
|
|
where
|
|
|
|
|
split es = let (first,rest) = break (x==) es
|
|
|
|
|
in first : splitAtElement x rest
|
2007-02-16 12:00:17 +03:00
|
|
|
|
|
2007-07-03 03:54:17 +04:00
|
|
|
|
-- trees
|
2007-03-11 00:24:57 +03:00
|
|
|
|
|
2012-10-21 21:18:18 +04:00
|
|
|
|
-- standard tree helpers
|
|
|
|
|
|
2007-03-11 00:24:57 +03:00
|
|
|
|
root = rootLabel
|
2008-10-10 15:52:15 +04:00
|
|
|
|
subs = subForest
|
2007-03-11 00:24:57 +03:00
|
|
|
|
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)
|
2009-06-05 13:44:20 +04:00
|
|
|
|
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
|
2014-09-11 00:07:53 +04:00
|
|
|
|
|
2008-10-11 08:18:26 +04:00
|
|
|
|
-- | 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
|
2007-03-11 00:24:57 +03:00
|
|
|
|
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
|
2007-03-11 00:24:57 +03:00
|
|
|
|
treefilter :: (a -> Bool) -> Tree a -> Tree a
|
2014-09-11 00:07:53 +04:00
|
|
|
|
treefilter f t = Node
|
|
|
|
|
(root t)
|
2007-03-11 00:24:57 +03:00
|
|
|
|
(map (treefilter f) $ filter (treeany f) $ branches t)
|
2014-09-11 00:07:53 +04:00
|
|
|
|
|
2008-10-11 08:18:26 +04:00
|
|
|
|
-- | is predicate true in any node of tree ?
|
2007-03-11 00:24:57 +03:00
|
|
|
|
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)
|
2014-09-11 00:07:53 +04:00
|
|
|
|
|
|
|
|
|
-- treedrop -- remove the leaves which do fulfill predicate.
|
2007-03-11 00:24:57 +03:00
|
|
|
|
-- 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
|
2011-06-05 22:31:19 +04:00
|
|
|
|
showtree = unlines . filter (regexMatches "[^ \\|]") . 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
|
|
|
|
|
|
2012-10-21 21:18:18 +04:00
|
|
|
|
|
|
|
|
|
-- | An efficient-to-build tree suggested by Cale Gibbard, probably
|
|
|
|
|
-- better than accountNameTreeFrom.
|
|
|
|
|
newtype FastTree a = T (M.Map a (FastTree a))
|
|
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
|
|
emptyTree = T M.empty
|
|
|
|
|
|
|
|
|
|
mergeTrees :: (Ord a) => FastTree a -> FastTree a -> FastTree a
|
|
|
|
|
mergeTrees (T m) (T m') = T (M.unionWith mergeTrees m m')
|
|
|
|
|
|
|
|
|
|
treeFromPath :: [a] -> FastTree a
|
|
|
|
|
treeFromPath [] = T M.empty
|
|
|
|
|
treeFromPath (x:xs) = T (M.singleton x (treeFromPath xs))
|
|
|
|
|
|
|
|
|
|
treeFromPaths :: (Ord a) => [[a]] -> FastTree a
|
|
|
|
|
treeFromPaths = foldl' mergeTrees emptyTree . map treeFromPath
|
|
|
|
|
|
|
|
|
|
|
2007-07-02 20:43:14 +04:00
|
|
|
|
-- debugging
|
|
|
|
|
|
2012-11-15 21:46:48 +04:00
|
|
|
|
-- more:
|
|
|
|
|
-- http://hackage.haskell.org/packages/archive/TraceUtils/0.1.0.2/doc/html/Debug-TraceUtils.html
|
|
|
|
|
-- http://hackage.haskell.org/packages/archive/trace-call/0.1/doc/html/Debug-TraceCall.html
|
|
|
|
|
-- http://hackage.haskell.org/packages/archive/htrace/0.1/doc/html/Debug-HTrace.html
|
|
|
|
|
-- http://hackage.haskell.org/packages/archive/traced/2009.7.20/doc/html/Debug-Traced.html
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Trace (print on stdout at runtime) a showable value.
|
2008-12-10 10:08:50 +03:00
|
|
|
|
-- (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
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Labelled trace - like strace, with a label prepended.
|
|
|
|
|
ltrace :: Show a => String -> a -> a
|
|
|
|
|
ltrace l a = trace (l ++ ": " ++ show a) a
|
2008-10-11 08:18:26 +04:00
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Monadic trace - like strace, but works as a standalone line in a monad.
|
2010-09-04 03:20:59 +04:00
|
|
|
|
mtrace :: (Monad m, Show a) => a -> m a
|
|
|
|
|
mtrace a = strace a `seq` return a
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Custom trace - like strace, with a custom show function.
|
|
|
|
|
traceWith :: (a -> String) -> a -> a
|
|
|
|
|
traceWith f e = trace (f e) e
|
2009-06-05 14:41:57 +04:00
|
|
|
|
|
2012-12-06 03:49:50 +04:00
|
|
|
|
-- | Parsec trace - show the current parsec position and next input,
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- and the provided label if it's non-null.
|
2012-12-06 03:49:50 +04:00
|
|
|
|
ptrace :: String -> GenParser Char st ()
|
2013-03-29 22:40:10 +04:00
|
|
|
|
ptrace msg = do
|
2012-12-06 03:49:50 +04:00
|
|
|
|
pos <- getPosition
|
2013-03-29 22:40:10 +04:00
|
|
|
|
next <- take peeklength `fmap` getInput
|
|
|
|
|
let (l,c) = (sourceLine pos, sourceColumn pos)
|
|
|
|
|
s = printf "at line %2d col %2d: %s" l c (show next) :: String
|
|
|
|
|
s' = printf ("%-"++show (peeklength+30)++"s") s ++ " " ++ msg
|
|
|
|
|
trace s' $ return ()
|
|
|
|
|
where
|
|
|
|
|
peeklength = 30
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Global debug level, which controls the verbosity of debug output
|
|
|
|
|
-- on the console. The default is 0 meaning no debug output. The
|
|
|
|
|
-- @--debug@ command line flag sets it to 1, or @--debug=N@ sets it to
|
2013-12-07 18:09:39 +04:00
|
|
|
|
-- a higher value (note: not @--debug N@ for some reason). This uses
|
|
|
|
|
-- unsafePerformIO and can be accessed from anywhere and before normal
|
|
|
|
|
-- command-line processing. After command-line processing, it is also
|
|
|
|
|
-- available as the @debug_@ field of 'Hledger.Cli.Options.CliOpts'.
|
2014-10-25 01:29:34 +04:00
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
|
-- {-# NOINLINE debugLevel #-}
|
2013-12-07 01:35:50 +04:00
|
|
|
|
debugLevel :: Int
|
|
|
|
|
debugLevel = case snd $ break (=="--debug") args of
|
|
|
|
|
"--debug":[] -> 1
|
|
|
|
|
"--debug":n:_ -> readDef 1 n
|
2013-12-07 18:09:39 +04:00
|
|
|
|
_ ->
|
|
|
|
|
case take 1 $ filter ("--debug" `isPrefixOf`) args of
|
|
|
|
|
['-':'-':'d':'e':'b':'u':'g':'=':v] -> readDef 1 v
|
|
|
|
|
_ -> 0
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
where
|
|
|
|
|
args = unsafePerformIO getArgs
|
|
|
|
|
|
|
|
|
|
-- | Print a message and a showable value to the console if the global
|
|
|
|
|
-- debug level is non-zero. Uses unsafePerformIO.
|
|
|
|
|
dbg :: Show a => String -> a -> a
|
2014-04-13 22:05:20 +04:00
|
|
|
|
dbg = dbg1
|
|
|
|
|
|
2014-07-18 02:06:28 +04:00
|
|
|
|
-- always prints
|
2014-04-13 22:05:20 +04:00
|
|
|
|
dbg0 :: Show a => String -> a -> a
|
|
|
|
|
dbg0 = dbgAt 0
|
|
|
|
|
|
|
|
|
|
dbg1 :: Show a => String -> a -> a
|
|
|
|
|
dbg1 = dbgAt 1
|
|
|
|
|
|
|
|
|
|
dbg2 :: Show a => String -> a -> a
|
|
|
|
|
dbg2 = dbgAt 2
|
2013-12-07 01:35:50 +04:00
|
|
|
|
|
2014-07-29 05:57:54 +04:00
|
|
|
|
dbg3 :: Show a => String -> a -> a
|
|
|
|
|
dbg3 = dbgAt 3
|
|
|
|
|
|
|
|
|
|
dbg4 :: Show a => String -> a -> a
|
|
|
|
|
dbg4 = dbgAt 4
|
|
|
|
|
|
|
|
|
|
dbg5 :: Show a => String -> a -> a
|
|
|
|
|
dbg5 = dbgAt 5
|
|
|
|
|
|
|
|
|
|
dbg6 :: Show a => String -> a -> a
|
|
|
|
|
dbg6 = dbgAt 6
|
|
|
|
|
|
|
|
|
|
dbg7 :: Show a => String -> a -> a
|
|
|
|
|
dbg7 = dbgAt 7
|
|
|
|
|
|
|
|
|
|
dbg8 :: Show a => String -> a -> a
|
|
|
|
|
dbg8 = dbgAt 8
|
|
|
|
|
|
|
|
|
|
dbg9 :: Show a => String -> a -> a
|
|
|
|
|
dbg9 = dbgAt 9
|
|
|
|
|
|
2014-04-02 15:47:32 +04:00
|
|
|
|
-- | Print a message and a showable value to the console if the global
|
|
|
|
|
-- debug level is at or above the specified level. Uses unsafePerformIO.
|
|
|
|
|
dbgAt :: Show a => Int -> String -> a -> a
|
|
|
|
|
dbgAt lvl = dbgppshow lvl
|
|
|
|
|
|
2014-07-03 10:24:58 +04:00
|
|
|
|
-- Could not deduce (a ~ ())
|
|
|
|
|
-- from the context (Show a)
|
|
|
|
|
-- bound by the type signature for
|
|
|
|
|
-- dbgM :: Show a => String -> a -> IO ()
|
|
|
|
|
-- at hledger/Hledger/Cli/Main.hs:200:13-42
|
|
|
|
|
-- ‘a’ is a rigid type variable bound by
|
|
|
|
|
-- the type signature for dbgM :: Show a => String -> a -> IO ()
|
|
|
|
|
-- at hledger/Hledger/Cli/Main.hs:200:13
|
|
|
|
|
-- Expected type: String -> a -> IO ()
|
|
|
|
|
-- Actual type: String -> a -> IO a
|
2014-06-13 03:14:41 +04:00
|
|
|
|
-- dbgAtM :: (Monad m, Show a) => Int -> String -> a -> m a
|
|
|
|
|
-- dbgAtM lvl lbl x = dbgAt lvl lbl x `seq` return x
|
2014-07-03 10:24:58 +04:00
|
|
|
|
-- XXX temporary:
|
2014-04-02 20:30:59 +04:00
|
|
|
|
dbgAtM :: Show a => Int -> String -> a -> IO ()
|
2014-06-13 03:14:41 +04:00
|
|
|
|
dbgAtM = dbgAtIO
|
2014-04-02 20:30:59 +04:00
|
|
|
|
|
2014-06-13 03:14:41 +04:00
|
|
|
|
dbgAtIO :: Show a => Int -> String -> a -> IO ()
|
|
|
|
|
dbgAtIO lvl lbl x = dbgAt lvl lbl x `seq` return ()
|
|
|
|
|
|
|
|
|
|
-- | print this string to the console before evaluating the expression,
|
2014-05-10 04:52:23 +04:00
|
|
|
|
-- if the global debug level is non-zero. Uses unsafePerformIO.
|
|
|
|
|
dbgtrace :: String -> a -> a
|
|
|
|
|
dbgtrace
|
|
|
|
|
| debugLevel > 0 = trace
|
|
|
|
|
| otherwise = flip const
|
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
-- | Print a showable value to the console, with a message, if the
|
|
|
|
|
-- debug level is at or above the specified level (uses
|
|
|
|
|
-- unsafePerformIO).
|
|
|
|
|
-- Values are displayed with show, all on one line, which is hard to read.
|
|
|
|
|
dbgshow :: Show a => Int -> String -> a -> a
|
|
|
|
|
dbgshow level
|
|
|
|
|
| debugLevel >= level = ltrace
|
|
|
|
|
| otherwise = flip const
|
|
|
|
|
|
|
|
|
|
-- | Print a showable value to the console, with a message, if the
|
|
|
|
|
-- debug level is at or above the specified level (uses
|
|
|
|
|
-- unsafePerformIO).
|
|
|
|
|
-- Values are displayed with ppShow, each field/constructor on its own line.
|
|
|
|
|
dbgppshow :: Show a => Int -> String -> a -> a
|
|
|
|
|
dbgppshow level
|
2014-03-31 18:58:28 +04:00
|
|
|
|
| debugLevel < level = flip const
|
|
|
|
|
| otherwise = \s a -> let p = ppShow a
|
|
|
|
|
ls = lines p
|
|
|
|
|
nlorspace | length ls > 1 = "\n"
|
|
|
|
|
| otherwise = " " ++ take (10 - length s) (repeat ' ')
|
|
|
|
|
ls' | length ls > 1 = map (" "++) ls
|
|
|
|
|
| otherwise = ls
|
|
|
|
|
in trace (s++":"++nlorspace++intercalate "\n" ls') a
|
2013-12-07 01:35:50 +04:00
|
|
|
|
|
2014-03-20 03:11:46 +04:00
|
|
|
|
-- -- | Print a showable value to the console, with a message, if the
|
|
|
|
|
-- -- debug level is at or above the specified level (uses
|
|
|
|
|
-- -- unsafePerformIO).
|
|
|
|
|
-- -- Values are displayed with pprint. Field names are not shown, but the
|
|
|
|
|
-- -- output is compact with smart line wrapping, long data elided,
|
|
|
|
|
-- -- and slow calculations timed out.
|
|
|
|
|
-- dbgpprint :: Data a => Int -> String -> a -> a
|
|
|
|
|
-- dbgpprint level msg a
|
|
|
|
|
-- | debugLevel >= level = unsafePerformIO $ do
|
|
|
|
|
-- pprint a >>= putStrLn . ((msg++": \n") ++) . show
|
|
|
|
|
-- return a
|
|
|
|
|
-- | otherwise = a
|
2013-12-07 01:35:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- | Like dbg, then exit the program. Uses unsafePerformIO.
|
|
|
|
|
dbgExit :: Show a => String -> a -> a
|
|
|
|
|
dbgExit msg = const (unsafePerformIO exitFailure) . dbg msg
|
|
|
|
|
|
|
|
|
|
-- | Print a message and parsec debug info (parse position and next
|
|
|
|
|
-- input) to the console when the debug level is at or above
|
|
|
|
|
-- this level. Uses unsafePerformIO.
|
|
|
|
|
-- pdbgAt :: GenParser m => Float -> String -> m ()
|
2013-03-29 22:40:10 +04:00
|
|
|
|
pdbg level msg = when (level <= debugLevel) $ ptrace msg
|
2012-12-06 03:49:50 +04:00
|
|
|
|
|
2013-12-07 01:35:50 +04:00
|
|
|
|
|
2008-11-27 03:35:00 +03:00
|
|
|
|
-- parsing
|
|
|
|
|
|
2011-07-17 21:16:40 +04:00
|
|
|
|
-- | Backtracking choice, use this when alternatives share a prefix.
|
|
|
|
|
-- Consumes no input if all choices fail.
|
2010-08-12 18:37:11 +04:00
|
|
|
|
choice' :: [GenParser tok st a] -> GenParser tok st a
|
2010-04-15 01:37:03 +04:00
|
|
|
|
choice' = choice . map Text.ParserCombinators.Parsec.try
|
|
|
|
|
|
2008-11-27 03:35:00 +03:00
|
|
|
|
parsewith :: Parser a -> String -> Either ParseError a
|
2009-09-22 19:56:59 +04:00
|
|
|
|
parsewith p = parse p ""
|
2008-11-27 03:35:00 +03:00
|
|
|
|
|
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
|
|
|
|
|
2008-11-27 03:35:00 +03:00
|
|
|
|
fromparse :: Either ParseError a -> a
|
2010-03-09 21:33:26 +03:00
|
|
|
|
fromparse = either parseerror id
|
|
|
|
|
|
2011-08-08 05:34:21 +04:00
|
|
|
|
parseerror :: ParseError -> a
|
2010-09-05 22:18:50 +04:00
|
|
|
|
parseerror e = error' $ showParseError e
|
2010-03-09 21:33:26 +03:00
|
|
|
|
|
2011-08-08 05:34:21 +04:00
|
|
|
|
showParseError :: ParseError -> String
|
2010-03-09 21:33:26 +03:00
|
|
|
|
showParseError e = "parse error at " ++ show e
|
|
|
|
|
|
2011-08-08 05:34:21 +04:00
|
|
|
|
showDateParseError :: ParseError -> String
|
2010-03-09 21:33:26 +03:00
|
|
|
|
showDateParseError e = printf "date parse error (%s)" (intercalate ", " $ tail $ lines $ show e)
|
2008-10-11 08:18:26 +04:00
|
|
|
|
|
2008-12-08 04:11:07 +03:00
|
|
|
|
nonspace :: GenParser Char st Char
|
2008-11-27 03:35:00 +03:00
|
|
|
|
nonspace = satisfy (not . isSpace)
|
|
|
|
|
|
2008-12-08 04:11:07 +03:00
|
|
|
|
spacenonewline :: GenParser Char st Char
|
2009-09-22 19:56:59 +04:00
|
|
|
|
spacenonewline = satisfy (`elem` " \v\f\t")
|
2008-11-27 03:35:00 +03:00
|
|
|
|
|
2008-12-08 04:11:07 +03:00
|
|
|
|
restofline :: GenParser Char st String
|
2008-11-27 03:35:00 +03:00
|
|
|
|
restofline = anyChar `manyTill` newline
|
2008-10-11 08:18:26 +04:00
|
|
|
|
|
2013-03-29 22:42:00 +04:00
|
|
|
|
eolof :: GenParser Char st ()
|
|
|
|
|
eolof = (newline >> return ()) <|> eof
|
|
|
|
|
|
2009-01-25 09:47:05 +03:00
|
|
|
|
-- time
|
|
|
|
|
|
|
|
|
|
getCurrentLocalTime :: IO LocalTime
|
|
|
|
|
getCurrentLocalTime = do
|
|
|
|
|
t <- getCurrentTime
|
|
|
|
|
tz <- getCurrentTimeZone
|
|
|
|
|
return $ utcToLocalTime tz t
|
2009-04-01 12:55:46 +04:00
|
|
|
|
|
2010-03-09 00:47:36 +03:00
|
|
|
|
-- testing
|
|
|
|
|
|
|
|
|
|
-- | Get a Test's label, or the empty string.
|
2012-05-14 23:23:12 +04:00
|
|
|
|
testName :: Test -> String
|
|
|
|
|
testName (TestLabel n _) = n
|
|
|
|
|
testName _ = ""
|
2010-03-09 00:47:36 +03:00
|
|
|
|
|
|
|
|
|
-- | Flatten a Test containing TestLists into a list of single tests.
|
2012-05-14 23:23:12 +04:00
|
|
|
|
flattenTests :: Test -> [Test]
|
|
|
|
|
flattenTests (TestLabel _ t@(TestList _)) = flattenTests t
|
|
|
|
|
flattenTests (TestList ts) = concatMap flattenTests ts
|
|
|
|
|
flattenTests t = [t]
|
2010-03-09 00:47:36 +03:00
|
|
|
|
|
|
|
|
|
-- | Filter TestLists in a Test, recursively, preserving the structure.
|
2012-05-14 23:23:12 +04:00
|
|
|
|
filterTests :: (Test -> Bool) -> Test -> Test
|
|
|
|
|
filterTests p (TestLabel l ts) = TestLabel l (filterTests p ts)
|
|
|
|
|
filterTests p (TestList ts) = TestList $ filter (any p . flattenTests) $ map (filterTests p) ts
|
|
|
|
|
filterTests _ t = t
|
2010-03-09 00:47:36 +03:00
|
|
|
|
|
|
|
|
|
-- | 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
|
|
|
|
|
|
2011-05-31 23:48:55 +04:00
|
|
|
|
-- | Assert a parse result is successful, printing the parse error on failure.
|
|
|
|
|
assertParseFailure :: (Either ParseError a) -> Assertion
|
|
|
|
|
assertParseFailure parse = either (const $ return ()) (const $ assertFailure "parse should not have succeeded") parse
|
|
|
|
|
|
2010-03-10 22:48:46 +03:00
|
|
|
|
-- | 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
|
2010-03-09 00:47:36 +03:00
|
|
|
|
|
|
|
|
|
printParseError :: (Show a) => a -> IO ()
|
|
|
|
|
printParseError e = do putStr "parse error at "; print e
|
|
|
|
|
|
2009-04-08 09:30:26 +04:00
|
|
|
|
-- misc
|
2009-04-01 12:55:46 +04:00
|
|
|
|
|
|
|
|
|
isLeft :: Either a b -> Bool
|
|
|
|
|
isLeft (Left _) = True
|
|
|
|
|
isLeft _ = False
|
|
|
|
|
|
|
|
|
|
isRight :: Either a b -> Bool
|
|
|
|
|
isRight = not . isLeft
|
|
|
|
|
|
2011-01-14 07:32:08 +03:00
|
|
|
|
-- | Apply a function the specified number of times. Possibly uses O(n) stack ?
|
|
|
|
|
applyN :: Int -> (a -> a) -> a -> a
|
|
|
|
|
applyN n f = (!! n) . iterate f
|
2012-03-24 22:08:11 +04:00
|
|
|
|
|
2012-05-30 12:36:01 +04:00
|
|
|
|
-- | Convert a possibly relative, possibly tilde-containing file path to an absolute one,
|
2014-09-11 00:07:53 +04:00
|
|
|
|
-- given the current directory. ~username is not supported. Leave "-" unchanged.
|
2012-05-30 12:36:01 +04:00
|
|
|
|
expandPath :: MonadIO m => FilePath -> FilePath -> m FilePath -- general type sig for use in reader parsers
|
|
|
|
|
expandPath _ "-" = return "-"
|
|
|
|
|
expandPath curdir p = (if isRelative p then (curdir </>) else id) `liftM` expandPath' p
|
2012-03-24 22:08:11 +04:00
|
|
|
|
where
|
2012-05-30 12:36:01 +04:00
|
|
|
|
expandPath' ('~':'/':p) = liftIO $ (</> p) `fmap` getHomeDirectory
|
|
|
|
|
expandPath' ('~':'\\':p) = liftIO $ (</> p) `fmap` getHomeDirectory
|
|
|
|
|
expandPath' ('~':_) = error' "~USERNAME in paths is not supported"
|
|
|
|
|
expandPath' p = return p
|
2012-04-16 20:44:41 +04:00
|
|
|
|
|
|
|
|
|
firstJust ms = case dropWhile (==Nothing) ms of
|
|
|
|
|
[] -> Nothing
|
|
|
|
|
(md:_) -> md
|
2013-03-29 22:46:10 +04:00
|
|
|
|
|
|
|
|
|
-- | Read a file in universal newline mode, handling whatever newline convention it may contain.
|
|
|
|
|
readFile' :: FilePath -> IO String
|
|
|
|
|
readFile' name = do
|
|
|
|
|
h <- openFile name ReadMode
|
|
|
|
|
hSetNewlineMode h universalNewlineMode
|
|
|
|
|
hGetContents h
|