#!/usr/bin/env stack --stack --install-ghc runghc module CountdownLoop (countdown) where import System.Environment (getArgs) import System.Console.ANSI (saveCursor, restoreCursor) import System.IO (hFlush, stdout) import Control.Concurrent (threadDelay) import ParseTime (count_down_time) 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" = putStrLn "00:00" >> return "00:00" countdown_loop delayer s = do saveCursor putStr s hFlush stdout delayer restoreCursor (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