ghcup-hs/app/ghcup/AnsiMain.hs

248 lines
7.2 KiB
Haskell
Raw Normal View History

2022-06-11 11:54:43 +00:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RankNTypes #-}
2022-06-09 09:16:34 +00:00
module AnsiMain where
import GHCup
import GHCup.Download
import GHCup.Errors
2022-06-18 21:19:16 +00:00
import GHCup.Prelude ( decUTF8Safe )
2022-06-09 09:16:34 +00:00
import GHCup.Prelude.File
import GHCup.Prelude.Logger
import GHCup.Prelude.Process
2022-06-18 21:19:16 +00:00
import GHCup.Types hiding ( LeanAppState(..) )
import GHCup.Types.Optics ( getDirs )
import GHCup.Utils
2022-06-09 09:16:34 +00:00
2022-06-11 11:54:43 +00:00
import Control.Monad.IO.Class
2022-06-18 21:19:16 +00:00
import Control.Monad.Trans.Except
2022-06-11 11:54:43 +00:00
-- import System.Console.ANSI
2022-06-18 21:19:16 +00:00
import System.Console.ANSI
2022-06-11 11:54:43 +00:00
import System.Console.ANSI.Codes
import System.Console.ANSI.Types
2022-06-18 21:19:16 +00:00
import Terminal.Game
import Text.PrettyPrint.HughesPJClass ( prettyShow )
2022-06-11 11:54:43 +00:00
2022-06-18 21:19:16 +00:00
import Control.Monad.Reader ( ReaderT(runReaderT) )
import Data.IORef
import Data.Maybe ( fromMaybe )
import qualified Data.Text as Tx
2022-06-11 11:54:43 +00:00
import qualified Data.Tuple as T
import qualified Data.Vector as V
2022-06-18 21:19:16 +00:00
import GHC.IO ( unsafePerformIO )
import Haskus.Utils.Variant.Excepts
import System.Exit
import Data.Versions (prettyVer)
2022-06-11 11:54:43 +00:00
data BrickData = BrickData
2022-06-18 21:19:16 +00:00
{ lr :: [ListResult]
2022-06-11 11:54:43 +00:00
}
deriving Show
data BrickSettings = BrickSettings
{ showAllVersions :: Bool
, showAllTools :: Bool
}
deriving Show
data BrickInternalState = BrickInternalState
{ clr :: V.Vector ListResult
, ix :: Int
}
deriving Show
data BrickState = BrickState
{ appData :: BrickData
, appSettings :: BrickSettings
, appState :: BrickInternalState
, appKeys :: KeyBindings
2022-06-18 21:19:16 +00:00
, appQuit :: Bool
2022-06-11 11:54:43 +00:00
}
deriving Show
ansiMain :: AppState -> IO ()
2022-06-09 09:16:34 +00:00
ansiMain s = do
2022-06-11 11:54:43 +00:00
writeIORef settings' s
eAppData <- getAppData (Just $ ghcupInfo s)
case eAppData of
Right ad -> do
let g = BrickState ad
2022-06-18 21:19:16 +00:00
defaultAppSettings
(constructList ad defaultAppSettings Nothing)
(keyBindings (s :: AppState))
False
2022-06-11 11:54:43 +00:00
sizeCheck
errorPress $ playGame (ghcupGame g)
Left e -> do
2022-06-18 21:19:16 +00:00
flip runReaderT s $ logError $ "Error building app state: " <> Tx.pack
(show e)
2022-06-11 11:54:43 +00:00
exitWith $ ExitFailure 2
where
sizeCheck :: IO ()
2022-06-18 21:19:16 +00:00
sizeCheck = let (w, h) = T.swap . snd $ boundaries in assertTermDims w h
2022-06-11 11:54:43 +00:00
ghcupGame :: BrickState -> Game BrickState
2022-06-18 21:19:16 +00:00
ghcupGame bs = Game 13
bs -- ticks per second
(\_ s e -> logicFun s e) -- logic function
(\r s -> centerFull r $ drawFun s r) -- draw function
appQuit -- quit function
drawFun :: BrickState -> GEnv -> Plane
drawFun (BrickState {..}) GEnv{..} =
blankPlane mw mh
& (1, 1) % box mw mh '.'
& (2, 2) % box (mw-2) (mh-2) ' '
& (2, 2) % renderItems (renderItem <$> lr appData)
2022-06-11 11:54:43 +00:00
where
mh :: Height
mw :: Width
(mh, mw) = snd boundaries
2022-06-18 21:19:16 +00:00
renderItems = foldr1 (===)
renderItem ListResult{..} =
let marks = if
| lSet -> (stringPlane "✔✔")
| lInstalled -> (stringPlane "")
| otherwise -> (stringPlane "")
ver = case lCross of
Nothing -> stringPlane . Tx.unpack . prettyVer $ lVer
Just c -> stringPlane . Tx.unpack $ (c <> "-" <> prettyVer lVer)
tool = printTool lTool
in marks ||| space ||| space ||| tool ||| space ||| ver
printTool Cabal = stringPlane "cabal"
printTool GHC = stringPlane "GHC"
printTool GHCup = stringPlane "GHCup"
printTool HLS = stringPlane "HLS"
printTool Stack = stringPlane "Stack"
space = stringPlane " "
2022-06-11 11:54:43 +00:00
logicFun :: BrickState -> Event -> BrickState
2022-06-18 21:19:16 +00:00
logicFun gs (KeyPress 'q') = gs { appQuit = True }
2022-06-11 11:54:43 +00:00
logicFun gs Tick = gs
logicFun gs (KeyPress c) = gs
defaultAppSettings :: BrickSettings
2022-06-18 21:19:16 +00:00
defaultAppSettings =
BrickSettings { showAllVersions = False, showAllTools = False }
2022-06-11 11:54:43 +00:00
2022-06-18 21:19:16 +00:00
constructList
:: BrickData
-> BrickSettings
-> Maybe BrickInternalState
-> BrickInternalState
constructList appD appSettings = replaceLR
(filterVisible (showAllVersions appSettings) (showAllTools appSettings))
(lr appD)
2022-06-11 11:54:43 +00:00
-- | Replace the @appState@ or construct it based on a filter function
-- and a new @[ListResult]@ evidence.
-- When passed an existing @appState@, tries to keep the selected element.
2022-06-18 21:19:16 +00:00
replaceLR
:: (ListResult -> Bool)
-> [ListResult]
-> Maybe BrickInternalState
-> BrickInternalState
2022-06-11 11:54:43 +00:00
replaceLR filterF lr s =
let oldElem = s >>= listSelectedElement'
newVec = V.fromList . filter filterF $ lr
newSelected =
case oldElem >>= \(_, oldE) -> V.findIndex (toolEqual oldE) newVec of
Just ix -> ix
Nothing -> selectLatest newVec
in BrickInternalState newVec newSelected
where
toolEqual e1 e2 =
lTool e1 == lTool e2 && lVer e1 == lVer e2 && lCross e1 == lCross e2
filterVisible :: Bool -> Bool -> ListResult -> Bool
2022-06-18 21:19:16 +00:00
filterVisible v t e
| lInstalled e = True
| v, not t, lTool e `notElem` hiddenTools = True
| not v, t, Old `notElem` lTag e = True
| v, t = True
| otherwise = (Old `notElem` lTag e) && (lTool e `notElem` hiddenTools)
2022-06-11 11:54:43 +00:00
hiddenTools :: [Tool]
hiddenTools = []
selectLatest :: V.Vector ListResult -> Int
2022-06-18 21:19:16 +00:00
selectLatest = fromMaybe 0
. V.findIndex (\ListResult {..} -> lTool == GHC && Latest `elem` lTag)
2022-06-11 11:54:43 +00:00
listSelectedElement' :: BrickInternalState -> Maybe (Int, ListResult)
2022-06-18 21:19:16 +00:00
listSelectedElement' BrickInternalState {..} = fmap (ix, ) $ clr V.!? ix
2022-06-11 11:54:43 +00:00
boundaries :: (Coords, Coords)
boundaries = ((1, 1), (24, 80))
settings' :: IORef AppState
{-# NOINLINE settings' #-}
settings' = unsafePerformIO $ do
dirs <- getAllDirs
let loggerConfig = LoggerConfig { lcPrintDebug = False
, consoleOutter = \_ -> pure ()
, fileOutter = \_ -> pure ()
, fancyColors = True
}
newIORef $ AppState defaultSettings
dirs
defaultKeyBindings
(GHCupInfo mempty mempty mempty)
(PlatformRequest A_64 Darwin Nothing)
loggerConfig
2022-06-18 21:19:16 +00:00
getAppData :: Maybe GHCupInfo -> IO (Either String BrickData)
2022-06-11 11:54:43 +00:00
getAppData mgi = runExceptT $ do
r <- ExceptT $ maybe getGHCupInfo (pure . Right) mgi
liftIO $ modifyIORef settings' (\s -> s { ghcupInfo = r })
settings <- liftIO $ readIORef settings'
flip runReaderT settings $ do
lV <- listVersions Nothing Nothing
pure $ BrickData (reverse lV)
getGHCupInfo :: IO (Either String GHCupInfo)
getGHCupInfo = do
settings <- readIORef settings'
2022-06-18 21:19:16 +00:00
r <-
2022-06-11 11:54:43 +00:00
flip runReaderT settings
2022-06-18 21:19:16 +00:00
. runE
@'[ DigestError
, GPGError
, JSONError
, DownloadFailed
, FileDoesNotExistError
]
2022-06-11 11:54:43 +00:00
$ liftE getDownloadsF
case r of
VRight a -> pure $ Right a
VLeft e -> pure $ Left (prettyShow e)
2022-06-18 21:19:16 +00:00