aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 6ec23de3bbcdb608dbe96df766c16a53de2cebe7 (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
module Main where

import Control.DeepSeq (force)
import Control.Exception (evaluate)
import Data.Char (toLower)
import Data.List (isInfixOf, isPrefixOf, sortOn, stripPrefix)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import Data.Ord (Down (..))
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format (defaultTimeLocale, formatTime)
import System.Directory
import System.Environment
import System.FilePath ((</>))
import Text.Read (readMaybe)

data Status
  = CLOSED
  | IN_PROGRESS
  | OPEN
  deriving (Show, Eq, Enum, Bounded, Read, Ord)

data Task = Task
  { priority :: Int
  , desc     :: String
  , status   :: Status
  } deriving (Show, Eq)

data AppError
  = TaskNotFound FilePath
  | InvalidStatus String
  | InvalidPriority String
  | MissingArgument String
  | UnknownOption String
  | UnexpectedArgument String
  deriving (Eq)

instance Show AppError where
  show (TaskNotFound path)      = "Could not find task file: " ++ path </> "Task.md"
  show (InvalidStatus s)        = "Invalid status: " ++ s ++ ". Use OPEN, IN_PROGRESS, or CLOSED."
  show (InvalidPriority p)      = "Invalid priority: " ++ p ++ ". Must be an integer."
  show (MissingArgument flag)   = "Missing value after " ++ flag
  show (UnknownOption flag)     = "Unknown option: " ++ flag
  show (UnexpectedArgument arg) = "Unexpected argument: " ++ arg

data TaskRecord = TaskRecord
  { recordDir  :: FilePath
  , recordName :: String
  , recordTask :: Task
  } deriving (Show, Eq)

recordFile :: TaskRecord -> FilePath
recordFile r = recordDir r </> "Task.md"

data NewTaskOptions = NewTaskOptions
  { optPriority :: Maybe Int
  , optDesc     :: Maybe String
  } deriving (Show, Eq)

data ListOptions = ListOptions
  { filterStatus      :: Maybe Status
  , filterPriority    :: Maybe Int
  , filterMinPriority :: Maybe Int
  , filterContains    :: Maybe String
  } deriving (Show, Eq)

data SetOptions = SetOptions
  { setName     :: Maybe String
  , setPriority :: Maybe Int
  , setDesc     :: Maybe String
  , setStatus   :: Maybe Status
  } deriving (Show, Eq)

defaultNewOptions :: NewTaskOptions
defaultNewOptions = NewTaskOptions Nothing Nothing

parseNewTaskOptions :: [String] -> Either AppError NewTaskOptions
parseNewTaskOptions = go defaultNewOptions
  where
    go opts []              = Right opts
    go opts ("-p" : p : rs) = readPriority p >>= \n -> go opts { optPriority = Just n } rs
    go _    ("-p" : [])     = Left (MissingArgument "-p")
    go opts ("-d" : d : rs) = go opts { optDesc = Just d } rs
    go _    ("-d" : [])     = Left (MissingArgument "-d")
    go _    (f    : _) | "-" `isPrefixOf` f = Left (UnknownOption f)
    go _    (a    : _)      = Left (UnexpectedArgument a)

defaultListOptions :: ListOptions
defaultListOptions = ListOptions Nothing Nothing Nothing Nothing

parseListOptions :: [String] -> Either AppError ListOptions
parseListOptions = go defaultListOptions
  where
    go opts [] = Right opts
    go opts (flag : val : rs)
      | flag `elem` ["--status",       "-s"] = readStatus val   >>= \s -> go opts { filterStatus      = Just s } rs
      | flag `elem` ["--priority",     "-p"] = readPriority val >>= \n -> go opts { filterPriority    = Just n } rs
      | flag `elem` ["--min-priority"      ] = readPriority val >>= \n -> go opts { filterMinPriority = Just n } rs
      | flag `elem` ["--contains",     "-c"] = go opts { filterContains = Just val } rs
    go _ (flag : [])
      | flag `elem` ["--status", "-s", "--priority", "-p", "--min-priority", "--contains", "-c"] =
          Left (MissingArgument flag)
    go _ (f : _) | "-" `isPrefixOf` f = Left (UnknownOption f)
    go _ (a : _)                       = Left (UnexpectedArgument a)

defaultSetOptions :: SetOptions
defaultSetOptions = SetOptions Nothing Nothing Nothing Nothing

parseSetOptions :: [String] -> Either AppError SetOptions
parseSetOptions = go defaultSetOptions
  where
    go opts [] = Right opts
    go opts (flag : val : rs)
      | flag `elem` ["--name",     "-n"] = go opts { setName     = Just val } rs
      | flag `elem` ["--priority", "-p"] = readPriority val >>= \n -> go opts { setPriority = Just n } rs
      | flag `elem` ["--desc",     "-d"] = go opts { setDesc     = Just val } rs
      | flag `elem` ["--status",   "-s"] = readStatus val   >>= \s -> go opts { setStatus   = Just s } rs
    go _ (flag : [])
      | flag `elem` ["--name", "-n", "--priority", "-p", "--desc", "-d", "--status", "-s"] =
          Left (MissingArgument flag)
    go _ (f : _) | "-" `isPrefixOf` f = Left (UnknownOption f)
    go _ (a : _)                       = Left (UnexpectedArgument a)

readPriority :: String -> Either AppError Int
readPriority s = maybe (Left (InvalidPriority s)) Right (readMaybe s)

readStatus :: String -> Either AppError Status
readStatus s = maybe (Left (InvalidStatus s)) Right (readMaybe s)

usage :: String -> String
usage prog = unlines
  [ "Usage: " ++ prog ++ " <command> [options]"
  , ""
  , "Commands:"
  , "  new    TASK_NAME [-p PRIORITY] [-d DESCRIPTION]"
  , "  list   [--status STATUS] [--priority N] [--min-priority N] [--contains TEXT]"
  , "  set    TASK_DIR [--name NAME] [--status STATUS] [--priority N] [--desc DESC]"
  , "  delete TASK_DIR"
  , ""
  , "Statuses: OPEN | IN_PROGRESS | CLOSED"
  ]

makeTaskMarkdown :: String -> Task -> String
makeTaskMarkdown taskName task =
  unlines
    [ "# " ++ taskName
    , ""
    , "- Description: " ++ desc task
    , "- Priority: "    ++ show (priority task)
    , "- Status: "      ++ show (status task)
    ]

parseFields :: [String] -> Map String String
parseFields = Map.fromList . concatMap parseLine
  where
    parseLine line =
      case stripPrefix "- " line of
        Nothing   -> []
        Just rest ->
          case break (== ':') rest of
            (key, ':' : ' ' : val) -> [(key, val)]
            _                      -> []

readTaskRecord :: FilePath -> IO (Either AppError TaskRecord)
readTaskRecord taskDir = do
  let taskFile = taskDir </> "Task.md"
  exists <- doesFileExist taskFile
  if not exists
    then pure (Left (TaskNotFound taskDir))
    else do
      contents <- readFile taskFile >>= evaluate . force
      let ls       = lines contents
          fields   = parseFields ls
          taskName = case ls of
            firstLine : _ -> fromMaybe firstLine (stripPrefix "# " firstLine)
            []            -> taskDir
          look k = Map.lookup k fields
          task = Task
            { priority = fromMaybe 0    (look "Priority" >>= readMaybe)
            , desc     = fromMaybe ""   (look "Description")
            , status   = fromMaybe OPEN (look "Status"   >>= readMaybe)
            }
      pure $ Right $ TaskRecord
        { recordDir  = taskDir
        , recordName = taskName
        , recordTask = task
        }

writeTaskRecord :: TaskRecord -> IO ()
writeTaskRecord record =
  writeFile (recordFile record) (makeTaskMarkdown (recordName record) (recordTask record))

matchesListOptions :: ListOptions -> TaskRecord -> Bool
matchesListOptions opts record =
  all id
    [ maybe True (status task   ==) (filterStatus opts)
    , maybe True (priority task ==) (filterPriority opts)
    , maybe True (priority task >=) (filterMinPriority opts)
    , maybe True (\needle -> map toLower needle `isInfixOf` searchableText) (filterContains opts)
    ]
  where
    task = recordTask record
    searchableText = map toLower $ unwords
      [ recordName record
      , desc task
      , show (status task)
      , show (priority task)
      ]

printTaskRecord :: TaskRecord -> IO ()
printTaskRecord record =
  putStrLn $
    recordFile record ++ ":1:1: "
      ++ "[" ++ show (status task) ++ "] "
      ++ "P"  ++ show (priority task) ++ " "
      ++ recordName record
      ++ descPart
  where
    task = recordTask record
    descPart = case desc task of
      "" -> ""
      d  -> " -- " ++ d

taskDirFormat :: String
taskDirFormat = "%y-%m-%d %H:%M:%S"

isTask :: FilePath -> Bool
isTask d = isBaseTask base && validSuffix rest
  where
    (base, rest) = splitAt 17 d
    isBaseTask s =
      length s == 17
        && isDatePart (take 8 s)
        && s !! 8 == ' '
        && isTimePart (drop 9 s)
    validSuffix ""             = True
    validSuffix ('.' : digits) = not (null digits) && allDigits digits
    validSuffix _              = False
    isDatePart s =
      length s == 8
        && allDigits (take 2 s)
        && s !! 2 == '-'
        && allDigits (take 2 (drop 3 s))
        && s !! 5 == '-'
        && allDigits (drop 6 s)
    isTimePart s =
      length s == 8
        && allDigits (take 2 s)
        && s !! 2 == ':'
        && allDigits (take 2 (drop 3 s))
        && s !! 5 == ':'
        && allDigits (drop 6 s)
    allDigits = all (`elem` ['0' .. '9'])

tasksSubdir :: FilePath
tasksSubdir = "tasks"

findTasksRoot :: IO FilePath
findTasksRoot = do
  hasSub <- doesDirectoryExist tasksSubdir
  pure $ if hasSub then tasksSubdir else "."

newTaskDirName :: FilePath -> IO FilePath
newTaskDirName root = do
  now <- getCurrentTime
  let base = formatTime defaultTimeLocale taskDirFormat now
  findFree base (0 :: Int)
  where
    findFree base n = do
      let name      = if n == 0 then base else base ++ "." ++ show n
          candidate = root </> name
      exists <- doesDirectoryExist candidate
      if not exists then pure candidate else findFree base (n + 1)

loadAllRecords :: IO [TaskRecord]
loadAllRecords = do
  root    <- findTasksRoot
  files   <- listDirectory root
  results <- mapM (readTaskRecord . (root </>)) (filter isTask files)
  pure $ sortOn (Down . priority . recordTask) [r | Right r <- results]

resolveTaskDir :: FilePath -> IO FilePath
resolveTaskDir d
  | isTask d  = findTasksRoot >>= \root -> pure (root </> d)
  | otherwise = pure d

printChanges :: TaskRecord -> TaskRecord -> IO ()
printChanges old new = mapM_ report3
  [ ("name",     recordName old,                     recordName new)
  , ("priority", show . priority . recordTask $ old,  show . priority . recordTask $ new)
  , ("desc",     desc . recordTask $ old,             desc . recordTask $ new)
  , ("status",   show . status . recordTask $ old,    show . status . recordTask $ new)
  ]
  where
    report3 (field, oldVal, newVal)
      | oldVal /= newVal = putStrLn $ "  " ++ field ++ ": " ++ oldVal ++ " -> " ++ newVal
      | otherwise        = pure ()

newTask :: String -> [String] -> IO ()
newTask programName args =
  case args of
    [] -> do
      putStrLn "Error: missing task name."
      putStrLn $ usage programName
    taskName : rest ->
      case parseNewTaskOptions rest of
        Left err -> do
          putStrLn (show err)
          putStrLn $ usage programName
        Right opts -> do
          root        <- findTasksRoot
          taskDirName <- newTaskDirName root
          let task = Task
                { priority = fromMaybe 0  (optPriority opts)
                , desc     = fromMaybe "" (optDesc opts)
                , status   = OPEN
                }
              record = TaskRecord
                { recordDir  = taskDirName
                , recordName = taskName
                , recordTask = task
                }
          createDirectory taskDirName
          writeTaskRecord record
          putStrLn $ recordFile record ++ ":1:1: Created task: " ++ taskName

listTasks :: String -> [String] -> IO ()
listTasks programName args =
  case parseListOptions args of
    Left err -> do
      putStrLn (show err)
      putStrLn $ usage programName
    Right opts -> do
      records <- loadAllRecords
      let filtered = filter (matchesListOptions opts) records
      if null filtered
        then putStrLn "(no matching tasks)"
        else mapM_ printTaskRecord filtered

setTask :: String -> [String] -> IO ()
setTask programName args =
  case args of
    [] -> putStrLn $ usage programName
    taskDir : rest ->
      case parseSetOptions rest of
        Left err -> do
          putStrLn (show err)
          putStrLn $ usage programName
        Right opts -> do
          resolved <- resolveTaskDir taskDir
          result   <- readTaskRecord resolved
          case result of
            Left err -> putStrLn (show err)
            Right record -> do
              let old = recordTask record
                  updatedTask = old
                    { priority = fromMaybe (priority old) (setPriority opts)
                    , desc     = fromMaybe (desc old)     (setDesc opts)
                    , status   = fromMaybe (status old)   (setStatus opts)
                    }
                  updatedRecord = record
                    { recordName = fromMaybe (recordName record) (setName opts)
                    , recordTask = updatedTask
                    }
              printChanges record updatedRecord
              writeTaskRecord updatedRecord
              putStrLn $ recordFile updatedRecord ++ ":1:1: Updated task: " ++ recordName updatedRecord

deleteTask :: String -> [String] -> IO ()
deleteTask programName args =
  case args of
    [] -> putStrLn $ usage programName
    taskDir : _ -> do
      resolved <- resolveTaskDir taskDir
      result   <- readTaskRecord resolved
      case result of
        Left err     -> putStrLn (show err)
        Right record -> do
          removeDirectoryRecursive (recordDir record)
          putStrLn $ "Deleted task: " ++ recordName record ++ " (" ++ recordDir record ++ ")"

handleArgs :: String -> [String] -> IO ()
handleArgs prog args =
  case args of
    "new"    : rest -> newTask    prog rest
    "list"   : rest -> listTasks  prog rest
    "set"    : rest -> setTask    prog rest
    "delete" : rest -> deleteTask prog rest
    []              -> listTasks  prog []
    cmd      : _    -> do
      putStrLn $ "Unknown command: " ++ cmd
      putStrLn $ usage prog

main :: IO ()
main = do
  prog <- getProgName
  args <- getArgs
  handleArgs prog args