Skip to content

Commit 6be847e

Browse files
committed
Show confirmation prompt for pre-install messages in TUI
1 parent b47aebe commit 6be847e

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

lib-tui/GHCup/Brick/Actions.hs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ suspendBrickAndRunAction s action = do
142142
Right _ -> liftIO $ putStrLn "Success"
143143

144144

145+
getPreInstallMessage :: AppState -> GHCTargetVersion -> Tool -> Maybe T.Text
146+
getPreInstallMessage s v lTool =
147+
let
148+
dls = _ghcupDownloads $ ghcupInfo s
149+
in case lTool of
150+
GHC -> do
151+
let vi = getVersionInfo v GHC dls
152+
(_viPreInstall =<< vi)
153+
Cabal -> do
154+
let vi = getVersionInfo v Cabal dls
155+
(_viPreInstall =<< vi)
156+
GHCup -> do
157+
let vi = snd <$> getLatest dls GHCup
158+
(_viPreInstall =<< vi)
159+
HLS -> do
160+
let vi = getVersionInfo v HLS dls
161+
(_viPreInstall =<< vi)
162+
Stack -> do
163+
let vi = getVersionInfo v Stack dls
164+
(_viPreInstall =<< vi)
165+
145166
installWithOptions :: (MonadReader AppState m, MonadIO m, MonadThrow m, MonadFail m, MonadMask m, MonadUnliftIO m, Alternative m)
146167
=> AdvanceInstall.InstallOptions
147168
-> ListResult
@@ -200,11 +221,6 @@ installWithOptions opts ListResult {..} = do
200221
case lTool of
201222
GHC -> do
202223
let vi = getVersionInfo v GHC dls
203-
forM_ (_viPreInstall =<< vi) $ \msg -> do
204-
lift $ logWarn msg
205-
lift $ logWarn
206-
"...waiting for 5 seconds, you can still abort..."
207-
liftIO $ threadDelay 5000000 -- give the user a sec to intervene
208224
case opts ^. AdvanceInstall.instBindist of
209225
Nothing -> do
210226
liftE $
@@ -228,11 +244,6 @@ installWithOptions opts ListResult {..} = do
228244

229245
Cabal -> do
230246
let vi = getVersionInfo v Cabal dls
231-
forM_ (_viPreInstall =<< vi) $ \msg -> do
232-
lift $ logWarn msg
233-
lift $ logWarn
234-
"...waiting for 5 seconds, you can still abort..."
235-
liftIO $ threadDelay 5000000 -- give the user a sec to intervene
236247
case opts ^. AdvanceInstall.instBindist of
237248
Nothing -> do
238249
liftE $
@@ -249,19 +260,9 @@ installWithOptions opts ListResult {..} = do
249260

250261
GHCup -> do
251262
let vi = snd <$> getLatest dls GHCup
252-
forM_ (_viPreInstall =<< vi) $ \msg -> do
253-
lift $ logWarn msg
254-
lift $ logWarn
255-
"...waiting for 5 seconds, you can still abort..."
256-
liftIO $ threadDelay 5000000 -- give the user a sec to intervene
257263
liftE $ upgradeGHCup Nothing False False $> (vi, dirs, ce)
258264
HLS -> do
259265
let vi = getVersionInfo v HLS dls
260-
forM_ (_viPreInstall =<< vi) $ \msg -> do
261-
lift $ logWarn msg
262-
lift $ logWarn
263-
"...waiting for 5 seconds, you can still abort..."
264-
liftIO $ threadDelay 5000000 -- give the user a sec to intervene
265266
case opts ^. AdvanceInstall.instBindist of
266267
Nothing -> do
267268
liftE $

lib-tui/GHCup/Brick/App/AdvanceInstallMenu.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
module GHCup.Brick.App.AdvanceInstallMenu where
88

99
import GHCup.List ( ListResult (..))
10-
import GHCup.Types (GHCTargetVersion(..), KeyBindings, AppState)
10+
import GHCup.Types (GHCTargetVersion(..), KeyBindings, AppState, bQuit)
1111
import GHCup.Brick.Widgets.BaseWidget
1212
import GHCup.Brick.Widgets.BasicOverlay
1313
import GHCup.Brick.Widgets.InputField.Class
1414
import GHCup.Brick.Widgets.InputField.CheckBox
1515
import GHCup.Brick.Widgets.InputField.EditInput as EditInput
16-
import GHCup.Brick.Widgets.GenericMenu
16+
import GHCup.Brick.Widgets.GenericMenu as GenericMenu
17+
import qualified GHCup.Brick.Widgets.ConfirmationPrompt as ConfirmationPrompt
1718
import qualified GHCup.Brick.Actions as Actions
1819
import qualified GHCup.Brick.App.Common as Common
1920
import GHCup.Brick.App.AdvanceInstallOptions
@@ -32,11 +33,13 @@ import Control.Monad (when, forM, forM_, void)
3233
import Data.Bifunctor (Bifunctor(..))
3334
import Data.Char (isSpace)
3435
import Data.List.NonEmpty ( NonEmpty (..) )
36+
import Data.Maybe
3537
import Data.Some
3638
import qualified Data.Text as T
3739
import GHC.Generics (Generic)
3840
import Prelude hiding ( appendFile )
3941
import qualified Graphics.Vty as Vty
42+
import Optics (lens, (^.))
4043
import Optics.State.Operators ((.=), (?=))
4144
import Optics.TH (makeLenses)
4245
import URI.ByteString (URI)
@@ -61,7 +64,16 @@ create kb lr s = mkGenericMenu
6164
menuFields
6265
validateInputs
6366
(s, lr)
64-
(\(s, lr) opts -> (Just CloseAllOverlays) <$ (Actions.suspendBrickAndRunAction s $ Actions.installWithOptions opts lr))
67+
(\(s, lr) opts -> do
68+
let action = (Just CloseAllOverlays) <$ (Actions.suspendBrickAndRunAction s $ Actions.installWithOptions opts lr)
69+
v = fromMaybe (GHCTargetVersion (lCross lr) (lVer lr)) (opts ^. instVersion)
70+
case Actions.getPreInstallMessage s v (lTool lr) of
71+
Nothing -> action
72+
Just msg -> do
73+
let prompt = ConfirmationPrompt.create "Warning" msg action (bQuit kb)
74+
GenericMenu.overlay ?= Some (IsSubWidget $ lens (const prompt) (\s _ -> s))
75+
pure Nothing
76+
)
6577
(Common.toMenuKeyBindings kb)
6678
"Advance Install"
6779
(Button (Common.MenuElement Common.OkButton)

lib-tui/GHCup/Brick/App/Navigation.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified GHCup.Brick.App.ContextMenu as ContextMenu
1414
import GHCup.Brick.Widgets.BaseWidget
1515
import GHCup.Brick.Widgets.BasicOverlay
1616
import qualified GHCup.Brick.Attributes as Attributes
17+
import qualified GHCup.Brick.Widgets.ConfirmationPrompt as ConfirmationPrompt
1718
import qualified GHCup.Brick.Widgets.SectionList as SectionList
1819

1920
import GHCup.List ( ListResult(..) )
@@ -47,7 +48,7 @@ import Control.Monad.Reader
4748
import Data.Some
4849
import Data.Vector ( Vector )
4950
import qualified Graphics.Vty as Vty
50-
import Optics (Lens', use, to, (^.), (%), (&), (%~), (.~))
51+
import Optics (Lens', lens, use, to, (^.), (%), (&), (%~), (.~))
5152
import Optics.TH (makeLenses)
5253
import Optics.State.Operators ((.=), (?=), (%=))
5354

@@ -255,7 +256,7 @@ keyHandlers :: KeyBindings
255256
]
256257
keyHandlers KeyBindings {..} =
257258
[ (bQuit, const "Quit" , Brick.halt)
258-
, (bInstall, const "Install" , withIOAction' install')
259+
, (bInstall, const "Install" , installAfterPreInstallPrompt)
259260
, (bUninstall, const "Uninstall", withIOAction' del')
260261
, (bSet, const "Set" , withIOAction' set')
261262
, (bChangelog, const "ChangeLog", withIOAction' changelog')
@@ -278,6 +279,22 @@ keyHandlers KeyBindings {..} =
278279
suspendBrickAndRunAction _appState $ action (curr_ix, e)
279280
updateNavigation
280281

282+
installAfterPreInstallPrompt = do
283+
Navigation {..} <- Brick.get
284+
case SectionList.sectionListSelectedElement _sectionList of
285+
Nothing -> pure ()
286+
Just (curr_ix, lr) -> do
287+
let action = (Just CloseAllOverlays) <$ suspendBrickAndRunAction _appState (install' (curr_ix, lr))
288+
v = GHCTargetVersion (lCross lr) (lVer lr)
289+
case getPreInstallMessage _appState v (lTool lr) of
290+
Nothing -> do
291+
action
292+
updateNavigation
293+
Just msg -> do
294+
let prompt = ConfirmationPrompt.create "Warning" msg action bQuit
295+
-- updateNavigation will happen after the prompt is closed
296+
overlay ?= Some (IsSubWidget $ lens (const prompt) (\s _ -> s))
297+
281298
openContextMenuforTool = do
282299
e <- use (sectionList % to SectionList.sectionListSelectedElement)
283300
case e of

0 commit comments

Comments
 (0)