Add updateWithLens, finish FileBrowser updates

This commit is contained in:
Jonathan Daugherty 2022-07-16 13:28:47 -07:00
parent 5db48f2820
commit 74ba7b6c56
2 changed files with 31 additions and 21 deletions

View File

@ -33,6 +33,7 @@ module Brick.Types
, EventM(..)
, BrickEvent(..)
, handleEventLensed
, updateWithLens
-- * Rendering infrastructure
, RenderM
@ -127,7 +128,16 @@ handleEventLensed :: Lens' s a
-> e
-- ^ The event to handle.
-> EventM n s ()
handleEventLensed target handleEvent ev = do
handleEventLensed target handleEvent ev =
updateWithLens target (handleEvent ev)
updateWithLens :: Lens' s a
-- ^ The lens to use to extract and store the state
-- mutated by the action.
-> EventM n a ()
-- ^ The action to run.
-> EventM n s ()
updateWithLens target act = do
ro <- EventM ask
s <- EventM $ lift get
let stInner = ES { applicationState = (applicationState s)^.target
@ -136,7 +146,7 @@ handleEventLensed target handleEvent ev = do
, cacheInvalidateRequests = cacheInvalidateRequests s
, requestedVisibleNames = requestedVisibleNames s
}
((), stInnerFinal) <- liftIO $ runStateT (runReaderT (runEventM (handleEvent ev)) ro) stInner
((), stInnerFinal) <- liftIO $ runStateT (runReaderT (runEventM act) ro) stInner
EventM $ lift $ put $ s { applicationState = applicationState s & target .~ applicationState stInnerFinal }
-- | The monad in which event handlers run. Although it may be tempting

View File

@ -143,7 +143,7 @@ where
import qualified Control.Exception as E
import Control.Monad (forM)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.State (get, modify)
import Control.Monad.State (put, get, modify)
import Data.Char (toLower, isPrint)
import Data.Maybe (fromMaybe, isJust, fromJust)
import qualified Data.Foldable as F
@ -358,10 +358,10 @@ setWorkingDirectory path b = do
Left (_::E.IOException) -> entries
Right parent -> parent : entries
let b' = setEntries allEntries b
return $ b' & fileBrowserWorkingDirectoryL .~ path
& fileBrowserExceptionL .~ exc
& fileBrowserSelectedFilesL .~ mempty
return $ (setEntries allEntries b)
& fileBrowserWorkingDirectoryL .~ path
& fileBrowserExceptionL .~ exc
& fileBrowserSelectedFilesL .~ mempty
parentOf :: FilePath -> IO FileInfo
parentOf path = getFileInfo ".." $ FP.takeDirectory path
@ -609,19 +609,19 @@ actionFileBrowserSelectCurrent =
actionFileBrowserListPageUp :: Ord n => EventM n (FileBrowser n) ()
actionFileBrowserListPageUp =
handleEventLensed fileBrowserEntriesL listMovePageUp
updateWithLens fileBrowserEntriesL listMovePageUp
actionFileBrowserListPageDown :: Ord n => EventM n (FileBrowser n) ()
actionFileBrowserListPageDown =
handleEventLensed fileBrowserEntriesL listMovePageDown
updateWithLens fileBrowserEntriesL listMovePageDown
actionFileBrowserListHalfPageUp :: Ord n => EventM n (FileBrowser n) ()
actionFileBrowserListHalfPageUp b =
handleEventLensed fileBrowserEntriesL (listMoveByPages (-0.5::Double))
actionFileBrowserListHalfPageUp =
updateWithLens fileBrowserEntriesL (listMoveByPages (-0.5::Double))
actionFileBrowserListHalfPageDown :: Ord n => EventM n (FileBrowser n) ()
actionFileBrowserListHalfPageDown =
handleEventLensed fileBrowserEntriesL (listMoveByPages (0.5::Double))
updateWithLens fileBrowserEntriesL (listMoveByPages (0.5::Double))
actionFileBrowserListTop :: Ord n => EventM n (FileBrowser n) ()
actionFileBrowserListTop =
@ -656,14 +656,14 @@ handleFileBrowserEventSearching :: (Ord n) => Vty.Event -> EventM n (FileBrowser
handleFileBrowserEventSearching e =
case e of
Vty.EvKey (Vty.KChar 'c') [Vty.MCtrl] ->
return $ updateFileBrowserSearch (const Nothing)
modify $ updateFileBrowserSearch (const Nothing)
Vty.EvKey Vty.KEsc [] ->
return $ updateFileBrowserSearch (const Nothing)
modify $ updateFileBrowserSearch (const Nothing)
Vty.EvKey Vty.KBS [] ->
return $ updateFileBrowserSearch (fmap safeInit)
Vty.EvKey Vty.KEnter [] ->
updateFileBrowserSearch (const Nothing) <$>
maybeSelectCurrentEntry
modify $ updateFileBrowserSearch (fmap safeInit)
Vty.EvKey Vty.KEnter [] -> do
maybeSelectCurrentEntry
modify $ updateFileBrowserSearch (const Nothing)
Vty.EvKey (Vty.KChar c) [] ->
modify $ updateFileBrowserSearch (fmap (flip T.snoc c))
_ ->
@ -726,11 +726,11 @@ maybeSelectCurrentEntry = do
then fileBrowserSelectedFilesL %= Set.insert (fileInfoFilename entry)
else case fileInfoFileType entry of
Just Directory ->
liftIO $ setWorkingDirectory (fileInfoFilePath entry) b
put =<< (liftIO $ setWorkingDirectory (fileInfoFilePath entry) b)
Just SymbolicLink ->
case fileInfoLinkTargetType entry of
Just Directory -> do
liftIO $ setWorkingDirectory (fileInfoFilePath entry) b
Just Directory ->
put =<< (liftIO $ setWorkingDirectory (fileInfoFilePath entry) b)
_ ->
return ()
_ ->