X.P.OrgMode: Fallback to "today" if no day is given

This commit is contained in:
Tony Zorman 2024-03-29 11:02:41 +01:00
parent 8ac84079a2
commit d691d25d1c
2 changed files with 10 additions and 4 deletions

View File

@ -533,7 +533,7 @@ pInput inp = (`runParser` inp) . choice $
-- | Parse a 'Priority'. -- | Parse a 'Priority'.
pPriority :: Parser Priority pPriority :: Parser Priority
pPriority = option NoPriority $ pPriority = option NoPriority $
" " *> skipSpaces *> choice skipSpaces *> choice
[ "#" *> foldCase "a" $> A [ "#" *> foldCase "a" $> A
, "#" *> foldCase "b" $> B , "#" *> foldCase "b" $> B
, "#" *> foldCase "c" $> C , "#" *> foldCase "c" $> C
@ -546,7 +546,7 @@ pTimeOfDay = option Nothing $
[ TimeOfDay <$> pHour <* ":" <*> pMinute -- HH:MM [ TimeOfDay <$> pHour <* ":" <*> pMinute -- HH:MM
, pHHMM -- HHMM , pHHMM -- HHMM
, TimeOfDay <$> pHour <*> pure 0 -- HH , TimeOfDay <$> pHour <*> pure 0 -- HH
] ] <* (void " " <|> eof)
where where
pHHMM :: Parser TimeOfDay pHHMM :: Parser TimeOfDay
pHHMM = do pHHMM = do
@ -566,6 +566,7 @@ pDate = skipSpaces *> choice
, pPrefix "tom" "orrow" Tomorrow , pPrefix "tom" "orrow" Tomorrow
, Next <$> pNext , Next <$> pNext
, Date <$> pDate' , Date <$> pDate'
, pure Today -- Fallback to today if no date was given.
] ]
where where
pNext :: Parser DayOfWeek = choice pNext :: Parser DayOfWeek = choice
@ -585,7 +586,7 @@ pDate = skipSpaces *> choice
pDate' :: Parser (Int, Maybe Int, Maybe Integer) pDate' :: Parser (Int, Maybe Int, Maybe Integer)
pDate' = pDate' =
(,,) <$> pNumBetween 1 31 -- day (,,) <$> (pNumBetween 1 31 <* (void " " <|> eof)) -- day
<*> optional (skipSpaces *> choice <*> optional (skipSpaces *> choice
[ pPrefix "ja" "nuary" 1 , pPrefix "f" "ebruary" 2 [ pPrefix "ja" "nuary" 1 , pPrefix "f" "ebruary" 2
, pPrefix "mar" "ch" 3 , pPrefix "ap" "ril" 4 , pPrefix "mar" "ch" 3 , pPrefix "ap" "ril" 4

View File

@ -56,6 +56,11 @@ spec = do
(Time {date = Date (22, Just 1, Just 2021), tod = Just $ TimeOfDay 1 1}) (Time {date = Date (22, Just 1, Just 2021), tod = Just $ TimeOfDay 1 1})
B B
) )
it "parses no day as today when given a time" $ do
pInput "todo +s 12:00"
`shouldBe` Just (Scheduled "todo" (Time {date = Today, tod = Just $ TimeOfDay 12 0}) NoPriority)
pInput "todo +d 14:05 #B"
`shouldBe` Just (Deadline "todo" (Time {date = Today, tod = Just $ TimeOfDay 14 5}) B)
context "no priority#b" $ do context "no priority#b" $ do
it "parses to the correct thing" $ it "parses to the correct thing" $
@ -179,7 +184,7 @@ instance Arbitrary Date where
[ pure Today [ pure Today
, pure Tomorrow , pure Tomorrow
, Next . toEnum <$> choose (0, 6) , Next . toEnum <$> choose (0, 6)
, do d <- posInt , do d <- posInt `suchThat` (<= 31)
m <- mbPos `suchThat` (<= Just 12) m <- mbPos `suchThat` (<= Just 12)
Date . (d, m, ) <$> if isNothing m Date . (d, m, ) <$> if isNothing m
then pure Nothing then pure Nothing