From 67e303af381a9e8b6c99fec3fc5d23ba0aa12e50 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 27 Feb 2024 09:44:17 -0600 Subject: [PATCH] Refactor TUI input dialog --- src/Zenith/CLI.hs | 80 ++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/src/Zenith/CLI.hs b/src/Zenith/CLI.hs index c7a656a..16ac439 100644 --- a/src/Zenith/CLI.hs +++ b/src/Zenith/CLI.hs @@ -59,14 +59,19 @@ data Name | AList | TList | HelpDialog - | WalNameField + | DialogInputField deriving (Eq, Show, Ord) -data WalletName = WalletName - { _walName :: !T.Text +data DialogInput = DialogInput + { _dialogInput :: !T.Text } deriving (Show) -makeLenses ''WalletName +makeLenses ''DialogInput + +data DialogType + = WName + | AName + | Blank data State = State { _network :: !String @@ -75,16 +80,16 @@ data State = State , _transactions :: !(L.List Name String) , _msg :: !String , _helpBox :: !Bool - , _walletBox :: !Bool + , _dialogBox :: !DialogType , _splashBox :: !Bool - , _walletForm :: !(Form WalletName () Name) + , _inputForm :: !(Form DialogInput () Name) , _focusRing :: !(F.FocusRing Name) } makeLenses ''State drawUI :: State -> [Widget Name] -drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s] +drawUI s = [splashDialog s, helpDialog s, inputDialog s, ui s] where ui :: State -> Widget Name ui st = @@ -122,13 +127,18 @@ drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s] map (hLimit 40 . str) ["Open help", "Close dialog", "Create Wallet", "Quit"] - walletDialog :: State -> Widget Name - walletDialog st = - if st ^. walletBox - then D.renderDialog - (D.dialog (Just (str "Create Wallet")) Nothing 50) - (renderForm $ st ^. walletForm) - else emptyWidget + inputDialog :: State -> Widget Name + inputDialog st = + case st ^. dialogBox of + WName -> + D.renderDialog + (D.dialog (Just (str "Create Wallet")) Nothing 50) + (renderForm $ st ^. inputForm) + AName -> + D.renderDialog + (D.dialog (Just (str "Create Account")) Nothing 50) + (renderForm $ st ^. inputForm) + Blank -> emptyWidget splashDialog :: State -> Widget Name splashDialog st = if st ^. splashBox @@ -143,9 +153,10 @@ drawUI s = [splashDialog s, helpDialog s, walletDialog s, ui s] C.hCenter (withAttr blinkAttr $ str "Press any key...")) else emptyWidget -mkWalletForm :: WalletName -> Form WalletName e Name -mkWalletForm = - newForm [label "Name: " @@= editTextField walName WalNameField (Just 1)] +mkInputForm :: DialogInput -> Form DialogInput e Name +mkInputForm = + newForm + [label "Name: " @@= editTextField dialogInput DialogInputField (Just 1)] where label s w = padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w @@ -180,23 +191,32 @@ appEvent (BT.VtyEvent e) = do BT.modify $ set helpBox False _ev -> return () else do - if s ^. walletBox - then do + case s ^. dialogBox of + WName -> do case e of - V.EvKey V.KEsc [] -> BT.modify $ set walletBox False + V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank V.EvKey V.KEnter [] -> do - BT.modify $ set walletBox False - fs <- BT.zoom walletForm $ BT.gets formState + BT.modify $ set dialogBox Blank + fs <- BT.zoom inputForm $ BT.gets formState printMsg $ - "Creating new wallet " <> T.unpack (fs ^. walName) - ev -> BT.zoom walletForm $ handleFormEvent (BT.VtyEvent ev) - else do + "Creating new wallet " <> T.unpack (fs ^. dialogInput) + ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev) + AName -> do + case e of + V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank + V.EvKey V.KEnter [] -> do + BT.modify $ set dialogBox Blank + fs <- BT.zoom inputForm $ BT.gets formState + printMsg $ + "Creating new address " <> T.unpack (fs ^. dialogInput) + ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev) + Blank -> do case e of V.EvKey (V.KChar '\t') [] -> focusRing %= F.focusNext V.EvKey (V.KChar 'q') [] -> M.halt V.EvKey (V.KChar '?') [] -> BT.modify $ set helpBox True - V.EvKey (V.KChar 'c') [] -> BT.modify $ set walletBox True - V.EvKey (V.KChar 's') [] -> printMsg "You pressed S!" + V.EvKey (V.KChar 'w') [] -> BT.modify $ set dialogBox WName + V.EvKey (V.KChar 'a') [] -> BT.modify $ set dialogBox AName ev -> case r of Just AList -> BT.zoom addresses $ L.handleListEvent ev @@ -251,9 +271,11 @@ runZenithCLI host port dbFilePath = do ("Start up Ok! Connected to Zebra " ++ (T.unpack . zgi_build) zebra ++ " on port " ++ show port ++ ".") False - (null walList) + (if null walList + then WName + else Blank) True - (mkWalletForm $ WalletName "Main") + (mkInputForm $ DialogInput "Main") (F.focusRing [AList, TList]) Nothing -> do print $