67 lines
2.2 KiB
Haskell
67 lines
2.2 KiB
Haskell
module Main (main) where
|
|
|
|
import Relude hiding (Proxy)
|
|
|
|
import Api qualified as Api
|
|
import Api.Types (last_activity_date)
|
|
import Cli
|
|
import Network.HTTP.Client
|
|
import Polysemy (Member, Members, Sem)
|
|
import Polysemy.Embed (Embed)
|
|
import Polysemy.Error (Error, errorToIOFinal)
|
|
import Polysemy.Output (Output)
|
|
import Polysemy.Output.IO (putTextLnO, runOutputToStdout)
|
|
import Polysemy.Sgr (Sgr, SgrRegion, ignoreSgrFull, runSgrFull, withSgr)
|
|
import Polysemy.Final (Final, embedToFinal, runFinal)
|
|
import Color.Print
|
|
import Servant.Client
|
|
import Servant.Polysemy.Client (ServantClient, runServantClientWith)
|
|
import System.Console.ANSI
|
|
|
|
defProxy :: Proxy
|
|
defProxy = Proxy "127.0.0.1" 4444
|
|
|
|
runProgram :: ClientEnv
|
|
-> Sem [ Output Text
|
|
, ServantClient, Error ClientError
|
|
, Embed IO, Final IO
|
|
] ()
|
|
-> IO ()
|
|
runProgram clientEnv =
|
|
(>>= either (error . fromString . displayException) pure)
|
|
. runFinal
|
|
. embedToFinal
|
|
. errorToIOFinal
|
|
. runServantClientWith clientEnv
|
|
. runOutputToStdout
|
|
|
|
sgrFullInterpreter :: Member (Output Text) r
|
|
=> Bool -> Sem (SgrRegion : Sgr : r) a -> Sem r a
|
|
sgrFullInterpreter noAnsi = if noAnsi then ignoreSgrFull else runSgrFull
|
|
|
|
program :: Members [Error ClientError, ServantClient, Output Text, Sgr, SgrRegion] r
|
|
=> Choice -> Sem r ()
|
|
program GetSections = do
|
|
sections <- Api.getSections
|
|
withSgr [SetColor Foreground Vivid White] do
|
|
sequenceA_ $ intersperse (putTextLnO "") (printColor <$> sections)
|
|
program GetTopics = do
|
|
topics <- Api.getTopics
|
|
withSgr [SetColor Foreground Vivid White] do
|
|
sequenceA_ $ intersperse (putTextLnO "")
|
|
(fmap printColor . reverse $ sortBy (comparing (.last_activity_date)) topics)
|
|
program GetTopic{topic_id} = do
|
|
topic <- Api.getTopic topic_id
|
|
withSgr [SetColor Foreground Vivid White] do
|
|
printColor topic
|
|
|
|
main :: IO ()
|
|
main = do
|
|
Options{noAnsi, choice} <- getOpts
|
|
manager' <- newManager $
|
|
defaultManagerSettings
|
|
& (managerSetProxy $ proxyEnvironmentNamed "I2P_HTTP_PROXY" $ Just defProxy)
|
|
let clientEnv = mkClientEnv manager' (BaseUrl Http "4rum.i2p" 80 "")
|
|
|
|
runProgram clientEnv . sgrFullInterpreter noAnsi $ program choice
|