Implement list element insertion and add demo

This commit is contained in:
Jonathan Daugherty 2015-05-17 09:53:00 -07:00
parent 6e7d92c994
commit 4422a8a99f
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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