aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/Filter.hs
blob: 717fa8d0ff205f8ebfc5aa54128942c9ed644d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

module Issue.Filter
  ( Filter,
    filterArg,
    applyFilter,
  )
where

import Control.Arrow (second)
import Data.Text (Text)
import Data.Text qualified as T
import Issue (Issue (..))
import Issue.Tag (Tag (..))
import Options.Applicative qualified as O

data Filter
  = ExcludeByTag Text (Maybe Text)
  | IncludeByTag Text (Maybe Text)
  deriving (Show)

filterArg :: O.Parser [Filter]
filterArg =
  O.many
    ( O.option
        (O.maybeReader (parse . T.pack))
        ( O.long "filter"
            <> O.short 'f'
            <> O.metavar "FILTER"
            <> O.help "Filters selected issues. Examples: @assigned, !@assigned joe"
        )
    )
  where
    parse s
      | "@" `T.isPrefixOf` s =
          Just (uncurry IncludeByTag (splitValue (T.drop 1 s)))
      | "!@" `T.isPrefixOf` s =
          Just (uncurry ExcludeByTag (splitValue (T.drop 2 s)))
      | otherwise = Nothing

    splitValue :: Text -> (Text, Maybe Text)
    splitValue s = case second T.strip (T.breakOn " " s) of
      (k, "") -> (k, Nothing)
      (k, v) -> (k, Just v)

applyFilter :: [Filter] -> Issue -> Bool
applyFilter [] _ = True
applyFilter (IncludeByTag k v : fs) i =
  applyByTag k v i && applyFilter fs i
applyFilter (ExcludeByTag k v : fs) i =
  not (applyByTag k v i) && applyFilter fs i

applyByTag :: Text -> Maybe Text -> Issue -> Bool
applyByTag k v i = any ((&&) <$> matchKey <*> matchValue) i.tags
  where
    matchKey (Tag k' _) = k' == k
    matchValue (Tag _ v') = maybe True ((==) v') v