Add desc/description as possible sort field
This commit is contained in:
parent
25bcf3eebb
commit
693360344c
@ -25,7 +25,7 @@ where
|
|||||||
|
|
||||||
import Data.List (nub, sortBy, sortOn)
|
import Data.List (nub, sortBy, sortOn)
|
||||||
import Data.List.Extra (nubSort)
|
import Data.List.Extra (nubSort)
|
||||||
import Data.Maybe (isJust, isNothing)
|
import Data.Maybe (isJust, isNothing, fromMaybe)
|
||||||
import Data.Ord
|
import Data.Ord
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Time.Calendar (Day)
|
import Data.Time.Calendar (Day)
|
||||||
@ -123,13 +123,21 @@ registerRunningCalculationFn ropts
|
|||||||
comparePostings :: ReportOpts -> SortSpec -> (Posting, Maybe Period) -> (Posting, Maybe Period) -> Ordering
|
comparePostings :: ReportOpts -> SortSpec -> (Posting, Maybe Period) -> (Posting, Maybe Period) -> Ordering
|
||||||
comparePostings _ [] _ _ = EQ
|
comparePostings _ [] _ _ = EQ
|
||||||
comparePostings ropts (ex:es) (a, pa) (b, pb) =
|
comparePostings ropts (ex:es) (a, pa) (b, pb) =
|
||||||
let comparison = case ex of
|
let
|
||||||
|
getDescription p =
|
||||||
|
let tx = ptransaction p
|
||||||
|
description = fmap (\t -> tdescription t) tx
|
||||||
|
-- If there's no transaction attached, then use empty text for the description
|
||||||
|
in fromMaybe "" description
|
||||||
|
comparison = case ex of
|
||||||
Amount' False -> compare (pamount a) (pamount b)
|
Amount' False -> compare (pamount a) (pamount b)
|
||||||
Account' False -> compare (paccount a) (paccount b)
|
Account' False -> compare (paccount a) (paccount b)
|
||||||
Date' False -> compare (postingDateOrDate2 (whichDate ropts) a) (postingDateOrDate2 (whichDate ropts) b)
|
Date' False -> compare (postingDateOrDate2 (whichDate ropts) a) (postingDateOrDate2 (whichDate ropts) b)
|
||||||
|
Description' False -> compare (getDescription a) (getDescription b)
|
||||||
Amount' True -> compare (Down (pamount a)) (Down (pamount b))
|
Amount' True -> compare (Down (pamount a)) (Down (pamount b))
|
||||||
Account' True -> compare (Down (paccount a)) (Down (paccount b))
|
Account' True -> compare (Down (paccount a)) (Down (paccount b))
|
||||||
Date' True -> compare (Down (postingDateOrDate2 (whichDate ropts) a)) (Down (postingDateOrDate2 (whichDate ropts) b))
|
Date' True -> compare (Down (postingDateOrDate2 (whichDate ropts) a)) (Down (postingDateOrDate2 (whichDate ropts) b))
|
||||||
|
Description' True -> compare (Down (getDescription a)) (Down (getDescription b))
|
||||||
in
|
in
|
||||||
if comparison == EQ then comparePostings ropts es (a, pa) (b, pb) else comparison
|
if comparison == EQ then comparePostings ropts es (a, pa) (b, pb) else comparison
|
||||||
|
|
||||||
|
|||||||
@ -676,9 +676,10 @@ queryFromFlags ReportOpts{..} = simplifyQuery $ And flagsq
|
|||||||
-- Each of these takes a bool, which shows if it has been inverted
|
-- Each of these takes a bool, which shows if it has been inverted
|
||||||
-- (True -> has been inverted, reverse the order)
|
-- (True -> has been inverted, reverse the order)
|
||||||
data SortField
|
data SortField
|
||||||
= Date' Bool
|
= Account' Bool
|
||||||
| Account' Bool
|
|
||||||
| Amount' Bool
|
| Amount' Bool
|
||||||
|
| Date' Bool
|
||||||
|
| Description' Bool
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
type SortSpec = [SortField]
|
type SortSpec = [SortField]
|
||||||
|
|
||||||
@ -697,6 +698,8 @@ getSortSpec opts =
|
|||||||
"date" -> Date' isNegated
|
"date" -> Date' isNegated
|
||||||
"account" -> Account' isNegated
|
"account" -> Account' isNegated
|
||||||
"amount" -> Amount' isNegated
|
"amount" -> Amount' isNegated
|
||||||
|
"desc" -> Description' isNegated
|
||||||
|
"description" -> Description' isNegated
|
||||||
_ -> error' $ "unsupported field '" ++ t ++ "' given to --sort"
|
_ -> error' $ "unsupported field '" ++ t ++ "' given to --sort"
|
||||||
where isNegated = isPrefixOf "-" t
|
where isNegated = isPrefixOf "-" t
|
||||||
trimmed = fromMaybe t (stripPrefix "-" t)
|
trimmed = fromMaybe t (stripPrefix "-" t)
|
||||||
|
|||||||
@ -74,3 +74,18 @@ $ hledger -f - register --sort -date
|
|||||||
a -1 0
|
a -1 0
|
||||||
2024-01-01 Demo a 1 1
|
2024-01-01 Demo a 1 1
|
||||||
b -1 0
|
b -1 0
|
||||||
|
|
||||||
|
# ** 6. --sort with description
|
||||||
|
<
|
||||||
|
2024-01-01 Other
|
||||||
|
a 1
|
||||||
|
b
|
||||||
|
|
||||||
|
2024-01-02 Demo
|
||||||
|
c 1
|
||||||
|
a
|
||||||
|
$ hledger -f - register --sort desc
|
||||||
|
2024-01-02 Demo c 1 1
|
||||||
|
a -1 0
|
||||||
|
2024-01-01 Other a 1 1
|
||||||
|
b -1 0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user