aboutsummaryrefslogtreecommitdiffstats
path: root/app/Main.hs
blob: b535335c72e06151939a5ddb09db5611d4454355 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PartialTypeSignatures #-}

-- TODO Edit issues.
--
-- I would like to edit issues from the command line.
--
-- `anissue edit @id` should bring up the issue inside `$EDITOR`, updating the issue with any changes made within `$EDITOR`.
--
-- Comment markers should be stripped, and the file format should be Markdown. Issue markers could be stripped.
--
-- I am personally anticipating that this is used most frequently in response to `anissue show @id`. So, maybe `anissue show @id --edit` should be a synonym?
-- Rationale: the latter is typing `<UP> --edit<CR>`. The former, after show, is typing `<UP><ESC>^wcwedit<CR>`, the former obviously being more palatable.

-- TODO Tag improvements (OR-filtering).
--
-- Currently it is not possible to filter for an issue satisfying one filter or another. We could add the following syntax allowing it:
--
-- `--filter '(@assigned aforemny) OR (@due 2023-10-4)'`
-- @topic tags

-- TODO Tag improvements (globbing).
--
-- I would like to filter `--filter '@assigned *@posteo.de'`.
-- @topic tags

-- TODO Tag improvements (priorities)
--
-- I would like anissue to support priorities when filtering. Let's for a first implementation say that priorities are represented by an integer-values `@priority` tag on an issue. The `--filter` can be extended for filtering integer-valued tags, and the following syntax:
--
-- - `--filter '@priority >1'`, `--filter '@priority <1`
-- - `--filter '@priority >=1'`, `--filter '@priority <=1`
--
-- At some later point, we can configure `high`, `medium`, `low` to mean
-- `1,2,3`, `4,5,6`, `7,8,9` respectively.
--
-- Issues having a `@priority` tag whose value is not an integer should be regarded not matching the filter.
-- @topic tags

-- TODO Tag improvements (dates).
--
-- I would like anissue to support due dates when filtering. Let's for a first implementation add the following filter syntax:
--
-- `--filter '@due 2023-10-04'` for all issues that are marked `@due 2023-10-04` or with an earlier `@due` date.
--
-- Issues having a `@due` tag whose value does not follow that date format precisely should be regarded not matching the filter.
-- @topic tags

-- TODO Add support for ammendments
--
-- The user can ammend more information to an issue which is located at
-- a different place by referencing the issue's id.  Example:
--
-- ```bash
-- #!/usr/bin/env bash
--
-- set -efu
--
-- ls -al
-- # TODO Original issue
-- #
-- # @id original-issue
--
-- ls
-- # @original-issue more information on the issue
--
-- edited
-- ```

-- TODO Add options to show command to display source code around the issue
--
-- The `show` command should have the command line arguments
-- `--after/-A`, `--before/-B` and `--context/-C` which take an integer
-- n and change the output to contain n lines after, before or around
-- the issue.
--
-- @topic show
-- @topic options

-- TODO Expose all command line options as environment variables
--
-- It should be possible to provide a default for any command line
-- argument via an environment variable.  The name of the variable
-- should be all upper case and prefixed with `ANISSUE_`, e.g. the
-- default value for the command line option `--internal-tags` should be
-- settable via `ANISSUE_INTERNAL_TAGS`.
--
-- @topic options
-- @topic settings

-- TODO Add a subcommand which appends the generated ids to the issue in the sourcecode
--
-- Given the following issue
--
-- ```
-- # TODO Some title
-- #
-- # Some description
-- ```
--
-- After running `anissue lint`, the issue will be changed to
--
-- ```
-- # TODO Some title
-- #
-- # Some description
-- #
-- # @id some-title
-- ```
--
-- @topic ids

-- FIXME Exclude tags in code blocks
--
-- Tags which appear in markdown code blocks should not be considered
-- tags.
--
-- @topic tags

-- TODO Only separate generated tags with a blank line when description does not end with tags
--
-- An issue like
--
-- ```
-- # TODO Some title
-- #
-- # @id some-title
-- ```
--
-- Should be rendered as
--
-- ```
-- # TODO Some title
--
-- @id some-title
-- @file the-file.sh
-- @row 42
-- ```
--
-- @topic tags

-- QUESTION Should automatically generated ids be random?
--
-- Right now default id of an issue is the slugification of it's title.
-- Once we have a command to automatically append this id to the issue
-- within the source code, users might feel tempted to change the id
-- after they have adjusted the title. This then would break the
-- provenance of the issue.  If the generated id was something like
-- `tooth-cherry-switch`, it would be just a random string and noone
-- would feel the need to change it.
--
-- I imagine a workflow where I add issues in the source code.  And
-- before commiting, I run `anissue lint` to generate ids for all new
-- issues.
--
-- @topic ids

-- TODO Dependencies between issues
--
-- The user should be able to specify that an issue is blocked by
-- a different issue by adding `@blocker id-of-blocking-issue`.
--
-- When running `anissue list --show-dependencies`, the output should
-- render a list of trees deduced from these blocker-relations. E.g.
--
-- ```
-- Issue A
-- ├ Issue A1 (blocked by A)
-- │ ├ Issue A11 (blocked by A1)
-- ├─┴ Issue A3A11 (blocked by A and A1)
-- └ Issue A2B1 (blocked by A and B)
-- Issue B
-- └ Issue A2B1 (blocked by A and B)
-- Issue C
-- ```
--
-- The items should be ordered with the most blocking issues at the top.
--
-- @topic dependencies

-- FIXME Render all unicode symbols correctly
--
-- Some symbols like e.g. `╬` are not rendered properly.

-- TODO Add fulltext search
--
-- Additional to `--filter` it should be possible to search for issues
-- using `--search 'some query'` using a search-engine like full text
-- search.
--
-- To make this fast, we could use a package like
-- <https://hackage.haskell.org/package/full-text-search> and make sure
-- to persist the index in a local cache.
--
-- @topic search

-- TODO Display issue type in list and show views
--
-- Depending on the type of issue (TODO, FIXME, ...) there should be
-- either a prefix in the list view (e.g. 🏗️, 🐞, ...), or the list
-- should be grouped by the type.

-- TODO Add option to group issue list
--
-- When running `anissue list` with the `--group-by` option, the output
-- will be grouped.  E.g. running `anissue list --group-by topic` would
-- produce
--
-- ```
-- Issue D
-- Issue E
--
-- @topic tags
-- Issue A
-- Issue B @topic ids
--
-- @topic ids
-- Issue B @topic tags
-- Issue C
-- ```
--
-- Other possible grouping options could be
--
-- type
-- : the issue type, i.e. `TODO`, `FIXME`, `QUESTION`, etc.
--
-- priority
-- : the priority, groups should be sorted with the highest priority
-- first
--
-- @topic options

-- FIXME Crash when displaying some unicode emojis
--
-- Running `anissue show tool-crashes-when-displaying-unicode-emojis`
-- will crash because of 🏗️.

module Main where

import Data.List (find)
import Data.Maybe (catMaybes, fromMaybe)
import Data.String qualified as String
import Data.Text qualified as T
import History (listIssues)
import Issue (Issue (..))
import Issue qualified as I
import Issue.Filter (Filter)
import Issue.Filter qualified as I
import Issue.Tag qualified as I
import Options.Applicative ((<**>))
import Options.Applicative qualified as O
import Prettyprinter qualified as P
import Prettyprinter.Render.Terminal qualified as P
import Process (sh_)
import System.Exit (ExitCode (ExitFailure), exitWith)
import System.Process.Typed qualified as P
import Text.Printf
import TreeGrepper.Match qualified as G
import Prelude hiding (id)

data Command
  = List
      { files :: [String],
        filters :: [Filter],
        internalTags :: Bool
      }
  | Show
      { id :: String,
        width :: Maybe Int
      }
  deriving (Show)

cmd :: O.Parser Command
cmd =
  O.hsubparser . mconcat $
    [ O.command "list" . O.info listCmd $
        O.progDesc "List all issues",
      O.command "show" . O.info showCmd $
        O.progDesc "Show details of all issues"
    ]

listCmd :: O.Parser Command
listCmd =
  List
    <$> filesArg
    <*> I.filterArg
    <*> internalTagsFlag

showCmd :: O.Parser Command
showCmd =
  Show
    <$> idArg
    <*> widthOption

filesArg :: O.Parser [String]
filesArg = O.many (O.strArgument (O.metavar "FILE" <> O.action "file"))

internalTagsFlag :: O.Parser Bool
internalTagsFlag =
  O.switch
    ( O.long "internal-tags"
        <> O.help "Whether to display internal tags."
    )

idArg :: O.Parser String
idArg =
  O.strArgument
    ( O.metavar "ID"
        <> O.completer
          ( O.listIOCompleter $
              catMaybes . map I.id <$> listIssues [] []
          )
    )

widthOption :: O.Parser (Maybe Int)
widthOption =
  O.optional
    ( O.option
        O.auto
        ( O.long "width"
            <> O.short 'w'
            <> O.metavar "INT"
            <> O.help "Wheather to insert line breaks after at most that many characters."
        )
    )

die :: String -> IO a
die s = do
  printf "error: %s\n" s
  exitWith (ExitFailure 1)

main :: IO ()
main = do
  O.execParser (O.info (cmd <**> O.helper) O.idm) >>= \case
    List {filters, files, internalTags} -> do
      issues <- listIssues filters files
      putDoc . P.vsep $
        map
          ( \issue ->
              P.hsep
                ( concat
                    [ [P.annotate P.bold (P.pretty issue.title)],
                      map
                        ( \(I.Tag k v) ->
                            P.annotate (P.colorDull P.Yellow) $
                              P.pretty ("@" `T.append` k `T.append` " " `T.append` v)
                        )
                        ( issue.tags
                            ++ if internalTags then issue.internalTags else []
                        )
                    ]
                )
          )
          issues
    Show {id, width} -> do
      issues <- listIssues [] []
      case find ((==) (Just id) . I.id) issues of
        Nothing -> die (printf "no issue with id `%s'\n" id)
        Just issue -> do
          putDoc $
            P.annotate (P.color P.Green) $
              P.pretty $
                issue.file
                  ++ ":"
                  ++ show issue.start.row
                  ++ ( case issue.provenance of
                         Nothing ->
                           "HEAD"
                         Just provenance ->
                           "\nvia "
                             ++ T.unpack provenance.firstCommit
                             ++ "\nby "
                             ++ T.unpack provenance.authorName
                             ++ " <"
                             ++ T.unpack provenance.authorEmail
                             ++ ">\nat "
                             ++ show provenance.date
                     )
                  ++ "\n\n"
          sh_
            ( P.setStdin
                ( String.fromString
                    ( "# "
                        ++ T.unpack issue.title
                        ++ "\n\n"
                        ++ fromMaybe "" (fmap T.unpack issue.description)
                    )
                )
                ( case width of
                    Nothing ->
                      "mdcat --local"
                    Just width' ->
                      String.fromString (printf "mdcat --columns %d --local" width')
                )
            )
          putDoc $
            P.pretty $
              "\n@file "
                ++ issue.file
                ++ "\n@row "
                ++ show issue.start.row
                ++ "\n"

putDoc :: P.Doc P.AnsiStyle -> IO ()
putDoc doc = do
  isTty <- (== 1) <$> c_isatty 1
  P.putDoc . (if isTty then (\x -> x) else P.unAnnotate) $ doc

foreign import ccall "unistd.h isatty" c_isatty :: Int -> IO Int