diff options
Diffstat (limited to 'app/Main.hs')
-rw-r--r-- | app/Main.hs | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/app/Main.hs b/app/Main.hs index 11b7602..f3411ca 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -169,7 +169,8 @@ filtersArg = <> O.help "Filter documents by tag" ) where - parse ('@' : tagKey) = Just (FilterByTag (T.pack tagKey)) + parse ('@' : tagKey) = Just (Filter Include (FilterByTag (T.pack tagKey))) + parse ('!' : '@' : tagKey) = Just (Filter Exclude (FilterByTag (T.pack tagKey))) parse _ = Nothing indexNamesArg :: O.Parser [FilePath] @@ -208,7 +209,11 @@ viewArg = <> O.help "Run command `view` on listed document(s)" ) -data Filter +data Filter = Filter Mode SimpleFilter + +data Mode = Include | Exclude + +data SimpleFilter = FilterByTag T.Text main :: IO () @@ -234,7 +239,7 @@ main = do mapM_ ( \doc -> do printf "%s\n" (takeBaseName doc.iFileName) - print doc.index.tags + printTags doc ) docs' Args {cmd = Edit {indexNames}} -> do @@ -243,10 +248,9 @@ main = do Args {cmd = List {filters, redo, todo = False, view = False, edit = False}} -> do doRedoIf filters redo mapM_ - ( \(Document {iFileName, index}) -> do - if hasTag (Tag "todo" Nothing) index.tags - then printf "TODO %s\n" (takeBaseName iFileName) - else printf " %s\n" (takeBaseName iFileName) + ( \doc -> do + printf "%s\n" (takeBaseName doc.iFileName) + printTags doc ) . applyFilters filters =<< getDocuments @@ -267,7 +271,7 @@ main = do allDocs <- getDocuments _ <- processDocumentsInteractively settings allDocs - . applyFilters [FilterByTag "todo"] + . applyFilters [Filter Include (FilterByTag "todo")] $ allDocs pure () Args {cmd = List {filters, redo, view = True}} -> do @@ -279,6 +283,16 @@ main = do viewDocuments =<< mapM (readDocument . (<.> "json")) indexNames +printTags :: Document -> IO () +printTags doc = + mapM_ + ( \tag -> + case tagValue tag of + Nothing -> printf "@%s\n" (tagKey tag) + Just tagValue -> printf "@%s %s\n" (tagKey tag) tagValue + ) + doc.index.tags + doRedoIf :: [Filter] -> Bool -> IO () doRedoIf filters redo = when redo do @@ -326,7 +340,9 @@ readDocument iFileName = applyFilters :: [Filter] -> [Document] -> [Document] applyFilters filters = filter (pred filters) `at` (.index.internalTags) where - pred1 (FilterByTag tagKey) = hasTag (Tag tagKey Nothing) + pred1 (Filter Include filter') = pred1' filter' + pred1 (Filter Exclude filter') = not . pred1' filter' + pred1' (FilterByTag tagKey) = hasTag (Tag tagKey Nothing) pred filters = \index -> all ($ index) (map pred1 filters) at :: ([a] -> [a]) -> (b -> a) -> [b] -> [b] |