Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Control.Concurrent
- import Data.Time
- import Data.Time.LocalTime
- import System.Process
- ------------------------------------------------------------------------------------------------------------------------
- -- Interface
- ------------------------------------------------------------------------------------------------------------------------
- -- | Clock digit.
- data ClockDigit = Colon | Zero | One | Two | Three | Four | Five | Six | Seven | Eight | Nine
- deriving (Eq, Ord, Show)
- -- | Clock digit as ASCII text, split across three lines.
- type AsciiDigit = (String, String, String)
- -- | Generate AsciiDigit from ClockDigit.
- digitToAscii :: ClockDigit -> AsciiDigit
- digitToAscii Colon = (" ", " . ", " . ")
- digitToAscii Zero = (" _ ", "| |", "|_|")
- digitToAscii One = (" ", " | ", " | ")
- digitToAscii Two = (" _ ", " _|", "|_ ")
- digitToAscii Three = ("_ ", "_| ", "_| ")
- digitToAscii Four = (" ", "|_|", " |")
- digitToAscii Five = (" _ ", "|_ ", " _|")
- digitToAscii Six = (" _ ", "|_ ", "|_|")
- digitToAscii Seven = (" _ ", " |", " |")
- digitToAscii Eight = (" _ ", "|_|", "|_|")
- digitToAscii Nine = (" _ ", "|_|", " |")
- -- | Generate AsciiDigits from ClockDigits.
- digitsToAscii :: [ClockDigit] -> [AsciiDigit]
- digitsToAscii [] = []
- digitsToAscii (d:ds) = digitToAscii d : digitsToAscii ds
- -- | Get one of the line components from an AsciiDigit.
- getAsciiDigitLn :: Int -> AsciiDigit -> String
- getAsciiDigitLn 0 (x,_,_) = x
- getAsciiDigitLn 1 (_,x,_) = x
- getAsciiDigitLn 2 (_,_,x) = x
- getAsciiDigitLn _ _ = error "Line number must be [0,3)"
- -- | Get one of the line components from a list of AsciiDigits.
- getAsciiDigitsLn :: Int -> [AsciiDigit] -> String
- getAsciiDigitsLn i [] = []
- getAsciiDigitsLn i (d:ds) = (getAsciiDigitLn i d) ++ (getAsciiDigitsLn i ds)
- -- | Merge a list of AsciiDigits into a single AsciiDigit.
- mergeAsciiDigits :: [AsciiDigit] -> AsciiDigit
- mergeAsciiDigits ds = (getAsciiDigitsLn 0 ds, getAsciiDigitsLn 1 ds, getAsciiDigitsLn 2 ds)
- -- | Fold an AsciiDigit into a single string.
- asciiDigitToStr :: AsciiDigit -> String
- asciiDigitToStr (l0, l1, l2) = l0 ++ "\n" ++ l1 ++ "\n" ++ l2
- -- | Fold a list of AsciiDigit into a single string.
- asciiDigitsToStr :: [AsciiDigit] -> String
- asciiDigitsToStr [] = []
- asciiDigitsToStr ds = asciiDigitToStr $ mergeAsciiDigits ds
- -- | Generate a list of ClockDigits from a number.
- numberToDigits' :: Int -> [ClockDigit]
- numberToDigits' 0 = []
- numberToDigits' x
- | r == 9 = numberToDigits' (x `div` 10) ++ [Nine]
- | r == 8 = numberToDigits' (x `div` 10) ++ [Eight]
- | r == 7 = numberToDigits' (x `div` 10) ++ [Seven]
- | r == 6 = numberToDigits' (x `div` 10) ++ [Six]
- | r == 5 = numberToDigits' (x `div` 10) ++ [Five]
- | r == 4 = numberToDigits' (x `div` 10) ++ [Four]
- | r == 3 = numberToDigits' (x `div` 10) ++ [Three]
- | r == 2 = numberToDigits' (x `div` 10) ++ [Two]
- | r == 1 = numberToDigits' (x `div` 10) ++ [One]
- | r == 0 = numberToDigits' (x `div` 10) ++ [Zero]
- | otherwise = undefined
- where r = x `mod` 10
- -- | Generate a list of ClockDigits from a number, padding with zeros where necessary.
- numberToDigits :: Int -> [ClockDigit]
- numberToDigits x
- | n == 0 = Zero : Zero : y
- | n == 1 = Zero : y
- | otherwise = y
- where y = numberToDigits' x
- n = length y
- -- | Generate a list of ClockDigits from a time triple.
- timeToDigits :: (Int,Int,Int) -> [ClockDigit]
- timeToDigits (h,m,s) = (numberToDigits h) ++ (Colon : numberToDigits m) ++ (Colon : numberToDigits s)
- -- | Generate a list of AsciiDigits from a time triple.
- timeToAscii t = digitsToAscii $ timeToDigits t
- -- | Generate a string from a time triple.
- timeToStr :: (Int,Int,Int) -> String
- timeToStr t = asciiDigitsToStr $ timeToAscii t
- -- | Print a time triple as a string.
- putTime :: (Int,Int,Int) -> IO ()
- putTime t = putStrLn $ timeToStr t
- ------------------------------------------------------------------------------------------------------------------------
- -- Clock
- ------------------------------------------------------------------------------------------------------------------------
- -- | Get the time of day as a triple (hours, minutes, seconds).
- getTimeTriple :: IO (Int,Int,Int)
- getTimeTriple = do
- t <- fmap (fromRational . timeOfDayToDayFraction . localTimeOfDay . zonedTimeToLocalTime) getZonedTime
- let h = t * 24
- m = (h - fromIntegral (truncate h)) * 60
- s = (m - fromIntegral (truncate m)) * 60
- return (truncate h,truncate m,truncate s)
- ------------------------------------------------------------------------------------------------------------------------
- -- Main
- ------------------------------------------------------------------------------------------------------------------------
- -- | Sleep for a number of seconds.
- sleep :: Int -> IO ()
- sleep s = threadDelay $ s * 1000000
- unix = False
- -- | Main function.
- main :: IO ()
- main = do
- t <- getTimeTriple
- system $ if unix then "clear" else "cls"
- putTime t
- sleep 1
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement