packages feed

Monadoro-0.3.5.0: src/CountdownLoop.hs

#!/usr/bin/env stack
--stack --install-ghc runghc

module CountdownLoop       (countdown, countdown_loop) where

import System.Environment  (getArgs)
import Control.Concurrent  (threadDelay)
import ParseTime           (count_down_time)
import Display             (display)

milisec_per_second :: Int
milisec_per_second = 10 ^ (6 :: Int)

{-| wait: Pauses for the given number of seconds.

>>> wait 0
-}

wait :: Int -> IO()
wait n = threadDelay (n * milisec_per_second)

with_delay :: IO()
with_delay = wait 1

-- Mocks a delay for test purposes.
no_delay :: IO()
no_delay = wait 0

{-| countdown_loop: Runs a Pomodoro timer.

>>> result <- countdown_loop no_delay "00:00"
...
>>> result
"00:00"

>>> result' <- countdown_loop no_delay "00:59"
...
>>> result'
"00:00"

>>> result'' <- countdown_loop no_delay "01:59"
...
>>> result''
"00:00"

-}

countdown :: String -> IO (String)
countdown = countdown_loop with_delay

countdown_loop :: IO() -> String -> IO (String)
countdown_loop delayer "00:00" = display "00:00" >> return "00:00"
countdown_loop delayer s = do
  display s
  delayer
  (countdown_loop delayer . count_down_time) s

parse :: [String] -> IO ()
parse [s] = countdown_loop with_delay s >> return ()
parse _   = countdown_loop with_delay "00:00" >> return ()

main :: IO ()
main = getArgs >>= parse