test FileBrowser widget

This commit is contained in:
Jonathan Lamothe 2023-05-22 13:58:34 -04:00
parent 70afacfee8
commit 776593d853

View File

@ -20,35 +20,44 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
module Main (main) where module Main (main) where
import Brick.AttrMap (forceAttrMap) import Brick.AttrMap (AttrMap, attrMap)
import Brick.Main (App (..), defaultMain, halt, showFirstCursor) import Brick.Main (App (..), defaultMain, halt, showFirstCursor)
import Brick.Types (BrickEvent (VtyEvent), EventM, Widget) import Brick.Types (BrickEvent (VtyEvent), EventM, Widget)
import Brick.Util (on) import Brick.Util (on)
import Brick.Widgets.Core (str, vBox) import Brick.Widgets.FileBrowser (
import Brick.Widgets.Edit (Editor, editor, handleEditorEvent, renderEditor) FileBrowser,
fileBrowserSelectedAttr,
handleFileBrowserEvent,
newFileBrowser,
renderFileBrowser)
import Control.Monad (void) import Control.Monad (void)
import Graphics.Vty.Attributes.Color (black, white) import Graphics.Vty.Attributes.Color (black, white)
import Graphics.Vty.Input.Events (Event (EvKey), Key (KEsc)) import Graphics.Vty.Input.Events (Event (EvKey), Key (KEsc))
app :: App (Editor String ()) () () app :: App (FileBrowser ()) () ()
app = App app = App
{ appDraw = draw { appDraw = draw
, appChooseCursor = showFirstCursor , appChooseCursor = showFirstCursor
, appHandleEvent = eventHandler , appHandleEvent = eventHandler
, appStartEvent = return () , appStartEvent = return ()
, appAttrMap = const $ forceAttrMap $ white `on` black , appAttrMap = const myAttrMap
} }
draw :: Editor String () -> [Widget ()] draw :: FileBrowser () -> [Widget ()]
draw = return . renderEditor (vBox . map str) True draw = return . renderFileBrowser True
eventHandler :: BrickEvent () () -> EventM () (Editor String ()) () eventHandler :: BrickEvent () () -> EventM () (FileBrowser ()) ()
eventHandler (VtyEvent (EvKey KEsc _)) = halt eventHandler (VtyEvent (EvKey KEsc _)) = halt
eventHandler e = handleEditorEvent e eventHandler (VtyEvent e) = handleFileBrowserEvent e
eventHandler _ = return ()
myAttrMap :: AttrMap
myAttrMap = attrMap (white `on` black)
[ ( fileBrowserSelectedAttr , black `on` white )
]
main :: IO () main :: IO ()
main = let main = void $ newFileBrowser (const True) () Nothing
s = editor () Nothing "" >>= defaultMain app
in void $ defaultMain app s
--jl --jl