{-# LANGUAGE DataKinds #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE TypeApplications #-}{-# LANGUAGE RankNTypes #-}{-# LANGUAGE ViewPatterns #-}{-# OPTIONS_GHC -Wno-unused-record-wildcards #-}{-# OPTIONS_GHC -Wno-unused-matches #-}{-# OPTIONS_GHC -Wno-missing-signatures #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE TemplateHaskell #-}{-# LANGUAGE BangPatterns #-}{-# LANGUAGE InstanceSigs #-}{-# LANGUAGE PatternSynonyms #-}{-This module contains common values used across the library. Crucially it contains two important types for the brick app:- Name: List all resources (widgets) used by the app. see https://github.com/jtdaugherty/brick/blob/master/docs/guide.rst#resource-names- Mode: Use to dispatch events and drawings. see: https://github.com/jtdaugherty/brick/issues/476#issuecomment-1629151920-}module GHCup.Brick.Common ( installedSign, setSign, notInstalledSign, checkBoxSelectedSign, showKey, showMod, keyToWidget, separator, frontwardLayer, enableScreenReader, zoom, lr, showAllVersions, _showAllVersions, bsToolL, bsCriteriaL, bsFromL, bsToL, bsHideOldL, bsShowNightlyL, bsShowRevisionsL, Name(..), Mode(..), BrickData(..), BrickSettings(..), ResourceId ( UrlEditBox, SetCheckBox, IsolateEditBox, ForceCheckBox, AdditionalEditBox , TargetGhcEditBox, BootstrapGhcEditBox, HadrianGhcEditBox, JobsEditBox, BuildConfigEditBox , PatchesEditBox, CrossTargetEditBox, AddConfArgsEditBox, OvewrwiteVerEditBox , BuildFlavourEditBox, BuildSystemEditBox, OkButton, AdvancedInstallButton , CompileGHCButton, CompileHLSButton, CabalProjectEditBox , CabalProjectLocalEditBox, UpdateCabalCheckBox, GitRefEditBox , BootstrapGhcSelectBox, HadrianGhcSelectBox, ToolVersionBox, GHCInstallTargets , DocsEditBox ) ) whereimport GHCup.Command.List ( ToolListResult, ListCriteria )import GHCup.Prelude ( isWindows )import GHCup.Types ( Tool, KeyCombination (KeyCombination), ShowRevisions, ShowNightly(..) )import Data.List (intercalate)import Prelude hiding ( appendFile )import qualified Graphics.Vty as Vtyimport Optics (Lens', lens)import Optics.TH (makeLenses, makeLensesFor)import Optics.Lens (toLensVL)import qualified Brickimport qualified Brick.Widgets.Border as Borderimport Brick ((<+>))import qualified Data.Text as Timport qualified Brick.Widgets.Center as Brickimport qualified Brick.Widgets.Border.Style as Borderimport Data.Time.Calendar (Day)-- We could use regular ADTs but different menus share the same options.-- example: all of ghcup compile ghc, ghcup compile hls, ghcup install cabal, etc...-- all have a --set, --force, etc... common arguments. If we went for the ADT we'd end up-- with SetCompileHLSOption, SetCompileGHCOption, SetInstallCabalOption, etc...-- which isn't terrible, but verbose enough to reject it.-- | A newtype for labeling resources in menus. It is bundled along with pattern synonymsnewtype ResourceId = ResourceId Int deriving (Eq, Ord, Show)pattern OkButton :: ResourceIdpattern OkButton = ResourceId 0pattern AdvancedInstallButton :: ResourceIdpattern AdvancedInstallButton = ResourceId 100pattern CompileGHCButton :: ResourceIdpattern CompileGHCButton = ResourceId 101pattern CompileHLSButton :: ResourceIdpattern CompileHLSButton = ResourceId 102pattern UrlEditBox :: ResourceIdpattern UrlEditBox = ResourceId 1pattern SetCheckBox :: ResourceIdpattern SetCheckBox = ResourceId 2pattern IsolateEditBox :: ResourceIdpattern IsolateEditBox = ResourceId 3pattern ForceCheckBox :: ResourceIdpattern ForceCheckBox = ResourceId 4pattern AdditionalEditBox :: ResourceIdpattern AdditionalEditBox = ResourceId 5pattern TargetGhcEditBox :: ResourceIdpattern TargetGhcEditBox = ResourceId 6pattern BootstrapGhcEditBox :: ResourceIdpattern BootstrapGhcEditBox = ResourceId 7pattern HadrianGhcEditBox :: ResourceIdpattern HadrianGhcEditBox = ResourceId 20pattern JobsEditBox :: ResourceIdpattern JobsEditBox = ResourceId 8pattern BuildConfigEditBox :: ResourceIdpattern BuildConfigEditBox = ResourceId 9pattern PatchesEditBox :: ResourceIdpattern PatchesEditBox = ResourceId 10pattern CrossTargetEditBox :: ResourceIdpattern CrossTargetEditBox = ResourceId 11pattern AddConfArgsEditBox :: ResourceIdpattern AddConfArgsEditBox = ResourceId 12pattern OvewrwiteVerEditBox :: ResourceIdpattern OvewrwiteVerEditBox = ResourceId 13pattern BuildFlavourEditBox :: ResourceIdpattern BuildFlavourEditBox = ResourceId 14pattern BuildSystemEditBox :: ResourceIdpattern BuildSystemEditBox = ResourceId 15pattern CabalProjectEditBox :: ResourceIdpattern CabalProjectEditBox = ResourceId 16pattern CabalProjectLocalEditBox :: ResourceIdpattern CabalProjectLocalEditBox = ResourceId 17pattern UpdateCabalCheckBox :: ResourceIdpattern UpdateCabalCheckBox = ResourceId 18pattern GitRefEditBox :: ResourceIdpattern GitRefEditBox = ResourceId 19pattern BootstrapGhcSelectBox :: ResourceIdpattern BootstrapGhcSelectBox = ResourceId 21pattern HadrianGhcSelectBox :: ResourceIdpattern HadrianGhcSelectBox = ResourceId 22pattern ToolVersionBox :: ResourceIdpattern ToolVersionBox = ResourceId 23pattern GHCInstallTargets :: ResourceIdpattern GHCInstallTargets = ResourceId 24pattern DocsEditBox :: ResourceIdpattern DocsEditBox = ResourceId 25-- | Name data type. Uniquely identifies each widget in the TUI.-- some constructors might end up unused, but still is a good practise-- to have all of them defined, just in casedata Name = AllTools -- ^ The main list widget | Singular Tool -- ^ The particular list for each tool | ListItem Tool Int -- ^ An item in list | KeyInfoBox -- ^ The text box widget with action information | TutorialBox -- ^ The tutorial widget | ContextBox -- ^ The resource for Context Menu | CompileGHCBox -- ^ The resource for CompileGHC Menu | AdvancedInstallBox -- ^ The resource for AdvancedInstall Menu | MenuElement ResourceId -- ^ Each element in a Menu. Resources must not be share for visible -- Menus, but MenuA and MenuB can share resources if they both are -- invisible, or just one of them is visible. deriving (Eq, Ord, Show)-- | Mode type. It helps to dispatch events to different handlers.data Mode = Navigation | KeyInfo | ToolInfo | Tutorial | ContextPanel | AdvancedInstallPanel | CompileGHCPanel | CompileHLSPanel deriving (Eq, Show, Ord)installedSign :: String | isWindows = "I " | otherwise = "✓ "setSign :: String | isWindows = "IS" | otherwise = "✔✔"notInstalledSign :: String | isWindows = "X " | otherwise = "✗ "checkBoxSelectedSign :: String | isWindows = "Y " | otherwise = "✓ "showKey :: Vty.Key -> StringshowKey (Vty.KChar '\t') = "TAB"showKey (Vty.KChar c) = [c]showKey Vty.KUp = "↑"showKey Vty.KDown = "↓"showKey Vty.KLeft = "←"showKey Vty.KRight = "→"showKey key = tail (show key)showMod :: Vty.Modifier -> StringshowMod = tail . show-- | Given a KeyComb, produces a string widget with and user friendly textkeyToWidget :: KeyCombination -> Brick.Widget nkeyToWidget (KeyCombination key mods) = Brick.str $ intercalate "+" (showKey key : (showMod <$> mods))-- | A section separator with max width. Looks like this: -------- o --------separator :: Brick.Widget nseparator = Border.hBorder <+> Brick.str " o " <+> Border.hBorder-- | Used to create a layer on top of the main navigation widget (tutorial, info, menus...)frontwardLayer :: T.Text -> Brick.Widget n -> Brick.Widget nfrontwardLayer layer_name = Brick.centerLayer . Brick.hLimitPercent 80 . Brick.vLimitPercent 75 . Brick.withBorderStyle Border.unicodeBold . Border.borderWithLabel (Brick.txt layer_name)-- | Puts a cursor at the line beginning so it can be read by screen readersenableScreenReader :: n -> Brick.Widget n -> Brick.Widget nenableScreenReader n = Brick.putCursor n (Brick.Location (0,0))-- |- tip: when debugging, use Brick.showCursor instead-- I refuse to give this a type signature.-- | Given a lens, zoom on it. It is needed because Brick uses microlens but GHCup uses optics.zoom l = Brick.zoom (toLensVL l)data BrickData = BrickData { _lr :: ToolListResult } deriving ShowmakeLenses ''BrickData-- Currently the only TUI 'toggable' setting is ioHideOldL... all the-- other fiels are only used in 'getAppData'. If you want to make-- them toggable as well, they have to be utilized in 'constructList'.data BrickSettings = BrickSettings { bsTool :: Maybe [Tool] , bsCriteria :: [ListCriteria] , bsFrom :: Maybe Day , bsTo :: Maybe Day , bsHideOld :: Bool , bsShowNightly :: ShowNightly , bsShowRevisions :: ShowRevisions } --deriving ShowmakeLensesFor [ ("bsTool", "bsToolL") , ("bsCriteria", "bsCriteriaL") , ("bsFrom", "bsFromL") , ("bsTo", "bsToL") , ("bsHideOld", "bsHideOldL") , ("bsShowNightly", "bsShowNightlyL") , ("bsShowRevisions", "bsShowRevisionsL") ] ''BrickSettingsshowAllVersions :: Lens' BrickSettings BoolshowAllVersions = lens (not . bsHideOld) (\BrickSettings{..} showAll -> let bsHideOld = not showAll in BrickSettings{..})_showAllVersions :: BrickSettings -> Bool_showAllVersions = not . bsHideOld