From 830cdb695e58938bc51ac072fe537fc17468d293 Mon Sep 17 00:00:00 2001 From: Jonathan Daugherty Date: Mon, 11 May 2015 07:55:48 -0700 Subject: [PATCH] Use lenses in test program --- brick.cabal | 3 ++- programs/Main.hs | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/brick.cabal b/brick.cabal index b9abe9e..a218990 100644 --- a/brick.cabal +++ b/brick.cabal @@ -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 diff --git a/programs/Main.hs b/programs/Main.hs index 88d4417..0a7b60c 100644 --- a/programs/Main.hs +++ b/programs/Main.hs @@ -1,48 +1,53 @@ {-# 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) ] ] 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 KEsc [] -> exitSuccess + 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