From 6ce5de5a227267e359aa3c38344e3858f035287d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 8 May 2016 01:05:00 +0200 Subject: command interpretation --- src/Main.hs | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index c3c1d7e..86c8771 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -6,6 +6,9 @@ import qualified Data.PQueue.Prio.Max as MaxPQueue import Data.Sequence (Seq, (|>), (<|), viewl, viewr, ViewL(..), ViewR(..)) import qualified Data.Sequence as Seq +import Data.Text (Text) +import qualified Data.Text as Text + import Data.Default.Class import Data.Maybe (fromMaybe) @@ -23,30 +26,43 @@ import Types import Utils import Command -type SequenceT m a = WriterT History (InputT (StateT Sequence m)) a - main :: IO () main = do historyFile <- flip getUserCacheFile "history" =<< getProgName let settings = setComplete noCompletion {- TODO -} . (\x -> x { historyFile = Just historyFile }) $ defaultSettings - void . flip runStateT def . runInputT settings . runWriterT $ runCli + void . flip runStateT def . runInputT settings $ runCli + +-- data Cmd = PerformAlt Alteration (Maybe Comment) +-- | ShowTip +-- | ShowHistory (Maybe Integer) +-- | Dump -runCli :: (MonadIO m, MonadException m) => SequenceT m () +runCli :: (MonadIO m, MonadException m, SequenceM m) => InputT m () runCli = do - input <- lift $ getInputLine "→ " + input <- getInputLine "→ " case input of Nothing -> liftIO exitSuccess Just input' -> do cmd <- parseCmd input' case cmd of - UnknownCommand help -> do - lift . outputStrLn $ help - ParseError err -> do - lift . outputStrLn $ "Error parsing input »" <> (fromMaybe "" input) <> "«: " <> err - Empty -> return () + UnknownCommand help -> do + outputStrLn $ help + ParseError err -> do + outputStrLn $ "Error parsing input »" <> (fromMaybe "" input) <> "«: " <> err + Empty -> return () - Quit -> liftIO exitSuccess + Quit -> liftIO exitSuccess + Dump -> get >>= outputStrLn . show + ShowTip -> do + tip <- fmap snd <$> gets (MaxPQueue.getMax . ctxSequence) + case tip of + Just entity -> outputStrLn . Text.unpack $ showEntity entity + Nothing -> outputStrLn "Queue is currently empty" + ShowHistory ns -> do + let sel = maybe id takeR ns + mapM_ (outputStrLn . show) =<< sel <$> gets ctxHistory + PerformAlt alt comment -> apply' alt comment _ -> undefined runCli -- cgit v1.2.3