brick/programs/ListDemo.hs

84 lines
2.3 KiB
Haskell
Raw Normal View History

2015-06-28 23:06:22 +03:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Lens
import Control.Monad (void)
import Data.Monoid
import Graphics.Vty
import Brick.Main
import Brick.Core
import Brick.Widgets.Core
import Brick.Widgets.Border
import Brick.Widgets.List
import Brick.Widgets.Center
import Brick.AttrMap
import Brick.Util
2015-06-28 23:22:05 +03:00
drawUI :: List Int -> [Widget]
drawUI l = [ui]
2015-06-28 23:06:22 +03:00
where
label = "Item " <+> cur <+> " of " <+> total
2015-06-28 23:22:05 +03:00
cur = case l^.listSelectedL of
2015-06-28 23:06:22 +03:00
Nothing -> "-"
Just i -> str (show (i + 1))
2015-06-28 23:22:05 +03:00
total = str $ show $ length $ l^.listElementsL
2015-06-28 23:15:15 +03:00
box = borderWithLabel label $
hLimit 25 $
vLimit 15 $
2015-06-28 23:22:05 +03:00
renderList l
2015-06-28 23:15:15 +03:00
ui = vCenter $ vBox [ hCenter box
, " "
, hCenter "Press +/- to add/remove list elements."
, hCenter "Press Esc to exit."
]
2015-06-28 23:06:22 +03:00
2015-06-28 23:22:05 +03:00
appEvent :: Event -> List Int -> EventM (Next (List Int))
appEvent e l =
2015-06-28 23:06:22 +03:00
case e of
2015-06-28 23:15:15 +03:00
EvKey (KChar '+') [] ->
2015-06-28 23:22:05 +03:00
let el = length $ l^.listElementsL
in continue $ listInsert el el l
2015-06-28 23:06:22 +03:00
2015-06-28 23:15:15 +03:00
EvKey (KChar '-') [] ->
2015-06-28 23:22:05 +03:00
case l^.listSelectedL of
Nothing -> continue l
Just i -> continue $ listRemove i l
2015-06-28 23:15:15 +03:00
2015-06-28 23:22:05 +03:00
EvKey KEsc [] -> halt l
2015-06-28 23:06:22 +03:00
2015-06-28 23:22:05 +03:00
ev -> continue $ handleEvent ev l
2015-06-28 23:06:22 +03:00
listDrawElement :: Bool -> Int -> Widget
listDrawElement sel i =
let selStr s = if sel
then withAttrName customAttr (str $ "<" <> s <> ">")
else str s
2015-06-29 06:05:30 +03:00
in (hCenterWith (Just ' ') $ vBox $ for [1..i+1] $ \j ->
"Item " <+> (selStr $ show i) <+> " Line " <+> (str $ show j)) <=> hBorder
2015-06-28 23:06:22 +03:00
2015-06-28 23:22:05 +03:00
initialState :: List Int
2015-06-29 06:05:30 +03:00
initialState = list (Name "list") listDrawElement [0, 1, 2]
2015-06-28 23:06:22 +03:00
customAttr :: AttrName
customAttr = listSelectedAttr <> "custom"
theMap :: AttrMap
theMap = attrMap defAttr
[ (listAttr, white `on` blue)
, (listSelectedAttr, blue `on` white)
, (customAttr, fg cyan)
]
2015-06-28 23:22:05 +03:00
theApp :: App (List Int) Event
2015-06-28 23:06:22 +03:00
theApp =
App { appDraw = drawUI
, appChooseCursor = showFirstCursor
, appHandleEvent = appEvent
, appAttrMap = const theMap
, appMakeVtyEvent = id
}
main :: IO ()
main = void $ defaultMain theApp initialState