From a37d3605e40b5c9c125568045e0fb99e9796fe49 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 9 Nov 2023 15:43:28 +0100 Subject: group-by -> group --- app/Issue/Group.hs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/Issue/GroupBy.hs | 45 --------------------------------------------- app/Main.hs | 14 +++++++------- 3 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 app/Issue/Group.hs delete mode 100644 app/Issue/GroupBy.hs (limited to 'app') diff --git a/app/Issue/Group.hs b/app/Issue/Group.hs new file mode 100644 index 0000000..69caf20 --- /dev/null +++ b/app/Issue/Group.hs @@ -0,0 +1,47 @@ +module Issue.Group + ( groupArg, + groupIssuesBy, + ) +where + +import Data.Map (Map) +import Data.Map qualified as M +import Data.Maybe (mapMaybe) +import Data.Text qualified as T +import Issue (Issue (..)) +import Issue.Tag (Tag (..)) +import Issue.Tag qualified as I +import Options.Applicative qualified as O + +groupArg :: O.Parser (Maybe T.Text) +groupArg = + O.optional + ( O.option + (O.maybeReader (parse . T.pack)) + ( O.long "group" + <> O.metavar "TAG" + <> O.help "Group selected issues." + ) + ) + where + parse s + | "@" `T.isPrefixOf` s = Just (T.drop 1 s) + | otherwise = Nothing + +groupIssuesBy :: T.Text -> [Issue] -> Map T.Text [Issue] +groupIssuesBy group issues = + foldl + ( \collected issue -> + foldl + (flip $ M.alter (Just . maybe [issue] (issue :))) + collected + (groupsOfIssue group issue) + ) + M.empty + issues + +groupsOfIssue :: T.Text -> Issue -> [T.Text] +groupsOfIssue group issue = + mapMaybe I.tagValue + . filter (\(Tag key _) -> key == group) + $ issue.tags ++ issue.internalTags diff --git a/app/Issue/GroupBy.hs b/app/Issue/GroupBy.hs deleted file mode 100644 index 18859f2..0000000 --- a/app/Issue/GroupBy.hs +++ /dev/null @@ -1,45 +0,0 @@ -module Issue.GroupBy - ( groupByArg, - groupIssuesBy, - ) -where - -import Data.Map (Map) -import Data.Map qualified as M -import Data.Maybe (mapMaybe) -import Data.Text qualified as T -import Issue (Issue (..)) -import Issue.Tag (Tag (..)) -import Issue.Tag qualified as I -import Options.Applicative qualified as O - -groupByArg :: O.Parser (Maybe T.Text) -groupByArg = - O.optional - ( O.option - (O.maybeReader (parse . T.pack)) - ( O.long "group-by" - <> O.metavar "TAG" - <> O.help "Group selected issues." - ) - ) - where - parse s - | "@" `T.isPrefixOf` s = Just (T.drop 1 s) - | otherwise = Nothing - -groupIssuesBy :: T.Text -> [Issue] -> Map T.Text [Issue] -groupIssuesBy groupBy issues = - foldl - ( \collected issue -> - foldl - (flip $ M.alter (Just . maybe [issue] (issue :))) - collected - (groupsOfIssue groupBy issue) - ) - M.empty - issues - where - groupsOfIssue group issue = - mapMaybe I.tagValue $ - filter (\(Tag key _) -> key == group) (issue.tags ++ issue.internalTags) diff --git a/app/Main.hs b/app/Main.hs index a51ba1e..d6a263a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -422,7 +422,7 @@ import Issue (Issue (..)) import Issue qualified as I import Issue.Filter (Filter, applyFilters) import Issue.Filter qualified as I -import Issue.GroupBy qualified as I +import Issue.Group qualified as I import Issue.Provenance qualified as I import Issue.Sort (Sort, applySorts) import Issue.Sort qualified as I @@ -514,7 +514,7 @@ data Command { files :: [String], filters :: [Filter], sort :: [Sort], - groupBy :: Maybe T.Text + group :: Maybe T.Text } | Log | Show @@ -543,7 +543,7 @@ listCmd = <$> filesArg <*> I.filterArg <*> I.sortArg - <*> I.groupByArg + <*> I.groupArg logCmd :: O.Parser Command logCmd = @@ -586,10 +586,10 @@ main :: IO () main = do settings <- readSettings O.execParser (O.info (options <**> O.helper) O.idm) >>= \case - Options {colorize, noPager, width, command = List {sort, filters, files, groupBy = Just groupBy}} -> do + Options {colorize, noPager, width, command = List {sort, filters, files, group = Just group}} -> do let withinPath issue = if null files then True else any (\file -> file `isPrefixOf` issue.file) files ungroupedIssues <- applySorts sort . applyFilters filters . filter withinPath . fst <$> getHistory - let groupedIssues = I.groupIssuesBy groupBy ungroupedIssues + let groupedIssues = I.groupIssuesBy group ungroupedIssues putDoc colorize noPager width . P.vsep . intersperse ("" :: P.Doc ann) @@ -598,7 +598,7 @@ main = do P.vsep $ ( P.annotate P.bold . P.annotate (P.color P.Yellow) $ ( ( P.annotate P.bold $ - (("@" :: P.Doc ann) <> P.pretty groupBy) <+> P.pretty name + (("@" :: P.Doc ann) <> P.pretty group) <+> P.pretty name ) <+> ("(" :: P.Doc ann) <> P.pretty (length issues) @@ -612,7 +612,7 @@ main = do issues ) (M.toList groupedIssues) - Options {internalTags, colorize, noPager, width, command = List {sort, filters, files, groupBy = Nothing}} -> do + Options {internalTags, colorize, noPager, width, command = List {sort, filters, files, group = Nothing}} -> do let withinPath issue = if null files then True else any (\file -> file `isPrefixOf` issue.file) files issues <- applySorts sort . applyFilters filters . filter withinPath . fst <$> getHistory putDoc colorize noPager width . P.vsep $ -- cgit v1.2.3