brick/programs/Main.hs

73 lines
1.7 KiB
Haskell
Raw Normal View History

2015-05-09 09:09:40 +03:00
{-# LANGUAGE OverloadedStrings #-}
2015-05-11 17:55:48 +03:00
{-# LANGUAGE TemplateHaskell #-}
2015-05-09 09:09:40 +03:00
module Main where
2015-05-11 17:55:48 +03:00
import Control.Lens
2015-05-09 19:56:14 +03:00
import Data.Default
2015-05-17 19:22:08 +03:00
import Data.Monoid
2015-05-09 09:09:40 +03:00
import Graphics.Vty
import System.Exit
import Brick
2015-05-09 10:18:29 +03:00
data St =
2015-05-11 17:55:48 +03:00
St { _stEditor :: Editor
2015-05-17 19:22:08 +03:00
, _stList :: List Int
}
2015-05-09 10:18:29 +03:00
2015-05-11 17:55:48 +03:00
makeLenses ''St
2015-05-16 08:03:53 +03:00
drawUI :: St -> [Prim]
drawUI st = [a]
2015-05-10 00:28:37 +03:00
where
2015-05-17 05:16:06 +03:00
a = centered $
bordered $
2015-05-17 19:22:08 +03:00
(VLimit 1 $
HLimit 25 $
UseAttr (cyan `on` blue) $
edit (st^.stEditor))
<<=
HFill '-'
=>>
(VLimit 10 $
HLimit 25 $
list (st^.stList))
2015-05-09 09:09:40 +03:00
2015-05-11 01:51:08 +03:00
handleEvent :: Event -> St -> IO St
handleEvent e st =
2015-05-09 09:09:40 +03:00
case e of
2015-05-17 05:42:25 +03:00
EvKey KEsc [] -> exitSuccess
EvKey KEnter [] -> error $ editStr $ st^.stEditor
ev -> return $ st & stEditor %~ (editEvent ev)
2015-05-17 19:22:08 +03:00
& stList %~ (listEvent ev)
initialState :: St
initialState =
2015-05-11 19:56:11 +03:00
St { _stEditor = editor (Name "edit") ""
2015-05-17 19:22:08 +03:00
, _stList = newList (Name "list") listDraw [0..6]
}
2015-05-17 19:22:08 +03:00
listDraw :: Bool -> Int -> Prim
listDraw sel i =
let selAttr = white `on` blue
2015-05-17 19:29:53 +03:00
p = hCentered (Txt $ "Number " <> show i)
2015-05-17 19:22:08 +03:00
in if sel
then UseAttr selAttr p
else p
2015-05-11 01:51:08 +03:00
theApp :: App St Event
2015-05-11 00:56:58 +03:00
theApp =
2015-05-09 19:56:14 +03:00
def { appDraw = drawUI
2015-05-11 01:42:59 +03:00
, appChooseCursor = showFirstCursor
, appHandleEvent = handleEvent
2015-05-17 18:17:06 +03:00
, appHandleSize =
2015-05-17 19:22:08 +03:00
\name sz st ->
case name of
Name "edit" -> st & stEditor %~ setSize sz
Name "list" -> st & stList %~ setSize sz
_ -> st
}
2015-05-09 09:09:40 +03:00
main :: IO ()
2015-05-11 01:51:08 +03:00
main = defaultMain theApp initialState