Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Created June 8, 2016 22:33
Show Gist options
  • Save simonmichael/f75813855f56c54b08e19f89155517da to your computer and use it in GitHub Desktop.
Save simonmichael/f75813855f56c54b08e19f89155517da to your computer and use it in GitHub Desktop.
hledger-ui lensification WIP
-- | Types of screen available within hledger-ui. Each has its own
-- specific state type, and generic initialisation, event handling
-- and rendering functions.
--
-- Screen types are pattern-matched by their constructor and their
-- state field, which must have a unique name. This type causes
-- partial functions, so take care.
data Screen =
AccountsScreen {
_asState :: AccountsScreenState
,sInitFn :: Day -> Bool -> AppState -> AppState -- ^ function to generate the screen's state on entry or change
,sDrawFn :: AppState -> [Widget] -- ^ brick renderer for this screen
,sHandleFn :: AppState -> Vty.Event -> EventM (Next AppState) -- ^ brick event handler for this screen
}
| RegisterScreen {
rsState :: RegisterScreenState
,sInitFn :: Day -> Bool -> AppState -> AppState
,sDrawFn :: AppState -> [Widget]
,sHandleFn :: AppState -> Vty.Event -> EventM (Next AppState)
}
| TransactionScreen {
tsState :: TransactionScreenState
,sInitFn :: Day -> Bool -> AppState -> AppState
,sDrawFn :: AppState -> [Widget]
,sHandleFn :: AppState -> Vty.Event -> EventM (Next AppState)
}
| ErrorScreen {
esState :: ErrorScreenState
,sInitFn :: Day -> Bool -> AppState -> AppState
,sDrawFn :: AppState -> [Widget]
,sHandleFn :: AppState -> Vty.Event -> EventM (Next AppState)
}
deriving (Show)
instance Show (List a) where show _ = "<List>"
instance Show Editor where show _ = "<Editor>"
instance Monoid (List a)
where
mempty = list "" V.empty 1
mappend a b = a & listElementsL .~ (a^.listElementsL <> b^.listElementsL)
-- | Render state for this type of screen.
data AccountsScreenState = AccountsScreenState {
_asItems :: List AccountsScreenItem -- ^ list of account names & balances
,_asSelectedAccount :: AccountName -- ^ full name of the currently selected account (or "")
} deriving (Show)
-- | An item in the accounts screen's list of accounts and balances.
data AccountsScreenItem = AccountsScreenItem {
asItemIndentLevel :: Int -- ^ indent level
,asItemAccountName :: AccountName -- ^ full account name
,asItemDisplayAccountName :: AccountName -- ^ full or short account name to display
,asItemRenderedAmounts :: [String] -- ^ rendered amounts
}
-- | Render state for this type of screen.
data RegisterScreenState = RegisterScreenState {
rsItems :: List RegisterScreenItem -- ^ list of transactions affecting this account
,rsSelectedAccount :: AccountName -- ^ full name of the account we are showing a register for
} deriving (Show)
-- | An item in the register screen's list of transactions in the current account.
data RegisterScreenItem = RegisterScreenItem {
rsItemDate :: String -- ^ date
,rsItemDescription :: String -- ^ description
,rsItemOtherAccounts :: String -- ^ other accounts
,rsItemChangeAmount :: String -- ^ the change to the current account from this transaction
,rsItemBalanceAmount :: String -- ^ the balance or running total after this transaction
,rsItemTransaction :: Transaction -- ^ the full transaction
}
-- | Render state for this type of screen.
data TransactionScreenState = TransactionScreenState {
tsTransaction :: NumberedTransaction -- ^ the transaction we are currently viewing, and its position in the list
,tsTransactions :: [NumberedTransaction] -- ^ the list of transactions we can step through
,tsSelectedAccount :: AccountName -- ^ the account whose register we entered this screen from
} deriving (Show)
type NumberedTransaction = (Integer, Transaction)
-- | Render state for this type of screen.
data ErrorScreenState = ErrorScreenState {
esError :: String -- ^ error message to show
} deriving (Show)
-- makeLenses ''AccountsScreenState
concat <$> mapM makeLenses [
''AccountsScreenState
,''RegisterScreenState
,''TransactionScreenState
,''ErrorScreenState
,''Screen
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment