From 8808276b743b3267e36243512d592b922f7665aa Mon Sep 17 00:00:00 2001 From: ners Date: Wed, 21 Feb 2024 18:26:08 +0100 Subject: [PATCH] trilby-cli: update terminal-widgets --- flake.lock | 18 +++++++-------- trilby-cli/flake.lock | 12 +++++----- trilby-cli/src/Internal/Prelude.hs | 9 +++++--- trilby-cli/src/Trilby/Install.hs | 16 ++++++------- trilby-cli/src/Trilby/Install/Options.hs | 29 +++++++++++++++++++----- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/flake.lock b/flake.lock index 228dfeb..a76b8ea 100644 --- a/flake.lock +++ b/flake.lock @@ -42,11 +42,11 @@ ] }, "locked": { - "lastModified": 1708294481, - "narHash": "sha256-DZtxmeb4OR7iCaKUUuq05ADV2rX8WReZEF7Tq//W0+Y=", + "lastModified": 1708451036, + "narHash": "sha256-tgZ38NummEdnXvxj4D0StHBzXgceAw8CptytHljH790=", "owner": "nix-community", "repo": "home-manager", - "rev": "a54e05bc12d88ff2df941d0dc1183cb5235fa438", + "rev": "517601b37c6d495274454f63c5a483c8e3ca6be1", "type": "github" }, "original": { @@ -95,11 +95,11 @@ }, "nixpkgs-23.11": { "locked": { - "lastModified": 1708294118, - "narHash": "sha256-evZzmLW7qoHXf76VCepvun1esZDxHfVRFUJtumD7L2M=", + "lastModified": 1708440434, + "narHash": "sha256-XY+B9mbhL/i+Q6fP6gBQ6P76rv9rWtpjQiUJ+DGtaUg=", "owner": "nixos", "repo": "nixpkgs", - "rev": "e0da498ad77ac8909a980f07eff060862417ccf7", + "rev": "526d051b128b82ae045a70e5ff1adf8e6dafa560", "type": "github" }, "original": { @@ -111,11 +111,11 @@ }, "nixpkgs-unstable": { "locked": { - "lastModified": 1708296515, - "narHash": "sha256-FyF489fYNAUy7b6dkYV6rGPyzp+4tThhr80KNAaF/yY=", + "lastModified": 1708475490, + "narHash": "sha256-g1v0TsWBQPX97ziznfJdWhgMyMGtoBFs102xSYO4syU=", "owner": "nixos", "repo": "nixpkgs", - "rev": "b98a4e1746acceb92c509bc496ef3d0e5ad8d4aa", + "rev": "0e74ca98a74bc7270d28838369593635a5db3260", "type": "github" }, "original": { diff --git a/trilby-cli/flake.lock b/trilby-cli/flake.lock index 6edcdb5..d707342 100644 --- a/trilby-cli/flake.lock +++ b/trilby-cli/flake.lock @@ -50,11 +50,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1708296515, - "narHash": "sha256-FyF489fYNAUy7b6dkYV6rGPyzp+4tThhr80KNAaF/yY=", + "lastModified": 1708475490, + "narHash": "sha256-g1v0TsWBQPX97ziznfJdWhgMyMGtoBFs102xSYO4syU=", "owner": "nixos", "repo": "nixpkgs", - "rev": "b98a4e1746acceb92c509bc496ef3d0e5ad8d4aa", + "rev": "0e74ca98a74bc7270d28838369593635a5db3260", "type": "github" }, "original": { @@ -84,11 +84,11 @@ "text-rope-zipper": "text-rope-zipper" }, "locked": { - "lastModified": 1708365514, - "narHash": "sha256-tZ4Ae74n+p3eYY2uSHW+y6b734Jg918FpmwG+rZEMy4=", + "lastModified": 1708535983, + "narHash": "sha256-8CMmFbVZKpqWlri6jEMNy+3yVcApvv2FBVgPHCPl/dw=", "owner": "ners", "repo": "terminal-widgets", - "rev": "d7c5044feec4c753e9d9ed0675570ee67040ca3e", + "rev": "5a60a216137ccb5237d50d60c656e618acdd3218", "type": "github" }, "original": { diff --git a/trilby-cli/src/Internal/Prelude.hs b/trilby-cli/src/Internal/Prelude.hs index b262014..011e1a3 100644 --- a/trilby-cli/src/Internal/Prelude.hs +++ b/trilby-cli/src/Internal/Prelude.hs @@ -31,6 +31,7 @@ module Internal.Prelude , parseChoiceWith , parseChoice , parseEnum + , runWidget , askText , askPassword , askYesNo @@ -200,12 +201,14 @@ askChoice prompt values defaultValue = do runWidget Select { prompt - , options = [(ishow v, False) | v <- values] - , multiselect = False + , options = [SelectOption{value, checked = False} | value <- values] + , optionText = ishow + , minSelect = 1 + , maxSelect = 1 , cursorRow = defaultValue + 1 } fromMaybeM (askChoice prompt values defaultValue) $ pure do - i <- List.findIndex snd select.options + i <- List.findIndex (.checked) select.options values !? i askEnum :: (Eq a, Bounded a, Enum a, Show a) => Text -> a -> App a diff --git a/trilby-cli/src/Trilby/Install.hs b/trilby-cli/src/Trilby/Install.hs index e89554f..6944f50 100644 --- a/trilby-cli/src/Trilby/Install.hs +++ b/trilby-cli/src/Trilby/Install.hs @@ -69,8 +69,8 @@ install (askOpts -> opts) | Just FlakeOpts{..} <- opts.flake = do let rootIsMounted = (ExitSuccess ==) . fst <$> cmd' ["mountpoint", "-q", fromString rootMount] unlessM rootIsMounted do $(logWarn) "Partitions are not mounted" - unlessM (askYesNo "Attempt to mount the partitions?" True) $ - errorExit "Cannot install without mounted partitions" + unlessM (askYesNo "Attempt to mount the partitions?" True) + $ errorExit "Cannot install without mounted partitions" runDisko $ Mount $ Disko.Flake flakeRef nixosInstall flakeRef whenM copyFlake do @@ -88,8 +88,8 @@ install (askOpts -> opts) = do let rootIsMounted = (ExitSuccess ==) . fst <$> cmd' ["mountpoint", "-q", fromString rootMount] unlessM rootIsMounted do $(logWarn) "Partitions are not mounted" - unlessM (askYesNo "Attempt to mount the partitions?" True) $ - errorExit "Cannot install without mounted partitions" + unlessM (askYesNo "Attempt to mount the partitions?" True) + $ errorExit "Cannot install without mounted partitions" runDisko $ Mount $ Disko.File diskoFile inDir trilbyDir do asRoot cmd_ ["chown", "-R", "1000:1000", fromString trilbyDir] @@ -134,8 +134,8 @@ install (askOpts -> opts) = do , fromString rootMount ] writeNixFile "disko.nix" $ clearLuksFiles disko - cmd_ $ - sconcat + cmd_ + $ sconcat [ ["nix", "flake", "lock"] , ["--accept-flake-config"] , ["--override-input", "trilby", "trilby"] @@ -147,8 +147,8 @@ nixosInstall :: Text -> App () nixosInstall flakeRef = do $(logWarn) "Performing installation ... " -- TODO(vkleen): this shouldn't work and neither should it be necessary ... - (withTrace . asRoot) rawCmd_ $ - sconcat + (withTrace . asRoot) rawCmd_ + $ sconcat [ ["nix", "build"] , ["--store", "/mnt"] , ["--impure"] diff --git a/trilby-cli/src/Trilby/Install/Options.hs b/trilby-cli/src/Trilby/Install/Options.hs index 155bfdb..8bb753f 100644 --- a/trilby-cli/src/Trilby/Install/Options.hs +++ b/trilby-cli/src/Trilby/Install/Options.hs @@ -1,3 +1,4 @@ +{-# OPTIONS_GHC -Wno-name-shadowing #-} {-# OPTIONS_GHC -Wno-partial-fields #-} module Trilby.Install.Options where @@ -7,6 +8,7 @@ import Data.Text qualified as Text import Internal.Prelude hiding (error) import Options.Applicative import System.Posix (getFileStatus, isBlockDevice) +import System.Terminal.Widgets.SearchSelect (SearchSelect (..), SearchSelectOption (..)) import Trilby.Config.Channel import Trilby.Config.Edition import Trilby.Config.Host (Keyboard (..)) @@ -137,13 +139,28 @@ askLocale = do askTimezone :: App Text askTimezone = do currentTz <- firstLine <$> shell "timedatectl show --property=Timezone --value" empty - tz <- askText "Choose time zone:" currentTz allTimezones <- Text.lines <$> shell "timedatectl list-timezones" empty - if tz `elem` allTimezones - then pure tz - else do - $(logError) "Unknown timezone" - askTimezone + ss <- + runWidget + SearchSelect + { prompt = "Choose time zone: " + , searchValue = "" + , options = + [ SearchSelectOption + { value + , visible = False + } + | value <- allTimezones + ] + , selections = [currentTz] + , optionText = id + , minSelect = 1 + , maxSelect = 1 + , maxVisible = 5 + , minSearchLength = 3 + , cursorRow = 0 + } + maybe askTimezone pure $ listToMaybe ss.selections askOpts :: InstallOpts Maybe -> InstallOpts App askOpts opts = do