blob: fef6b368c42e001819e131c361d81f6ba3526a95 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use for_" #-}
{-# HLINT ignore "Use <=<" #-}
module Daffm.Action.Core where
import Brick (suspendAndResume')
import qualified Brick.Widgets.List as L
import Control.Monad.State (MonadIO (liftIO), MonadState, get, gets, modify, put)
import Daffm.State
import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FilePathText, FileType (..))
import qualified Data.Set as Set
import qualified Data.Text as Text
import System.Directory (getHomeDirectory)
import System.FilePath (takeDirectory)
import System.Process (callProcess)
modifyM :: (MonadState s m) => (s -> m s) -> m ()
modifyM f = get >>= f >>= put
loadDir :: FilePathText -> AppEvent ()
loadDir dir = do
modifyM (liftIO . (>>= filterInvalidSelections) . loadDirToState dir)
reloadDir :: AppEvent ()
reloadDir = do
AppState {stateCwd} <- get
loadDir stateCwd
goBackToParentDir :: AppEvent ()
goBackToParentDir = do
dir <- gets (Text.pack . takeDirectory . Text.unpack . stateCwd)
loadDir dir
changeDir :: FilePathText -> AppEvent ()
changeDir dir = do
loadDir dir
goHome :: AppEvent ()
goHome = do
liftIO getHomeDirectory >>= changeDir . Text.pack
openSelectedFile :: AppEvent ()
openSelectedFile = do
currentFile >>= \case
Just file -> openFile file
Nothing -> pure ()
openFile :: FileInfo -> AppEvent ()
openFile (FileInfo {filePath, fileType = Directory}) = loadDir filePath
openFile (FileInfo {filePath, fileType}) = do
suspendAndResume' $ do
putStrLn $ "Opening " <> show fileType <> ": " <> Text.unpack filePath
callProcess "nvim" [Text.unpack filePath]
currentFile :: AppEvent (Maybe FileInfo)
currentFile = do
gets (fmap snd . L.listSelectedElement . stateFiles)
toggleCurrentFileSelection :: AppEvent ()
toggleCurrentFileSelection = do
currentFile >>= \case
Just file -> modify $ toggleFileSelection (filePath file)
Nothing -> pure ()
moveCurrent 1
clearFileSelections :: AppEvent ()
clearFileSelections =
modify $ \s -> s {stateFileSelections = Set.empty}
moveCurrent :: Int -> AppEvent ()
moveCurrent count = do
files <- gets stateFiles
modify $ \s -> s {stateFiles = L.listMoveBy count files}
|