Initial commit
This commit is contained in:
commit
23f34e71af
13 files changed
+681
No files matched your search
@@ -0,0 +1,41 @@
|
||||
module Command
|
||||
( Command(..)
|
||||
, execCommand
|
||||
)
|
||||
where
|
||||
|
||||
import Options.Applicative
|
||||
( Parser
|
||||
, argument
|
||||
, command
|
||||
, execParser
|
||||
, fullDesc
|
||||
, help
|
||||
, helper
|
||||
, info
|
||||
, metavar
|
||||
, progDesc
|
||||
, str
|
||||
, subparser
|
||||
, (<**>)
|
||||
)
|
||||
import Data.Text (Text)
|
||||
|
||||
|
||||
data Command
|
||||
= Start FilePath Text
|
||||
| Stop FilePath
|
||||
deriving Show
|
||||
|
||||
commandParser :: Parser Command
|
||||
commandParser = subparser (startCommand <> stopCommand)
|
||||
where
|
||||
stopCommand = command "stop" (info (Stop <$> pathParser <**> helper) (progDesc "Stop the current running task"))
|
||||
startCommand = command "start" (info (Start <$> pathParser <*> taskParser <**> helper) (progDesc "Start a new task or replace existing"))
|
||||
taskParser = argument str (metavar "TASK" <> help "Name of the task in 'foo.bar' format")
|
||||
pathParser = argument str (metavar "FILE" <> help "Filepath with the clocking information")
|
||||
|
||||
execCommand :: IO Command
|
||||
execCommand = execParser opts
|
||||
where
|
||||
opts = info (commandParser <**> helper) fullDesc
|
||||
@@ -0,0 +1,98 @@
|
||||
{-# LANGUAGE ApplicativeDo #-}
|
||||
{-# LANGUAGE DerivingVia #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE StrictData #-}
|
||||
module Hledger.Row
|
||||
( Task
|
||||
, task
|
||||
, getTask
|
||||
, Row(..)
|
||||
, RowContent(..)
|
||||
, Operation(..)
|
||||
, encode
|
||||
, decode
|
||||
, encodeFile
|
||||
, decodeFile
|
||||
)
|
||||
where
|
||||
|
||||
import Conduit (ConduitT, (.|))
|
||||
import Control.Applicative ((<|>))
|
||||
import qualified Data.Attoparsec.ByteString.Char8 as P
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString.Char8 as BC
|
||||
import qualified Data.Conduit.Combinators as Conduit
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as TE
|
||||
import Data.Time (LocalTime, defaultTimeLocale, formatTime, parseTimeM)
|
||||
|
||||
|
||||
newtype Task = Task { getTask :: Text }
|
||||
deriving (Eq, Show) via Text
|
||||
|
||||
task :: Text -> Maybe Task
|
||||
task "" = Nothing
|
||||
task x = Just $ Task x
|
||||
|
||||
data Operation
|
||||
= In Task
|
||||
| Out
|
||||
deriving (Eq, Show)
|
||||
|
||||
data Row
|
||||
= Row RowContent
|
||||
| Empty
|
||||
deriving (Show, Eq)
|
||||
|
||||
data RowContent = RowContent
|
||||
{ rowOperation :: Operation
|
||||
, rowTime :: LocalTime
|
||||
}
|
||||
deriving (Eq, Show)
|
||||
|
||||
encode :: Row -> ByteString
|
||||
encode Empty = ""
|
||||
encode (Row (RowContent{rowOperation = In (Task t), rowTime})) =
|
||||
"i" <> " " <>
|
||||
BC.pack (formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" rowTime) <> " " <>
|
||||
TE.encodeUtf8 t
|
||||
encode (Row (RowContent{rowOperation = Out, rowTime})) =
|
||||
"o" <> " " <>
|
||||
BC.pack (formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" rowTime)
|
||||
|
||||
decode :: ByteString -> Either String Row
|
||||
decode = P.parseOnly ((Row <$> parseRow) <|> (Empty <$ parseEmpty))
|
||||
where
|
||||
parseEmpty = P.skipSpace <* P.endOfInput
|
||||
parseRow :: P.Parser RowContent
|
||||
parseRow = do
|
||||
P.skipSpace
|
||||
-- See the data model above, the operation is dependent on the task,
|
||||
-- hence the monadic parser
|
||||
op <- parseOp
|
||||
time <- parseTime
|
||||
t <- parseTask op
|
||||
pure $ RowContent t time
|
||||
parseOp :: P.Parser (Task -> Operation)
|
||||
parseOp = (In <$ P.char 'i' <* P.skipSpace) <|> (const Out <$ P.char 'o' <* P.skipSpace)
|
||||
parseTime :: P.Parser LocalTime
|
||||
parseTime = do
|
||||
-- I'm assuming that '2022-08-15 17:02:00' has 19 characters
|
||||
-- The input is ByteString, but since 'parseTime' eventually wants
|
||||
-- String, I'm decoding it first to Text while retaining the decoding
|
||||
-- error.
|
||||
timeStr <- either (fail . show) (pure . T.unpack) . TE.decodeUtf8' =<< P.take 19
|
||||
P.skipSpace
|
||||
parseTimeM False defaultTimeLocale "%Y-%m-%d %H:%M:%S" timeStr
|
||||
parseTask :: (Task -> Operation) -> P.Parser Operation
|
||||
parseTask op = do
|
||||
either (fail . show) (pure . op . Task) . TE.decodeUtf8' =<< P.takeByteString
|
||||
|
||||
-- | Decode a file containing the hledger clock-ins and clock-uts
|
||||
decodeFile :: Monad m => ConduitT ByteString Row m ()
|
||||
decodeFile = Conduit.linesUnboundedAscii .| Conduit.concatMap decode
|
||||
|
||||
encodeFile :: Monad m => ConduitT Row ByteString m ()
|
||||
encodeFile = Conduit.map encode .| Conduit.unlinesAscii
|
||||
@@ -0,0 +1,44 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE RankNTypes #-}
|
||||
module MyLib (someFunc) where
|
||||
|
||||
import Command (Command(..), execCommand)
|
||||
import Conduit
|
||||
(ConduitT, ResourceT, liftIO, runConduit, runResourceT, yield, (.|))
|
||||
import qualified Data.Conduit.Combinators as Conduit
|
||||
import Data.Time (getCurrentTime, getCurrentTimeZone, utcToLocalTime)
|
||||
import Hledger.Row
|
||||
import System.Directory (copyFile)
|
||||
|
||||
update :: FilePath -> (forall i m. Monad m => ConduitT i Row m () -> ConduitT i Row m ()) -> IO ()
|
||||
update path action = runResourceT $ do
|
||||
tempFile <- runConduit $
|
||||
Conduit.sourceFile path .|
|
||||
action decodeFile .|
|
||||
encodeFile .|
|
||||
Conduit.sinkSystemTempFile "ledger.timeclock"
|
||||
liftIO $ copyFile tempFile path
|
||||
|
||||
query :: FilePath -> ConduitT i Row (ResourceT IO) ()
|
||||
query path = Conduit.sourceFile path .| decodeFile
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = do
|
||||
now <- utcToLocalTime <$> getCurrentTimeZone <*> getCurrentTime
|
||||
execCommand >>= \case
|
||||
Stop path -> update path (<> yield (stop now))
|
||||
Start path requestedTask -> do
|
||||
t <- maybe (error "Task name can't be empty") pure $ task requestedTask
|
||||
-- Query the previous state
|
||||
-- If we're clocked in, stop the previous clock before starting the new
|
||||
-- one
|
||||
prev <- runResourceT $ runConduit $ query path .| Conduit.last
|
||||
case prev of
|
||||
Just (Row (RowContent{rowOperation=In _})) ->
|
||||
update path (\c -> c <> yield (stop now) <> yield (start now t))
|
||||
_ -> update path (<> yield (start now t))
|
||||
where
|
||||
stop time = Row (RowContent Out time)
|
||||
start time t = Row (RowContent (In t) time)
|
||||
|
||||
Reference in new issue
Block a user