drawUI :: St -> [Widget ()]
drawUI st = [a]
where
a = (str $"Last event: "<> (show $ st^.stLastBrickEvent))
<=>
(str $"Counter value is: "<> (show $ st^.stCounter))
さすがに 最初の newBChan 10 は氣になるので 少し調べた
モジュール Brick.BChan で
BChan といふデータは
BChan is an abstract type representing a bounded FIFO channel.
と書かれてゐる
有限の FIFOチャネルを表す? FIFO って First In First Out ってこと? 情報技術者試験の勉強で出てきたやうな・・・
スタックみたいなもんだと 考へていいのかな・・・
んで newBChan 10 といふのは そのチャネルを10個まで持てるやうなデータ BChanをつくるってこと?
{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE TemplateHaskell #-}{-# LANGUAGE RankNTypes #-}module Main whereimport Lens.Micro
import Lens.Micro.TH
import Lens.Micro.Mtl
importqualified Graphics.Vty as V
importqualified Brick.Main as M
importqualified Brick.Types as T
import Brick.Widgets.Core
( (<+>)
, (<=>)
, hLimit
, vLimit
, str
)
importqualified Brick.Widgets.Center as C
importqualified Brick.Widgets.Edit as E
importqualified Brick.AttrMap as A
importqualified Brick.Focus as F
import Brick.Util (on)
data Name = Edit1
| Edit2
deriving (Ord, Show, Eq)
data St =
St { _focusRing :: F.FocusRing Name
, _edit1 :: E.Editor String Name
, _edit2 :: E.Editor String Name
}
makeLenses ''St
drawUI :: St -> [T.Widget Name]
drawUI st = [ui]
where
e1 = F.withFocusRing (st^.focusRing) (E.renderEditor (str . unlines)) (st^.edit1)
e2 = F.withFocusRing (st^.focusRing) (E.renderEditor (str . unlines)) (st^.edit2)
ui = C.center $
(str "Input 1 (unlimited): "<+> (hLimit 30$ vLimit 5 e1)) <=>
str " "<=>
(str "Input 2 (limited to 2 lines): "<+> (hLimit 30 e2)) <=>
str " "<=>
str "Press Tab to switch between editors, Esc to quit."
appEvent :: T.BrickEvent Name e -> T.EventM Name St ()
appEvent (T.VtyEvent (V.EvKey V.KEsc [])) =
M.halt
appEvent (T.VtyEvent (V.EvKey (V.KChar '\t') [])) =
focusRing %= F.focusNext
appEvent (T.VtyEvent (V.EvKey V.KBackTab [])) =
focusRing %= F.focusPrev
appEvent ev =do
r <- use focusRing
case F.focusGetCurrent r of
Just Edit1 -> zoom edit1 $ E.handleEditorEvent ev
Just Edit2 -> zoom edit2 $ E.handleEditorEvent ev
Nothing -> return ()
initialState :: St
initialState =
St (F.focusRing [Edit1, Edit2])
(E.editor Edit1 Nothing "")
(E.editor Edit2 (Just 2) "")
theMap :: A.AttrMap
theMap = A.attrMap V.defAttr
[ (E.editAttr, V.white `on` V.blue)
, (E.editFocusedAttr, V.black `on` V.yellow)
]
appCursor :: St -> [T.CursorLocation Name] -> Maybe (T.CursorLocation Name)
appCursor = F.focusRingCursor (^.focusRing)
theApp :: M.App St e Name
theApp =
M.App { M.appDraw = drawUI
, M.appChooseCursor = appCursor
, M.appHandleEvent = appEvent
, M.appStartEvent = return ()
, M.appAttrMap = const theMap
}
main :: IO ()
main =do
st <- M.defaultMain theApp initialState
putStrLn "In input 1 you entered:\n"
putStrLn $ unlines $ E.getEditContents $ st^.edit1
putStrLn "In input 2 you entered:\n"
putStrLn $ unlines $ E.getEditContents $ st^.edit2
はい わかりません
では まづ 最初の import文などは讀み飛ばして
data Name = Edit1
| Edit2
deriving (Ord, Show, Eq)
data St =
St { _focusRing :: F.FocusRing Name
, _edit1 :: E.Editor String Name
, _edit2 :: E.Editor String Name
}
あたりから見ていくか
Name は 單に名前のことだと分かる
St は状態なんだらうけど focusRing って何だろ?
edit1 とか edit2 は デモ画面にある 二つのエディタのことを指すんだらう
withFocusRing :: (Eq n, Named a n) => FocusRing n -> (Bool -> a -> b) -> a -> b
となってる
FocusRing n といふのが フォーカスリング(多分 エディットする部分が黄色くなるやうなやつだと思ふ)
(Bool -> a -> b) といふのが 真偽値と なんかを取って なんかを返す函數 (E.renderEditor (str .unlines) の部分)
a にあたるのが (st^.edit1)の部分
あぶないあぶない
ちょっと 深く見すぎた
次行ってみやう
appEvent :: T.BrickEvent Name e -> T.EventM Name St ()
appEvent (T.VtyEvent (V.EvKey V.KEsc [])) =
M.halt
appEvent (T.VtyEvent (V.EvKey (V.KChar '\t') [])) =
focusRing %= F.focusNext
appEvent (T.VtyEvent (V.EvKey V.KBackTab [])) =
focusRing %= F.focusPrev
appEvent ev =do
r <- use focusRing
case F.focusGetCurrent r of
Just Edit1 -> zoom edit1 $ E.handleEditorEvent ev
Just Edit2 -> zoom edit2 $ E.handleEditorEvent ev
Nothing -> return ()