brick/programs/FileBrowserDemo.hs

110 lines
3.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Main where
import qualified Control.Exception as E
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import qualified Graphics.Vty as V
import Control.Monad.State (get)
import qualified Data.Text as Text
import qualified Brick.Main as M
import qualified Brick.Widgets.List as L
import Brick.AttrMap (AttrName)
import Brick.Types
( Widget
, BrickEvent(..)
)
import Brick.Widgets.Center
( center
2018-12-01 02:13:16 +03:00
, hCenter
)
import Brick.Widgets.Border
2018-11-30 22:23:11 +03:00
( borderWithLabel
)
import Brick.Widgets.Core
2018-12-01 02:13:16 +03:00
( vBox, (<=>), padTop
, hLimit, vLimit, txt
, withDefAttr, emptyWidget
, Padding(..)
)
2021-04-29 22:14:08 +03:00
import qualified Brick.Widgets.FileBrowser as FB
import qualified Brick.AttrMap as A
2018-11-30 21:04:22 +03:00
import Brick.Util (on, fg)
import qualified Brick.Types as T
data Name = FileBrowser1
deriving (Eq, Show, Ord)
2021-04-29 22:14:08 +03:00
drawUI :: FB.FileBrowser Name -> [Widget Name]
2018-12-01 02:13:16 +03:00
drawUI b = [center $ ui <=> help]
where
2018-12-01 02:13:16 +03:00
ui = hCenter $
vLimit 15 $
hLimit 50 $
2018-11-30 22:23:11 +03:00
borderWithLabel (txt "Choose a file") $
FB.renderFileBrowser True b
help = padTop (Pad 1) $
2021-04-29 22:14:08 +03:00
vBox [ case FB.fileBrowserException b of
Nothing -> emptyWidget
Just e -> hCenter $ withDefAttr errorAttr $
txt $ Text.pack $ E.displayException e
, hCenter $ txt "Up/Down: select"
, hCenter $ txt "/: search, Ctrl-C or Esc: cancel search"
, hCenter $ txt "Enter: change directory or select file"
, hCenter $ txt "Esc: quit"
2018-12-01 02:13:16 +03:00
]
appEvent :: BrickEvent Name e -> T.EventM Name (FB.FileBrowser Name) ()
appEvent (VtyEvent ev) = do
b <- get
case ev of
2021-04-29 22:14:08 +03:00
V.EvKey V.KEsc [] | not (FB.fileBrowserIsSearching b) ->
M.halt
2018-11-30 21:04:22 +03:00
_ -> do
FB.handleFileBrowserEvent ev
2018-11-30 21:04:22 +03:00
-- If the browser has a selected file after handling the
-- event (because the user pressed Enter), shut down.
case ev of
V.EvKey V.KEnter [] -> do
b' <- get
2021-04-29 22:14:08 +03:00
case FB.fileBrowserSelection b' of
[] -> return ()
_ -> M.halt
_ -> return ()
appEvent _ = return ()
errorAttr :: AttrName
errorAttr = "error"
theMap :: A.AttrMap
theMap = A.attrMap V.defAttr
[ (L.listSelectedFocusedAttr, V.black `on` V.yellow)
2018-11-30 22:14:12 +03:00
, (FB.fileBrowserCurrentDirectoryAttr, V.white `on` V.blue)
, (FB.fileBrowserSelectionInfoAttr, V.white `on` V.blue)
2018-11-30 21:04:22 +03:00
, (FB.fileBrowserDirectoryAttr, fg V.blue)
, (FB.fileBrowserBlockDeviceAttr, fg V.magenta)
, (FB.fileBrowserCharacterDeviceAttr, fg V.green)
, (FB.fileBrowserNamedPipeAttr, fg V.yellow)
, (FB.fileBrowserSymbolicLinkAttr, fg V.cyan)
2018-12-01 07:20:22 +03:00
, (FB.fileBrowserUnixSocketAttr, fg V.red)
, (FB.fileBrowserSelectedAttr, V.white `on` V.magenta)
, (errorAttr, fg V.red)
]
2021-04-29 22:14:08 +03:00
theApp :: M.App (FB.FileBrowser Name) e Name
theApp =
M.App { M.appDraw = drawUI
, M.appChooseCursor = M.showFirstCursor
, M.appHandleEvent = appEvent
, M.appStartEvent = return ()
, M.appAttrMap = const theMap
}
main :: IO ()
main = do
b <- M.defaultMain theApp =<< FB.newFileBrowser FB.selectNonDirectories FileBrowser1 Nothing
putStrLn $ "Selected entry: " <> show (FB.fileBrowserSelection b)