Advertisement
Guest User

Untitled

a guest
May 30th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Brick.AttrMap
  4. import Brick.Main
  5. import Brick.Types
  6. import Brick.Widgets.Core
  7. import Brick.Widgets.List
  8. import Brick.Widgets.Center
  9. import Control.Monad
  10. import Graphics.Vty
  11. import qualified Data.Vector as V
  12.  
  13. listDrawElement :: Bool -> Char -> Widget ()
  14. listDrawElement _ a = str $ show a
  15.  
  16. onDraw :: List () Char -> [Widget ()]
  17. onDraw l = [renderList listDrawElement True l]
  18.  
  19. onEvent :: List () Char -> BrickEvent () e -> EventM() (Next (List () Char))
  20. onEvent l (VtyEvent e) = case e of
  21.   EvKey KEsc [] -> halt l
  22.   _ -> handleListEventVi handleListEvent e l >>= continue
  23. onEvent l _ = continue l
  24.  
  25. theApp :: App (List () Char) e ()
  26. theApp = App { appDraw = onDraw
  27.           , appChooseCursor = neverShowCursor
  28.           , appHandleEvent = onEvent
  29.           , appStartEvent = return
  30.           , appAttrMap = const $ attrMap defAttr []
  31.           }
  32.  
  33. initialState :: List () Char
  34. initialState = list () (V.fromList . take 100 $ cycle ['a','b','c']) 1
  35.  
  36. main :: IO ()
  37. main = void $ defaultMain theApp initialState
  38.  
  39. {-
  40. {-# LANGUAGE CPP #-}
  41. module Main where
  42.  
  43. import Control.Monad (void)
  44. import Data.Maybe (fromMaybe)
  45. #if !(MIN_VERSION_base(4,11,0))
  46. import Data.Monoid
  47. #endif
  48. import qualified Graphics.Vty as V
  49. import Lens.Micro ((^.))
  50.  
  51. import qualified Brick.AttrMap as A
  52. import qualified Brick.Main as M
  53. import Brick.Types (Widget)
  54. import qualified Brick.Types as T
  55. import Brick.Util (fg, on)
  56. import qualified Brick.Widgets.Border as B
  57. import qualified Brick.Widgets.Center as C
  58. import Brick.Widgets.Core (hLimit, str, vBox, vLimit, withAttr, (<+>))
  59. import qualified Brick.Widgets.List as L
  60. import qualified Data.Vector as Vec
  61.  
  62. drawUI :: (Show a) => L.List () a -> [Widget ()]
  63. drawUI l = [ui]
  64.     where
  65.         label = str "Item " <+> cur <+> str " of " <+> total
  66.         cur = case l^.(L.listSelectedL) of
  67.                 Nothing -> str "-"
  68.                 Just i  -> str (show (i + 1))
  69.         total = str $ show $ Vec.length $ l^.(L.listElementsL)
  70.         box = B.borderWithLabel label $
  71.               hLimit 25 $
  72.               vLimit 15 $
  73.               L.renderList listDrawElement True l
  74.         ui = C.vCenter $ vBox [ C.hCenter box
  75.                               , str " "
  76.                               , C.hCenter $ str "Press +/- to add/remove list elements."
  77.                               , C.hCenter $ str "Press Esc to exit."
  78.                               ]
  79.  
  80. appEvent :: L.List () Char -> T.BrickEvent () e -> T.EventM () (T.Next (L.List () Char))
  81. appEvent l (T.VtyEvent e) =
  82.     case e of
  83.         V.EvKey (V.KChar '+') [] ->
  84.             let el = nextElement (L.listElements l)
  85.                 pos = Vec.length $ l^.(L.listElementsL)
  86.             in M.continue $ L.listInsert pos el l
  87.  
  88.         V.EvKey (V.KChar '-') [] ->
  89.             case l^.(L.listSelectedL) of
  90.                 Nothing -> M.continue l
  91.                 Just i  -> M.continue $ L.listRemove i l
  92.  
  93.         V.EvKey V.KEsc [] -> M.halt l
  94.  
  95.         ev -> M.continue =<< (L.handleListEventVi L.handleListEvent) ev l
  96.     where
  97.       nextElement :: Vec.Vector Char -> Char
  98.       nextElement v = fromMaybe '?' $ Vec.find (flip Vec.notElem v) (Vec.fromList ['a' .. 'z'])
  99. appEvent l _ = M.continue l
  100.  
  101. listDrawElement :: (Show a) => Bool -> a -> Widget ()
  102. listDrawElement sel a =
  103.     let selStr s = if sel
  104.                    then withAttr customAttr (str $ "<" <> s <> ">")
  105.                    else str s
  106.     in C.hCenter $ str "Item " <+> (selStr $ show a)
  107.  
  108. initialState :: L.List () Char
  109. initialState = L.list () (Vec.fromList ['a','b','c']) 1
  110.  
  111. customAttr :: A.AttrName
  112. customAttr = L.listSelectedAttr <> "custom"
  113.  
  114. theMap :: A.AttrMap
  115. theMap = A.attrMap V.defAttr
  116.     [ (L.listAttr,            V.white `on` V.blue)
  117.     , (L.listSelectedAttr,    V.blue `on` V.white)
  118.     , (customAttr,            fg V.cyan)
  119.     ]
  120.  
  121. theApp :: M.App (L.List () Char) e ()
  122. theApp =
  123.     M.App { M.appDraw = drawUI
  124.           , M.appChooseCursor = M.showFirstCursor
  125.           , M.appHandleEvent = appEvent
  126.           , M.appStartEvent = return
  127.           , M.appAttrMap = const theMap
  128.           }
  129.  
  130. main :: IO ()
  131. main = void $ M.defaultMain theApp initialState
  132. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement