CLI enhancements to manage lists of items #60

Merged
pitmutt merged 2 commits from rav001 into dev041 2024-02-09 22:21:46 +00:00
10 changed files with 166 additions and 21 deletions

View file

@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## Added
- `Core` module
- `CLI` module
- `DB` module
- Command line arguments to switch to legacy version
## [0.4.1] ## [0.4.1]
### Fixed ### Fixed

View file

@ -2,7 +2,6 @@
module Main where module Main where
import Brick (simpleMain)
import Control.Monad (void) import Control.Monad (void)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as B import qualified Data.ByteString as B
@ -200,6 +199,7 @@ main :: IO ()
main = do main = do
config <- load ["zenith.cfg"] config <- load ["zenith.cfg"]
args <- getArgs args <- getArgs
dbName <- require config "dbName"
nodeUser <- require config "nodeUser" nodeUser <- require config "nodeUser"
nodePwd <- require config "nodePwd" nodePwd <- require config "nodePwd"
if not (null args) if not (null args)
@ -215,7 +215,7 @@ main = do
" ______ _ _ _ \n |___ / (_) | | | \n / / ___ _ __ _| |_| |__ \n / / / _ \\ '_ \\| | __| '_ \\ \n / /_| __/ | | | | |_| | | |\n /_____\\___|_| |_|_|\\__|_| |_|\n Zcash Full Node CLI v0.4.0" " ______ _ _ _ \n |___ / (_) | | | \n / / ___ _ __ _| |_| |__ \n / / / _ \\ '_ \\| | __| '_ \\ \n / /_| __/ | | | | |_| | | |\n /_____\\___|_| |_|_|\\__|_| |_|\n Zcash Full Node CLI v0.4.0"
} }
(root nodeUser nodePwd) (root nodeUser nodePwd)
"cli" -> simpleMain ui "cli" -> runZenithCLI dbName
_ -> printUsage _ -> printUsage
else printUsage else printUsage

View file

@ -44,6 +44,11 @@ library:
- persistent-sqlite - persistent-sqlite
- persistent-template - persistent-template
- brick - brick
- mtl
- microlens
- microlens-mtl
- microlens-th
- vty
- zcash-haskell - zcash-haskell
executables: executables:

View file

@ -1,13 +1,122 @@
{-# LANGUAGE TemplateHaskell #-}
module Zenith.CLI where module Zenith.CLI where
import Brick (Widget, (<+>), joinBorders, simpleMain, str, withBorderStyle) import Control.Monad (void)
import Brick.Widgets.Border (borderWithLabel, vBorder) import Control.Monad.State (modify)
import Brick.Widgets.Border.Style (unicode) import Data.Maybe (fromMaybe)
import Brick.Widgets.Center (center) import qualified Data.Text as T
import qualified Graphics.Vty as V
import Lens.Micro ((^.))
import Lens.Micro.Mtl
import Lens.Micro.TH
ui :: Widget () import qualified Brick.AttrMap as A
ui = import qualified Brick.Main as M
joinBorders $ import qualified Brick.Types as BT
withBorderStyle unicode $ import Brick.Types (Widget)
borderWithLabel (str "Zenith") $ import Brick.Util (fg, on)
(center (str "Addresses") <+> vBorder <+> center (str "Transactions")) import qualified Brick.Widgets.Border as B
import Brick.Widgets.Border.Style (unicode)
import qualified Brick.Widgets.Center as C
import Brick.Widgets.Core
( (<+>)
, hLimit
, joinBorders
, str
, vBox
, vLimit
, withAttr
, withBorderStyle
)
import qualified Brick.Widgets.List as L
import qualified Data.Vector as Vec
import Zenith.Core
data Name
= WList
| AList
| TList
deriving (Eq, Show, Ord)
data State = State
{ _network :: String
, _wallets :: L.List Name String
, _addresses :: L.List Name String
, _transactions :: L.List Name String
} deriving (Show)
makeLenses ''State
drawUI :: State -> [Widget Name]
drawUI s = [ui s]
where
ui :: State -> Widget Name
ui s =
joinBorders $
withBorderStyle unicode $
B.borderWithLabel (str $ "Zenith - " <> s ^. network) $
(C.center (listBox "Addresses" (s ^. addresses)) <+>
B.vBorder <+> C.center (listBox "Transactions" (s ^. transactions)))
listBox :: String -> L.List Name String -> Widget Name
listBox titleLabel l =
C.vCenter $
vBox
[ C.hCenter
(B.borderWithLabel (str titleLabel) $
hLimit 25 $ vLimit 15 $ L.renderList listDrawElement True l)
, str " "
, C.hCenter $ str "Select "
]
listDrawElement :: (Show a) => Bool -> a -> Widget Name
listDrawElement sel a =
let selStr s =
if sel
then withAttr customAttr (str $ "<" <> s <> ">")
else str s
in C.hCenter $ selStr $ show a
initialState :: State
initialState =
State
"Main"
(L.list WList (Vec.fromList ["wall1"]) 1)
(L.list AList (Vec.fromList ["addr1", "addr2"]) 1)
(L.list TList (Vec.fromList ["tx1", "tx2", "tx3"]) 1)
customAttr :: A.AttrName
customAttr = L.listSelectedAttr <> A.attrName "custom"
appEvent :: BT.BrickEvent Name e -> BT.EventM Name State ()
appEvent (BT.VtyEvent e) =
case e of
V.EvKey V.KEsc [] -> M.halt
ev -> BT.zoom addresses $ L.handleListEvent ev
theMap :: A.AttrMap
theMap =
A.attrMap
V.defAttr
[ (L.listAttr, V.white `on` V.blue)
, (L.listSelectedAttr, V.blue `on` V.white)
, (customAttr, fg V.cyan)
]
theApp :: M.App State e Name
theApp =
M.App
{ M.appDraw = drawUI
, M.appChooseCursor = M.showFirstCursor
, M.appHandleEvent = appEvent
, M.appStartEvent = return ()
, M.appAttrMap = const theMap
}
runZenithCLI :: T.Text -> IO ()
runZenithCLI dbName = do
w <- checkWallets dbName
if (null w)
then void $ M.defaultMain theApp initialState
else do
print "No wallet found. Create one? Y/N"

12
src/Zenith/Core.hs Normal file
View file

@ -0,0 +1,12 @@
module Zenith.Core where
import qualified Data.Text as T
import Database.Persist
import Database.Persist.Sqlite
import Zenith.DB
checkWallets :: T.Text -> IO [Entity ZcashWallet]
checkWallets dbName = do
runSqlite dbName $ do runMigration migrateAll
wallets <- runSqlite dbName $ selectList [ZcashWalletBirthdayHeight >. 0] []
return wallets

View file

@ -21,7 +21,9 @@ import qualified Data.Text as T
import Database.Persist import Database.Persist
import Database.Persist.Sqlite import Database.Persist.Sqlite
import Database.Persist.TH import Database.Persist.TH
import ZcashHaskell.Types (Phrase) import ZcashHaskell.Types (Phrase, ZcashNet)
derivePersistField "ZcashNet"
share share
[mkPersist sqlSettings, mkMigrate "migrateAll"] [mkPersist sqlSettings, mkMigrate "migrateAll"]
@ -32,5 +34,6 @@ share
tPrivateKey BS.ByteString tPrivateKey BS.ByteString
birthdayHeight Int birthdayHeight Int
name T.Text name T.Text
network ZcashNet
deriving Show deriving Show
|] |]

View file

@ -44,9 +44,9 @@ packages:
# extra-deps: [] # extra-deps: []
extra-deps: extra-deps:
- git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git - git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git
commit: 6ea8698ccb5e44f9900ba0e61c6ffe6cba900139 commit: c4f345b1deb876e19a51c5f7ae1b4402fae14126
- git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git - git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git
commit: fe2df6f7d63272ac147911c1573550bed1d38a37 commit: fd1ddce73c0ad18a2a4509a299c6e93f8c6c383d
- git: https://git.vergara.tech/Vergara_Tech/haskell-foreign-rust.git - git: https://git.vergara.tech/Vergara_Tech/haskell-foreign-rust.git
commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05 commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05
- git: https://github.com/well-typed/borsh.git - git: https://github.com/well-typed/borsh.git

View file

@ -5,26 +5,26 @@
packages: packages:
- completed: - completed:
commit: 6ea8698ccb5e44f9900ba0e61c6ffe6cba900139 commit: c4f345b1deb876e19a51c5f7ae1b4402fae14126
git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git
name: zcash-haskell name: zcash-haskell
pantry-tree: pantry-tree:
sha256: 0506f9f095dbb134a4e7b3ba73a60a21c6298cbea01409871141b31cd0cf9c46 sha256: 1bf709484bc488e51e18aa11001abdc06100ed7086b9bbd765d28c4a3d8e9113
size: 1366 size: 1366
version: 0.4.1 version: 0.4.1
original: original:
commit: 6ea8698ccb5e44f9900ba0e61c6ffe6cba900139 commit: c4f345b1deb876e19a51c5f7ae1b4402fae14126
git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git git: https://git.vergara.tech/Vergara_Tech/zcash-haskell.git
- completed: - completed:
commit: fe2df6f7d63272ac147911c1573550bed1d38a37 commit: fd1ddce73c0ad18a2a4509a299c6e93f8c6c383d
git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git
name: hexstring name: hexstring
pantry-tree: pantry-tree:
sha256: 71f12a60e85f7e1897b07bb2d4c77794faee50df250d68b0c47b3d343dd4625a sha256: 05af6ec085b0c8ac00e0c3043652095a6a9c9d3bd2112ffdcb4c4e28206e0b1c
size: 741 size: 741
version: 0.12.0 version: 0.12.0
original: original:
commit: fe2df6f7d63272ac147911c1573550bed1d38a37 commit: fd1ddce73c0ad18a2a4509a299c6e93f8c6c383d
git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git git: https://git.vergara.tech/Vergara_Tech/haskell-hexstring.git
- completed: - completed:
commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05 commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05

View file

@ -26,6 +26,7 @@ source-repository head
library library
exposed-modules: exposed-modules:
Zenith.CLI Zenith.CLI
Zenith.Core
Zenith.DB Zenith.DB
Zenith.Types Zenith.Types
Zenith.Utils Zenith.Utils
@ -44,6 +45,10 @@ library
, bytestring , bytestring
, http-conduit , http-conduit
, http-types , http-types
, microlens
, microlens-mtl
, microlens-th
, mtl
, persistent , persistent
, persistent-sqlite , persistent-sqlite
, persistent-template , persistent-template
@ -54,6 +59,7 @@ library
, scientific , scientific
, text , text
, vector , vector
, vty
, zcash-haskell , zcash-haskell
default-language: Haskell2010 default-language: Haskell2010

View file

@ -1,2 +1,3 @@
nodeUser = "user" nodeUser = "user"
nodePwd = "superSecret" nodePwd = "superSecret"
dbName = "zenith.db"