Skip to content
Permalink
Browse files

Support optional filter expression after "count" command

  • Loading branch information...
ad-si committed Oct 4, 2019
1 parent 96ef732 commit 61e87b715573e295d4543fb32e9ac5a8f68a036b
Showing with 37 additions and 15 deletions.
  1. +7 −5 tasklite-core/app/Main.hs
  2. +30 −10 tasklite-core/source/Lib.hs
@@ -33,7 +33,6 @@ import DbSetup
import ImportExport
import Migrations
import Paths_tasklite_core (version) -- Special module provided by Cabal
import Task (TaskState(..))
import Utils


@@ -105,7 +104,7 @@ data Command
| ListRepeating
| ListNoTag
| ListWithTag [Text]
| Count (Filter TaskState)
| CountFiltered (Maybe [Text])
| QueryTasks Text
| RunSql Text
| RunFilter [Text]
@@ -541,8 +540,11 @@ commandParser conf =

<|> subparser ( commandGroup (T.unpack $ fst advanced_sec)

<> command "count" (toParserInfo (pure $ Count NoFilter)
"Output total number of tasks")
<> command "count"
(toParserInfo
(CountFiltered <$> (optional $ some
(strArgument $ metavar "FILTER_EXP" <> help "Filter expressions")))
"Output total number of tasks filtered by the specified expressions")

<> command "config" (toParserInfo (pure PrintConfig)
"Print current configuration of TaskLite")
@@ -780,7 +782,7 @@ executeCLiCommand conf now connection cmd =
AddNote noteText ids -> addNote conf connection noteText ids
SetDueUtc datetime ids -> setDueUtc conf connection datetime ids
Duplicate ids -> duplicateTasks conf connection ids
Count taskFilter -> countTasks conf taskFilter
CountFiltered taskFilter -> countTasks conf connection taskFilter

{- Unset -}
UnDueTasks ids -> undueTasks conf connection ids
@@ -998,18 +998,38 @@ getIdLength numOfItems =
ceiling (logBase sizeOfAlphabet (numOfItems / targetCollisionChance)) + 1


countTasks :: Config -> Filter TaskState -> IO (Doc AnsiStyle)
countTasks conf taskStateFilter = do
execWithConn conf $ \connection -> do
[NumRows taskCount] <- case taskStateFilter of
NoFilter -> query_ connection $ Query $
countTasks :: Config -> Connection -> Maybe [Text] -> IO (Doc AnsiStyle)
countTasks conf connection filterExpression = do
let
parserResults = readP_to_S filterExpsParser $
T.unpack (unwords $ fromMaybe [""] filterExpression)
filterMay = listToMaybe parserResults

case filterMay of
Nothing -> do
[NumRows taskCount] <- query_ connection $ Query $
"select count(1) from `" <> tableName conf <> "`"
Utils.Only taskState -> query connection
(Query $ "select count(1) from `" <> tableName conf
<> "` where `state` == ?")
[(show taskState) :: Text]

pure $ pretty taskCount
pure $ pretty taskCount

Just (filterExps, _) -> do
let
ppInvalidFilter = \case
(InvalidFilter error) ->
(dquotes $ pretty error) <+> "is an invalid filter"
(HasStatus Nothing) -> "Filter contains an invalid state value"
_ -> "The functions should not be called with a valid function"
errors = P.filter (not . isValidFilter) filterExps
errorsDoc = if (P.length errors) > 0
then Just $
vsep (fmap (annotate (color Red) . ppInvalidFilter) errors)
<> hardline <> hardline
else Nothing

-- TODO: Increase performance of this query
tasks <- query_ connection (getFilterQuery filterExps)

pure $ fromMaybe (pretty $ P.length (tasks :: [FullTask])) errorsDoc


-- TODO: Print number of remaining tasks and how to display them at the bottom

0 comments on commit 61e87b7

Please sign in to comment.
You can’t perform that action at this time.