mirror of
https://github.com/jtdaugherty/brick.git
synced 2025-01-07 14:36:59 +03:00
Implement list element insertion and add demo
This commit is contained in:
parent
6e7d92c994
commit
4422a8a99f
@ -37,7 +37,9 @@ appEvent :: Event -> St -> IO St
|
||||
appEvent e st =
|
||||
case e of
|
||||
EvKey KEsc [] -> exitSuccess
|
||||
EvKey KEnter [] -> error $ editStr $ st^.stEditor
|
||||
EvKey KEnter [] ->
|
||||
let e = length $ listElements $ st^.stList
|
||||
in return $ st & stList %~ listInsert 0 e
|
||||
ev -> return $ st & stEditor %~ (handleEvent ev)
|
||||
& stList %~ (handleEvent ev)
|
||||
|
||||
|
14
src/Brick.hs
14
src/Brick.hs
@ -328,6 +328,20 @@ drawList l =
|
||||
vScroll (listScroll l) $
|
||||
VBox drawn <<= VPad ' '
|
||||
|
||||
listInsert :: Int -> a -> List a -> List a
|
||||
listInsert pos e l =
|
||||
let es = listElements l
|
||||
newSel = case listSelected l of
|
||||
Nothing -> 0
|
||||
Just s -> if pos < s
|
||||
then s + 1
|
||||
else s
|
||||
(front, back) = splitAt pos es
|
||||
in l { listSelected = Just newSel
|
||||
, listElements = front ++ (e : back)
|
||||
, listScroll = vScrollToView newSel (listScroll l)
|
||||
}
|
||||
|
||||
class HandleEvent a where
|
||||
handleEvent :: Event -> a -> a
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user