{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Activity
where
import Data.List
import Data.Maybe
import Text.Printf
import Hledger
import Hledger.Cli.CliOptions
activitymode :: Mode RawOpts
activitymode = [Char]
-> [Flag RawOpts]
-> [([Char], [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Activity.txt")
[]
[([Char], [Flag RawOpts])
generalflagsgroup1]
[Flag RawOpts]
hiddenflags
([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ [Char] -> Arg RawOpts
argsFlag [Char]
"[QUERY]")
barchar :: Char
barchar :: Char
barchar = Char
'*'
activity :: CliOpts -> Journal -> IO ()
activity :: CliOpts -> Journal -> IO ()
activity CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec} Journal
j = [Char] -> IO ()
putStr ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> [Char]
showHistogram ReportSpec
rspec Journal
j
showHistogram :: ReportSpec -> Journal -> String
showHistogram :: ReportSpec -> Journal -> [Char]
showHistogram ReportSpec{_rsQuery :: ReportSpec -> Query
_rsQuery=Query
q,_rsReportOpts :: ReportSpec -> ReportOpts
_rsReportOpts=ReportOpts{interval_ :: ReportOpts -> Interval
interval_=Interval
i,date2_ :: ReportOpts -> Bool
date2_=Bool
date2}} Journal
j =
((DateSpan, [Posting]) -> [Char])
-> [(DateSpan, [Posting])] -> [Char]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (([Posting] -> [Char]) -> (DateSpan, [Posting]) -> [Char]
forall {t} {t} {t}.
(PrintfArg t, PrintfType t) =>
(t -> t) -> (DateSpan, t) -> t
printDayWith [Posting] -> [Char]
forall {t :: * -> *} {a}. Foldable t => t a -> [Char]
countBar) [(DateSpan, [Posting])]
spanps
where
interval :: Interval
interval | Interval
i Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
== Interval
NoInterval = Int -> Interval
Days Int
1
| Bool
otherwise = Interval
i
span' :: DateSpan
span' = Bool -> Query -> DateSpan
queryDateSpan Bool
date2 Query
q DateSpan -> DateSpan -> DateSpan
`spanDefaultsFrom` Bool -> Journal -> DateSpan
journalDateSpan Bool
date2 Journal
j
spans :: [DateSpan]
spans = (DateSpan -> Bool) -> [DateSpan] -> [DateSpan]
forall a. (a -> Bool) -> [a] -> [a]
filter (Maybe Day -> Maybe Day -> DateSpan
DateSpan Maybe Day
forall a. Maybe a
Nothing Maybe Day
forall a. Maybe a
Nothing DateSpan -> DateSpan -> Bool
forall a. Eq a => a -> a -> Bool
/=) ([DateSpan] -> [DateSpan]) -> [DateSpan] -> [DateSpan]
forall a b. (a -> b) -> a -> b
$ Interval -> DateSpan -> [DateSpan]
splitSpan Interval
interval DateSpan
span'
spanps :: [(DateSpan, [Posting])]
spanps = [(DateSpan
s, (Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter (DateSpan -> Posting -> Bool
isPostingInDateSpan DateSpan
s) [Posting]
ps) | DateSpan
s <- [DateSpan]
spans]
ps :: [Posting]
ps = (Posting -> Day) -> [Posting] -> [Posting]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Posting -> Day
postingDate ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ (Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Posting -> Bool
`matchesPosting`) ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
printDayWith :: (t -> t) -> (DateSpan, t) -> t
printDayWith t -> t
f (DateSpan Maybe Day
b Maybe Day
_, t
ps) = [Char] -> [Char] -> t -> t
forall r. PrintfType r => [Char] -> r
printf [Char]
"%s %s\n" (Day -> [Char]
forall a. Show a => a -> [Char]
show (Day -> [Char]) -> Day -> [Char]
forall a b. (a -> b) -> a -> b
$ Maybe Day -> Day
forall a. HasCallStack => Maybe a -> a
fromJust Maybe Day
b) (t -> t
f t
ps)
countBar :: t a -> [Char]
countBar t a
ps = Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
ps) Char
barchar