aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm/Action/Core.hs
blob: 337801e42e02d65c8f3903433f4836416d2bfa5f (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
{-# 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 -> FilePathText -> AppEvent ()
loadDir dir parentDir = do
  modifyM (liftIO . (>>= filterInvalidSelections) . loadDirToState dir parentDir)

reloadDir :: AppEvent ()
reloadDir = do
  AppState {stateCwd, stateParentDir} <- get
  loadDir stateCwd stateParentDir

goBackToParentDir :: AppEvent ()
goBackToParentDir = do
  dir <- gets stateParentDir
  loadDir dir (Text.pack . takeDirectory $ Text.unpack dir)

goHome :: AppEvent ()
goHome = do
  dir <- liftIO getHomeDirectory
  loadDir (Text.pack dir) (Text.pack $ takeDirectory dir)

openSelectedFile :: AppEvent ()
openSelectedFile = do
  currentFile >>= \case
    Just file -> openFile file
    Nothing -> pure ()

openFile :: FileInfo -> AppEvent ()
openFile (FileInfo {filePath, fileType = Directory}) = do
  gets stateCwd >>= 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}