Use lenses in test program

This commit is contained in:
Jonathan Daugherty 2015-05-11 07:55:48 -07:00
parent 17f8c9eefd
commit 830cdb695e
2 changed files with 20 additions and 14 deletions

View File

@ -29,7 +29,8 @@ executable brick
build-depends: base,
brick,
vty >= 5.2.9,
data-default
data-default,
lens
executable brick-rogue
hs-source-dirs: programs

View File

@ -1,30 +1,35 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Lens
import Data.Default
import Data.Monoid
import Graphics.Vty
import System.Exit
import Brick
data St =
St { stEditor :: Editor
, trans :: Location
St { _stEditor :: Editor
, _trans :: Location
}
makeLenses ''St
eName :: Name
eName = Name "edit"
drawUI :: St -> [Widget]
drawUI st = [top]
where
top = translated (trans st) $
top = translated (st^.trans) $
bordered $
hLimit 40 $
vBox [ "Top"
, hBorder '-'
, hBox [ " Edit: "
, hLimit 20 $ edit (stEditor st) `withAttr` (cyan `on` blue)
, hLimit 20 $ edit (st^.stEditor) `withAttr` (cyan `on` blue)
]
]
@ -32,17 +37,17 @@ handleEvent :: Event -> St -> IO St
handleEvent e st =
case e of
EvKey KEsc [] -> exitSuccess
EvKey KEnter [] -> error $ editStr $ stEditor st
EvKey KLeft [MCtrl] -> return $ st { trans = trans st `locOffset` (Location (-1, 0)) }
EvKey KRight [MCtrl] -> return $ st { trans = trans st `locOffset` (Location (1, 0)) }
EvKey KUp [MCtrl] -> return $ st { trans = trans st `locOffset` (Location (0, -1)) }
EvKey KDown [MCtrl] -> return $ st { trans = trans st `locOffset` (Location (0, 1)) }
ev -> return $ st { stEditor = editEvent ev (stEditor st) }
EvKey KEnter [] -> error $ editStr $ st^.stEditor
EvKey KLeft [MCtrl] -> return $ st & trans %~ (<> (Location (-1, 0)))
EvKey KRight [MCtrl] -> return $ st & trans %~ (<> (Location (1, 0)))
EvKey KUp [MCtrl] -> return $ st & trans %~ (<> (Location (0, -1)))
EvKey KDown [MCtrl] -> return $ st & trans %~ (<> (Location (0, 1)))
ev -> return $ st & stEditor %~ (editEvent ev)
initialState :: St
initialState =
St { stEditor = editor eName ""
, trans = Location (0, 0)
St { _stEditor = editor eName ""
, _trans = Location (0, 0)
}
theApp :: App St Event