From 9258461e3ce99b968558899eb3f2d8d1da43e964 Mon Sep 17 00:00:00 2001 From: q9i <46249765+quantum9Innovation@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:21:55 -0700 Subject: [PATCH] refactor: create pure module `Pure` contains functions for processing data and building commands. The module includes functions for reading lines of input, formatting time, and constructing a command with a given script and time. --- app/Main.hs | 18 +----------------- app/Pure.hs | 20 ++++++++++++++++++++ suntheme.cabal | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 app/Pure.hs diff --git a/app/Main.hs b/app/Main.hs index aba8926..9d51013 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,12 +5,12 @@ module Main where +import Pure (buildCmd, kill, readLines) import Const (darkModeScript, lightModeScript, prog, query) import Types (ResponseMsg(..), ResponseCode(..), Status(..), genericErr, toResponseMsg) import Data.Time (ZonedTime, getCurrentTime) import Data.Time.Solar (Location(Location), sunrise, sunset) -import Data.Time.RFC3339 (formatTimeRFC3339) import Data.Time.LocalTime (getTimeZone, utcToZonedTime, zonedTimeToUTC) import Data.List.Extra ((!?)) import Data.ByteString.Char8 (unpack) @@ -77,10 +77,6 @@ ping run err = do runner = run r in destruct status runner err -readLines :: [String] -> Maybe (String, Double, Double, String) -readLines [msg, latStr, lonStr, tz] = Just (msg, read latStr, read lonStr, tz) -readLines _ = Nothing - process :: Response -> (Double -> Double -> String -> IO ()) -> IO () -> IO () process r run err = case info of @@ -130,9 +126,6 @@ backupRunner = do exec :: String -> (SomeException -> IO String) -> IO String exec cmd err = do catch (readProcess "bash" ["-c", cmd] "") err -kill :: String -> String -kill = (++) "atrm " - killall :: [String] -> IO () killall = foldr ((>>) . dispatch . kill) (return ()) where @@ -160,15 +153,6 @@ finish queue = do getId = head . words . last . lines failure = print :: SomeException -> IO () -after :: (Eq a) => a -> [a] -> [a] -after c = drop 1 . dropWhile (/= c) - -formatTime :: ZonedTime -> String -formatTime = take 5 . after 'T' . formatTimeRFC3339 - -buildCmd :: String -> ZonedTime -> String -buildCmd script time = "echo \"" ++ script ++ "\" | at " ++ formatTime time - activateOnSunrise :: ZonedTime -> ZonedTime -> IO Bool activateOnSunrise sunriseTime sunsetTime = do timeNow <- now diff --git a/app/Pure.hs b/app/Pure.hs new file mode 100644 index 0000000..30ceef1 --- /dev/null +++ b/app/Pure.hs @@ -0,0 +1,20 @@ +module Pure where + +import Data.Time (ZonedTime) +import Data.Time.RFC3339 (formatTimeRFC3339) + +readLines :: [String] -> Maybe (String, Double, Double, String) +readLines [msg, latStr, lonStr, tz] = Just (msg, read latStr, read lonStr, tz) +readLines _ = Nothing + +kill :: String -> String +kill = (++) "atrm " + +after :: (Eq a) => a -> [a] -> [a] +after c = drop 1 . dropWhile (/= c) + +formatTime :: ZonedTime -> String +formatTime = take 5 . after 'T' . formatTimeRFC3339 + +buildCmd :: String -> ZonedTime -> String +buildCmd script time = "echo \"" ++ script ++ "\" | at " ++ formatTime time diff --git a/suntheme.cabal b/suntheme.cabal index 801f81d..d2c530e 100644 --- a/suntheme.cabal +++ b/suntheme.cabal @@ -62,7 +62,7 @@ executable suntheme main-is: Main.hs -- Modules included in this executable, other than Main. - other-modules: Types, Const + other-modules: Types, Const, Pure -- LANGUAGE extensions used by modules in this package. -- other-extensions: