2018-11-30 03:21:15 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
module Main where
|
|
|
|
|
2018-12-03 21:33:47 +03:00
|
|
|
import qualified Control.Exception as E
|
2018-11-30 03:21:15 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
|
|
|
import Data.Monoid
|
|
|
|
#endif
|
|
|
|
import qualified Graphics.Vty as V
|
|
|
|
|
2022-07-17 09:10:03 +03:00
|
|
|
import Control.Monad.State (get)
|
2018-12-03 21:33:47 +03:00
|
|
|
import qualified Data.Text as Text
|
2018-11-30 03:21:15 +03:00
|
|
|
import qualified Brick.Main as M
|
|
|
|
import qualified Brick.Widgets.List as L
|
2022-08-05 04:44:57 +03:00
|
|
|
import Brick.AttrMap (AttrName, attrName)
|
2018-11-30 03:21:15 +03:00
|
|
|
import Brick.Types
|
|
|
|
( Widget
|
|
|
|
, BrickEvent(..)
|
|
|
|
)
|
|
|
|
import Brick.Widgets.Center
|
|
|
|
( center
|
2018-12-01 02:13:16 +03:00
|
|
|
, hCenter
|
2018-11-30 03:21:15 +03:00
|
|
|
)
|
|
|
|
import Brick.Widgets.Border
|
2018-11-30 22:23:11 +03:00
|
|
|
( borderWithLabel
|
2018-11-30 03:21:15 +03:00
|
|
|
)
|
|
|
|
import Brick.Widgets.Core
|
2018-12-01 02:13:16 +03:00
|
|
|
( vBox, (<=>), padTop
|
|
|
|
, hLimit, vLimit, txt
|
2018-12-03 21:33:47 +03:00
|
|
|
, withDefAttr, emptyWidget
|
2022-07-23 02:07:00 +03:00
|
|
|
, Padding(..)
|
2018-11-30 03:21:15 +03:00
|
|
|
)
|
2021-04-29 22:14:08 +03:00
|
|
|
import qualified Brick.Widgets.FileBrowser as FB
|
2018-11-30 03:21:15 +03:00
|
|
|
import qualified Brick.AttrMap as A
|
2018-11-30 21:04:22 +03:00
|
|
|
import Brick.Util (on, fg)
|
2018-11-30 03:21:15 +03:00
|
|
|
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]
|
2018-11-30 03:21:15 +03:00
|
|
|
where
|
2018-12-01 02:13:16 +03:00
|
|
|
ui = hCenter $
|
2018-11-30 03:21:15 +03:00
|
|
|
vLimit 15 $
|
|
|
|
hLimit 50 $
|
2018-11-30 22:23:11 +03:00
|
|
|
borderWithLabel (txt "Choose a file") $
|
2018-11-30 03:21:15 +03:00
|
|
|
FB.renderFileBrowser True b
|
2022-07-23 02:07:00 +03:00
|
|
|
help = padTop (Pad 1) $
|
2021-04-29 22:14:08 +03:00
|
|
|
vBox [ case FB.fileBrowserException b of
|
2018-12-03 21:33:47 +03:00
|
|
|
Nothing -> emptyWidget
|
|
|
|
Just e -> hCenter $ withDefAttr errorAttr $
|
|
|
|
txt $ Text.pack $ E.displayException e
|
|
|
|
, hCenter $ txt "Up/Down: select"
|
2018-12-01 02:16:35 +03:00
|
|
|
, 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
|
|
|
]
|
2018-11-30 03:21:15 +03:00
|
|
|
|
2022-07-17 09:10:03 +03:00
|
|
|
appEvent :: BrickEvent Name e -> T.EventM Name (FB.FileBrowser Name) ()
|
|
|
|
appEvent (VtyEvent ev) = do
|
|
|
|
b <- get
|
2018-11-30 03:21:15 +03:00
|
|
|
case ev of
|
2021-04-29 22:14:08 +03:00
|
|
|
V.EvKey V.KEsc [] | not (FB.fileBrowserIsSearching b) ->
|
2022-07-17 09:10:03 +03:00
|
|
|
M.halt
|
2018-11-30 21:04:22 +03:00
|
|
|
_ -> do
|
2022-07-17 09:10:03 +03:00
|
|
|
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.
|
2018-12-06 22:54:25 +03:00
|
|
|
case ev of
|
2022-07-17 09:10:03 +03:00
|
|
|
V.EvKey V.KEnter [] -> do
|
|
|
|
b' <- get
|
2021-04-29 22:14:08 +03:00
|
|
|
case FB.fileBrowserSelection b' of
|
2022-07-17 09:10:03 +03:00
|
|
|
[] -> return ()
|
|
|
|
_ -> M.halt
|
|
|
|
_ -> return ()
|
|
|
|
appEvent _ = return ()
|
2018-11-30 03:21:15 +03:00
|
|
|
|
2018-12-03 21:33:47 +03:00
|
|
|
errorAttr :: AttrName
|
2022-08-05 04:44:57 +03:00
|
|
|
errorAttr = attrName "error"
|
2018-12-03 21:33:47 +03:00
|
|
|
|
2018-11-30 03:21:15 +03:00
|
|
|
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)
|
2018-11-30 22:49:08 +03:00
|
|
|
, (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)
|
2018-12-06 22:54:25 +03:00
|
|
|
, (FB.fileBrowserSelectedAttr, V.white `on` V.magenta)
|
2018-12-03 21:33:47 +03:00
|
|
|
, (errorAttr, fg V.red)
|
2018-11-30 03:21:15 +03:00
|
|
|
]
|
|
|
|
|
2021-04-29 22:14:08 +03:00
|
|
|
theApp :: M.App (FB.FileBrowser Name) e Name
|
2018-11-30 03:21:15 +03:00
|
|
|
theApp =
|
|
|
|
M.App { M.appDraw = drawUI
|
|
|
|
, M.appChooseCursor = M.showFirstCursor
|
|
|
|
, M.appHandleEvent = appEvent
|
2022-07-17 09:10:03 +03:00
|
|
|
, M.appStartEvent = return ()
|
2018-11-30 03:21:15 +03:00
|
|
|
, M.appAttrMap = const theMap
|
|
|
|
}
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2018-12-03 21:57:10 +03:00
|
|
|
b <- M.defaultMain theApp =<< FB.newFileBrowser FB.selectNonDirectories FileBrowser1 Nothing
|
2018-11-30 03:21:15 +03:00
|
|
|
putStrLn $ "Selected entry: " <> show (FB.fileBrowserSelection b)
|