packages feed

hledger-iadd 1.1.3 → 1.1.4

raw patch · 5 files changed

+52/−4 lines, 5 filesdep ~brickdep ~megaparsecPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: brick, megaparsec

API changes (from Hackage documentation)

+ Model: accountsByFrequency :: Journal -> [AccountName]

Files

ChangeLog.md view
@@ -1,3 +1,9 @@+# 1.1.4++  - Sort account names by frequency for completion+  - Bind Home/End im entry field+  - Bump brick and vty dependencies+ # 1.1.3    - Add more emacs/readline like keybindings in entry field (`C-f`/`C-b`,
hledger-iadd.cabal view
@@ -1,5 +1,5 @@ name:                hledger-iadd-version:             1.1.3+version:             1.1.4 synopsis:            A terminal UI as drop-in replacement for hledger add description:         This is a terminal UI as drop-in replacement for hledger add.                      .@@ -57,7 +57,7 @@   default-language:    Haskell2010   build-depends:       base >= 4.8 && < 5                      , hledger-lib >= 1.0 && < 1.2-                     , brick >= 0.15.2 && < 0.17+                     , brick >= 0.15.2 && < 0.18                      , vty >= 5.4                      , text                      , microlens@@ -82,7 +82,7 @@   build-depends:       base >= 4.8 && < 5                      , hledger-iadd                      , hledger-lib >= 1.0 && < 1.2-                     , brick >= 0.15.2 && < 0.17+                     , brick >= 0.15.2 && < 0.18                      , vty >= 5.4                      , text                      , microlens
src/Brick/Widgets/Edit/EmacsBindings.hs view
@@ -38,4 +38,7 @@   EvKey (KChar 'w') [MCtrl] -> return $ applyEdit deletePrevWord edit   EvKey (KChar 'd') [MMeta] -> return $ applyEdit deleteWord edit +  EvKey KHome       []      -> return $ applyEdit gotoBOL edit+  EvKey KEnd        []      -> return $ applyEdit gotoEOL edit+   _ -> E.handleEditorEvent event edit
src/Model.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE LambdaCase, OverloadedStrings #-}  module Model@@ -8,6 +9,9 @@        , undo        , context        , suggest++       -- * Helpers exported for easier testing+       , accountsByFrequency        ) where  import           Data.Function@@ -87,7 +91,7 @@   let descs = HL.journalDescriptions j   in sortBy (descUses j) $ filter (matches matchAlgo entryText) descs context j matchAlgo _ entryText (AccountQuestion _) = return $-  let names = HL.journalAccountNames j+  let names = accountsByFrequency j   in  filter (matches matchAlgo entryText) names context journal _ _ entryText (AmountQuestion _ _) = return $   maybeToList $ T.pack . HL.showMixedAmount <$> trySumAmount journal entryText@@ -243,6 +247,23 @@         -- Add one to the current count of this element         count :: Text -> HM.HashMap Text (Sum Int) -> HM.HashMap Text (Sum Int)         count = HM.alter (<> Just 1)++-- | All accounts occuring in the journal sorted in descending order of+-- appearance.+accountsByFrequency :: HL.Journal -> [HL.AccountName]+accountsByFrequency journal =+  let+    accounts = map HL.paccount (HL.journalPostings journal)+    frequencyMap :: HM.HashMap HL.AccountName Int = foldr insertOrPlusOne HM.empty accounts+    mapWithSubaccounts = foldr insertIfNotPresent frequencyMap (subaccounts frequencyMap)+  in+    map fst (sortBy (compare `on` (Down . snd)) (HM.toList mapWithSubaccounts))+++  where+    insertOrPlusOne = HM.alter (Just . maybe 1 (+1))+    insertIfNotPresent account = HM.insertWith (flip const) account 0+    subaccounts m = HL.expandAccountNames (HM.keys m)  fromEither :: Either a a -> a fromEither = either id id
tests/ModelSpec.hs view
@@ -19,6 +19,7 @@ spec :: Spec spec = do   describe "suggest" suggestSpec+  describe "accountsByFrequency" accByFreqSpec   suggestSpec :: Spec@@ -67,6 +68,23 @@           t = mkTransaction ((2016, 1, 1), "Bar", [("foo", 3)])        suggest j german (AmountQuestion "y" t) `shouldReturn` (Just "€-3.00")+++accByFreqSpec :: Spec+accByFreqSpec = do+  it "sorts according to frequency" $ do+    let postings = [("x", 1), ("y", 2), ("y", 3)]+        j = mkJournal [ ((2017, 1, 1), "Foo", postings) ]++    accountsByFrequency j `shouldBe` ["y", "x"]++  it "includes subaccounts" $ do+    let j = mkJournal [ ((2017, 1, 1), "Foo", [("x:y", 2)]) ]+    accountsByFrequency j `shouldContain` ["x"]++  it "only counts explicit occurences for sorting" $ do+    let j = mkJournal [ ((2017, 1, 1), "Foo", [("x:y", 2), ("x:y", 3), ("x:z", 4)]) ]+    accountsByFrequency j `shouldBe` ["x:y", "x:z", "x"]   -- Helpers